Find the Last Matching Element in an Array Using C#

karthikeya Boyini
Updated on 22-Jun-2020 13:59:37

423 Views

To find the last matching element, use the Array.LastIndexOf method. Returns -1 if the element isn’t present in the integer array.The following is the array −int[] val = { 97, 45, 76, 21, 89, 45 };Now, let’s say you need to search the last Index of element 45. For that, use Array.LastIndexOf() method −int res = Array.LastIndexOf(val, 45);The following is an example −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // last Index of element 45       int res = Array.LastIndexOf(val, 45);       // Display the index       Console.WriteLine("Index of element 45 is = "+res);    } }OutputIndex of element 45 is = 5

TrueForAll Method in C# Arrays

Samual Sam
Updated on 22-Jun-2020 13:59:12

163 Views

With TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // checking whether all the array element are more than one or not       bool result = Array.TrueForAll(val, res => res > 1);       Console.WriteLine(result);    } }OutputTrueWith TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; ... Read More

Clear a List in C#

karthikeya Boyini
Updated on 22-Jun-2020 13:58:40

456 Views

Firstly, set a list −List myList = new List(); myList.Add(45); myList.Add(77);Now, to clear the above list, use Clear() −myList.Clear();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add(45);       myList.Add(77);       Console.WriteLine("Elements: "+myList.Count);       myList.Clear();       Console.WriteLine("Elements after using clear: "+myList.Count);    } }OutputElements: 2 Elements after using clear: 0

Create a List from Elements of an Array in C#

Samual Sam
Updated on 22-Jun-2020 13:58:10

484 Views

Set an array −int[] val = new int[5]; // integer elements val[0] = 15; val[1] = 25; val[2] = 40; val[3] = 58; val[4] = 70;Now set a list and add array in it −List myList = new List(val);The following is the code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] val = new int[5];       // integer elements       val[0] = 15;       val[1] = 25;       val[2] = 40;       val[3] = 58;       val[4] = 70;       List myList = new List(val);       Console.WriteLine("Elements...");       foreach(int res in myList) {          Console.WriteLine(res);       }       // count integer elements       Console.WriteLine("Number of elements: "+myList.Count);    } }OutputElements... 15 25 40 58 70 Number of elements: 5

Display All Records from MySQL Table Using PHP and mysql_fetch_assoc

seetha
Updated on 22-Jun-2020 13:57:51

455 Views

To illustrate this we are fetching all the records from a table named ‘Tutorials_tbl’ with the help of PHP script that uses mysql_fetch_assoc() function in the following example −Example

Enum.GetName in C#

Ankith Reddy
Updated on 22-Jun-2020 13:57:32

643 Views

Gets the string representation of an Enum value using the Enum.GetName.It has two parameters −Type − Enumeration typeObject − Value of an enumerationThe following is an example −Example Live Demousing System; class Demo {    enum Vehicle {       Car,       Motorbike,       Truck,       Bicycles    };    static void Main() {       // Usig GetName to get the string representation of enum type       string res = Enum.GetName(typeof(Vehicle), Vehicle.Motorbike);           // Displaying       Console.WriteLine(res);    } }OutputMotorbike

Get Names from Enum in C#

Arjun Thakur
Updated on 22-Jun-2020 13:57:05

65 Views

It gets an array of the names of constants in an enumeration. The following is the syntax −Enum.GetNames(Type)Here, Type is an enumeration type.The following is an example −Example Live Demousing System; class Demo {    enum Vehicle {       Car,       Motorbike,       Truck,    };    static void Main() {       // display the enum       foreach ( string res in Enum.GetNames ( typeof (Vehicle)))       Console.WriteLine (res);    } }OutputCar Motorbike Truck

C# NullReferenceException

Chandu yadav
Updated on 22-Jun-2020 13:56:42

273 Views

NullReferenceException occurs when you try to to access member fields, or function types that points to null.Here is an example −Example Live Demousing System; class Demo {    static void Main() {       string str = null;       if (str.Length > 0) {          Console.WriteLine(str);       }    } }OutputThe following is the output. It throws NullReferenceException, since you are tryonhg access a memebt that points to null −Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Demo.Main () [0x00002] in :0

Combine Two Arrays in C#

George John
Updated on 22-Jun-2020 13:56:18

5K+ Views

Firstly, declare and initialize two arrays −int[] arr1 = { 37, 45, 65 }; int[] arr2 = { 70, 89, 118 };Now create a new list −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);Use the AddRange() method the arrays into the newly created list.myList.AddRange(arr1); myList.AddRange(arr2);Now convert the list into array as shown below −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       int[] arr1 = { 37, 45, 65 };       int[] arr2 = { 70, 89, 118 };       // displaying array1       Console.WriteLine("Array 1...");     ... Read More

PHP Functions to Fetch Data from MySQL Table

Nikitha N
Updated on 22-Jun-2020 13:55:09

205 Views

PHP uses following functions to fetch data from an existing MySQL table −mysql_query() functionThis function is used in PHP script to fetch data from an existing MySQL table. This function takes two parameters and returns TRUE on success or FALSE on failure. Its syntax is as follows −Syntaxbool mysql_query( sql, connection );Followings are the parameters used in this function −S. No.Parameter & Description1.SqlRequired - SQL query to fetch data from an existing MySQL table2.connectionOptional - if not specified, then the last opened connection by mysql_connect will be used.mysql_fetch_array() functionThis is another function which is used in PHP script while fetching ... Read More

Advertisements