Programming Articles

Page 861 of 2547

What is the difference between list and dictionary in C#?

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 6K+ Views

A List and Dictionary are both generic collections in C#, but they serve different purposes. A List stores elements in a sequential order with index-based access, while a Dictionary stores key-value pairs for fast lookups. Understanding when to use each collection type is crucial for writing efficient C# applications. Lists are ideal for ordered data where you need to access elements by position, while dictionaries are perfect for mapping relationships and quick key-based retrieval. Syntax Following is the syntax for creating a List − List listName = new List(); // or with initialization List listName ...

Read More

How to open a plain text file in C#?

Arjun Thakur
Arjun Thakur
Updated on 17-Mar-2026 354 Views

In C#, you can open and read a plain text file using the StreamReader class from the System.IO namespace. The StreamReader provides methods to read character data from a stream in a particular encoding. Syntax Following is the basic syntax for opening a text file with StreamReader − StreamReader sr = new StreamReader("filepath"); For proper resource management, use the using statement − using (StreamReader sr = new StreamReader("filepath")) { // Read file content } Using StreamReader to Read Text Files The most common approach is ...

Read More

How to validate a string for a numeric representation using TryParse in C#

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

The TryParse method in C# is used to validate whether a string contains a valid numeric representation. It attempts to convert the string to a number and returns true if successful, or false if the conversion fails. This is safer than using Parse because it doesn't throw exceptions on invalid input. Syntax Following is the syntax for using TryParse − bool result = int.TryParse(stringValue, out int number); Parameters stringValue − The string to be parsed. out number − The output parameter that receives the parsed value if successful, or ...

Read More

How to get int value from enum in C#?

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

In C#, an enum (enumeration) is a value type that represents a group of named constants. By default, each enum value has an underlying integer value starting from 0. You can extract the integer value from an enum using type casting. Syntax Following is the syntax for casting an enum to int − int value = (int)EnumName.EnumValue; Following is the syntax for declaring an enum − public enum EnumName { Value1, Value2, Value3 } Using Default Integer Values By default, enum values are assigned integer values starting from 0 ...

Read More

How to convert Lower case to Upper Case using C#?

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

To convert lowercase to uppercase in C#, use the ToUpper() method. This method converts all lowercase characters in a string to their uppercase equivalents and returns a new string without modifying the original string. Syntax Following is the syntax for the ToUpper() method − string.ToUpper() string.ToUpper(CultureInfo) Parameters CultureInfo (optional) − Specifies the culture-specific formatting information. If not provided, the current culture is used. Return Value The ToUpper() method returns a new string with all lowercase characters converted to uppercase. The original string remains unchanged. Using ToUpper() Method ...

Read More

How to perform Matrix Addition using C#?

George John
George John
Updated on 17-Mar-2026 1K+ Views

Matrix addition in C# involves adding corresponding elements from two matrices of the same dimensions. The matrices must have identical rows and columns for addition to be possible. The result is a new matrix where each element is the sum of the corresponding elements from the input matrices. Syntax Following is the basic syntax for matrix addition − int[, ] result = new int[rows, columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { ...

Read More

Different ways of Reading a file in C#

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

There are several ways to read files in C#, each suited for different scenarios. C# provides various classes in the System.IO namespace for file operations, including reading text files, binary files, and handling different data types efficiently. Using StreamReader for Text Files The StreamReader class is the most common way to read text files line by line. It provides efficient reading with proper resource management using the using statement − using System; using System.IO; class Program { static void Main(string[] args) { // ...

Read More

How to write multi-line comments in C#?

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

Multi-line comments in C# are comments that span more than one line. They are useful for adding detailed explanations, documenting code blocks, or temporarily disabling large sections of code during development. Syntax Multi-line comments in C# use the following syntax − /* This is a multi-line comment that can span multiple lines All text between /* and */ is ignored */ The compiler ignores everything between /* and */, making it perfect for lengthy explanations or code documentation. Basic Multi-line Comment Example using System; ...

Read More

Value parameters vs Reference parameters vs Output Parameters in C#

Chandu yadav
Chandu yadav
Updated on 17-Mar-2026 3K+ Views

In C#, there are three ways to pass parameters to methods: value parameters, reference parameters, and output parameters. Each mechanism behaves differently in terms of how data is passed and whether changes affect the original variables. Syntax Following are the syntax forms for the three parameter types − // Value parameter (default) public void Method(int value) { } // Reference parameter public void Method(ref int value) { } // Output parameter public void Method(out int value) { } Value Parameters Value parameters copy the actual value of an argument into the ...

Read More

Print a 2 D Array or Matrix in C#

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

A 2D array or matrix in C# is a data structure that stores elements in rows and columns. To print a 2D array, you need to use nested loops to iterate through each row and column, displaying the elements in a formatted manner. Syntax Following is the syntax for declaring a 2D array − dataType[, ] arrayName = new dataType[rows, columns]; Following is the syntax for printing a 2D array using nested loops − for (int i = 0; i < rows; i++) { for (int j = ...

Read More
Showing 8601–8610 of 25,466 articles
« Prev 1 859 860 861 862 863 2547 Next »
Advertisements