Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 808 of 2109
GroupBy() Method in C#
The GroupBy() method in C# is a LINQ extension method that groups elements from a collection based on a specified key selector function. It returns an IGrouping where elements sharing the same key are grouped together. Syntax Following is the syntax for the GroupBy() method − public static IEnumerable GroupBy( this IEnumerable source, Func keySelector ) Parameters source − The collection to group elements from. keySelector − A function that extracts the key for each element. Return Value Returns an ...
Read MoreOrderby clause in C#
The orderby clause in C# is used to sort elements in a collection based on one or more specified criteria. It supports both ascending (default) and descending order, and can be used with LINQ query expressions or method syntax. Syntax Following is the syntax for using orderby in query expression − var result = from element in collection orderby element.Property [ascending|descending] select element; Following is the syntax for ...
Read MoreRead in a file in C# with StreamReader
The StreamReader class in C# is used to read text files efficiently. It provides methods to read characters, lines, or the entire content of a text file. StreamReader is part of the System.IO namespace and implements IDisposable, making it suitable for use with using statements for automatic resource cleanup. Syntax Following is the syntax to create a StreamReader object − StreamReader sr = new StreamReader("filename.txt"); Following is the syntax to use StreamReader with automatic disposal − using (StreamReader sr = new StreamReader("filename.txt")) { // read operations } ...
Read MoreC# program to return an array from methods
In C#, methods can return arrays by specifying the array type in the method signature and using the return statement. This allows you to create, populate, and return arrays from methods to be used by the calling code. Syntax Following is the syntax for declaring a method that returns an array − dataType[] MethodName() { // create and populate array return arrayVariable; } To call the method and use the returned array − dataType[] result = MethodName(); Returning String Arrays from Methods ...
Read MoreC# program to get the last element from an array
In C#, there are several ways to get the last element from an array. The most common approach is to use the array's Length property to access the element at the last index position. Syntax Following is the syntax to get the last element using array indexing − arrayName[arrayName.Length - 1] You can also use LINQ methods to get the last element − arrayName.Last() arrayName.LastOrDefault() Using Array Indexing The most efficient way to get the last element is by using the array's length minus one as the index − ...
Read MoreC# program to create an empty string array
Creating an empty string array in C# is useful when you need to declare an array without initial elements. There are several ways to create an empty string array, each serving different purposes depending on your requirements. Syntax Following are the different ways to create an empty string array − string[] str = new string[] {}; string[] str = new string[0]; string[] str = {}; Using Array Initializer Syntax The most straightforward way to create an empty string array is using the array initializer syntax with empty braces − using System; ...
Read MoreC# program to find the index of a word in a string
Finding the index of a word in a string or array is a common programming task in C#. The Array.IndexOf() method provides an efficient way to locate the position of a specific element within an array of strings. Syntax Following is the syntax for using Array.IndexOf() method − int index = Array.IndexOf(array, searchValue); Parameters array − The one-dimensional array to search searchValue − The object to locate in the array Return Value The method returns the index of the first occurrence of the specified value. If the value is ...
Read MoreC# program to separate joined strings in C#
In C#, you can join string arrays into a single string using string.Join() and then separate the joined string back into individual elements using the Split() method. This is useful when you need to manipulate strings by combining them temporarily and then extracting the original components. Syntax Following is the syntax for joining strings − string joinedString = string.Join(separator, stringArray); Following is the syntax for separating joined strings − string[] separatedArray = joinedString.Split(separator); Parameters separator − The character or string used to separate elements during joining and ...
Read MoreBool Array in C#
A bool array in C# is used to store multiple boolean values (true and false) in a single collection. Boolean arrays are useful for tracking states, flags, or conditions across multiple elements. Syntax Following are the different ways to declare and initialize a bool array − // Declaration with size bool[] arrayName = new bool[size]; // Declaration with initialization bool[] arrayName = {true, false, true}; // Using new keyword with values bool[] arrayName = new bool[] {true, false, true}; Creating and Initializing Bool Arrays Method 1: Declaration with Size and Assignment ...
Read MoreDefault operator in C#
The default operator in C# returns the default value for any data type. For value types, it returns zero or false, while for reference types, it returns null. The default expressions are evaluated at compile-time, making them efficient for initialization. Starting with C# 7.1, you can use the simplified default literal when the type can be inferred from the context. Syntax Following is the syntax for the traditional default operator − default(Type) Following is the syntax for the simplified default literal (C# 7.1+) − Type variable = default; Default ...
Read More