Csharp Articles

Page 171 of 196

How to use the ?: conditional operator in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 382 Views

The conditional operator (also called the ternary operator) is represented by the symbol ?:. It provides a concise way to write simple if-else statements in a single expression. The operator has right-to-left associativity and evaluates a boolean condition to return one of two possible values. Syntax Following is the syntax for the conditional operator − condition ? value_if_true : value_if_false Where: condition is an expression that evaluates to a boolean value value_if_true is returned if the condition is true value_if_false is returned if the condition is false How It Works ...

Read More

How to use the GetLowerBound method of array class in C#

Samual Sam
Samual Sam
Updated on 17-Mar-2026 539 Views

The GetLowerBound() method of the Array class in C# returns the lower bound of the specified dimension in an array. For most arrays in C#, the lower bound is typically 0, but this method becomes useful when working with arrays that have custom bounds or multi-dimensional arrays. Syntax Following is the syntax for the GetLowerBound() method − public int GetLowerBound(int dimension); Parameters dimension − A zero-based dimension of the array whose lower bound needs to be found. Return Value Returns an integer representing the lower bound of the specified ...

Read More

How to sort a list in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 288 Views

Sorting a list in C# can be accomplished using several methods. The List class provides the Sort() method for in-place sorting, while LINQ offers additional sorting capabilities. This article demonstrates various approaches to sort lists in ascending and descending order. Syntax Following is the syntax for the basic Sort() method − list.Sort(); Following is the syntax for sorting with a custom comparer − list.Sort((x, y) => y.CompareTo(x)); // descending order Using List.Sort() Method The Sort() method sorts the elements in the entire list using the default comparer for the ...

Read More

How to use the GetType method of array class in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

The GetType() method in C# is inherited from the Object class and returns the exact runtime type of the current instance. When used with arrays, it provides information about the array's type, including its element type and dimensions. Syntax Following is the syntax for using the GetType() method − Type type = arrayInstance.GetType(); Return Value The method returns a Type object that represents the exact runtime type of the current instance. Using GetType() with Arrays When applied to arrays, GetType() returns the array type, which includes information about the element type ...

Read More

How to use the GetValue() method of array class in C#?

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 3K+ Views

The GetValue() method of the Array class in C# retrieves the value at a specified position in a multi-dimensional or single-dimensional array. This method is particularly useful when working with arrays created using Array.CreateInstance(), as it provides a generic way to access array elements regardless of the array's underlying type. Syntax Following is the syntax for using GetValue() with different array dimensions − // For single-dimensional array object value = array.GetValue(index); // For multi-dimensional array object value = array.GetValue(index1, index2); object value = array.GetValue(index1, index2, index3); Parameters index − A 32-bit ...

Read More

How to use the Main() method in C#?

George John
George John
Updated on 17-Mar-2026 857 Views

The Main() method in C# is the entry point of any C# application. It is a static method that runs when the program starts, without requiring an instance of the class to be created. The Main() method defines what the class does when executed and can instantiate other objects and variables. Syntax Following are the valid signatures for the Main() method − static void Main() static void Main(string[] args) static int Main() static int Main(string[] args) Parameters The Main() method signature includes the following components − static − The method ...

Read More

How to use the return statement in C#?

Samual Sam
Samual Sam
Updated on 17-Mar-2026 683 Views

The return statement in C# is used to exit a method and optionally return a value to the caller. When a method contains a return statement, the program control immediately transfers back to the calling method along with the specified return value. Syntax Following is the syntax for using the return statement − return; // For void methods (no value returned) return expression; // For methods that return a value Using Return Statement with Value Types When a method has a return type other than void, it must return a ...

Read More

How to use the ToString() method of array in C#?

George John
George John
Updated on 17-Mar-2026 546 Views

The ToString() method in C# returns a string representation of an object. For arrays, the default ToString() method returns the type name rather than the array contents. However, ToString() is commonly used with array properties and methods that return numeric values. Syntax Following is the syntax for using ToString() with array methods − arrayObject.Method().ToString() For converting array elements to strings − array[index].ToString() Using ToString() with Array Properties The ToString() method is useful when converting array properties like bounds and length to string format for display purposes − ...

Read More

How to use XmlSerializer in C#?

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 643 Views

The XmlSerializer in C# is used to convert objects to XML format (serialization) and back to objects (deserialization). This allows you to store object data in XML files or transmit data between applications in a standardized XML format. XmlSerializer provides fine-grained control over how objects are encoded into XML, including element names, attributes, and namespaces through various attributes. Syntax Following is the syntax for creating an XmlSerializer instance − XmlSerializer serializer = new XmlSerializer(typeof(ClassName)); Following is the syntax for serializing an object to XML − using (StreamWriter writer = new StreamWriter(filePath)) ...

Read More

How to select a random element from a C# list?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 45K+ Views

Selecting a random element from a C# list is a common task in programming. This involves using the Random class to generate a random index within the bounds of the list, then accessing the element at that index. Syntax Following is the basic syntax for selecting a random element from a list − Random random = new Random(); int index = random.Next(list.Count); var randomElement = list[index]; Using Random.Next() Method The Random.Next() method generates a random integer between 0 (inclusive) and the specified maximum value (exclusive). When you pass list.Count as the parameter, it ...

Read More
Showing 1701–1710 of 1,951 articles
« Prev 1 169 170 171 172 173 196 Next »
Advertisements