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
Csharp Articles
Page 133 of 196
Read 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 MoreDefault value of StringBuilder in C#
The default operator in C# returns the default value for any data type. For reference types like StringBuilder, the default value is null. This is useful when you need to initialize a StringBuilder variable without creating an instance immediately. Syntax Following is the syntax for using the default operator with StringBuilder − StringBuilder variable = default(StringBuilder); In C# 7.1 and later, you can also use the simplified syntax − StringBuilder variable = default; Using default(StringBuilder) Example using System; using System.Text; public class Demo { ...
Read MoreDefault value of bool in C#
The default operator in C# returns the default value for any data type. For the bool type, the default value is false. When a bool variable is declared without initialization, it automatically gets assigned the default value of false. Syntax Following is the syntax for using the default operator with bool − bool variable = default(bool); In C# 7.1 and later, you can also use the simplified syntax − bool variable = default; Using default(bool) Operator The following example demonstrates how to get the default value of bool ...
Read More