Csharp Articles

Page 147 of 196

Collection Initialization in C#

Ankith Reddy
Ankith Reddy
Updated on 17-Mar-2026 957 Views

Collection initialization in C# allows you to initialize collections with values at the time of creation using a concise syntax. This feature, introduced in C# 3.0, makes code more readable and eliminates the need for multiple Add() method calls. Collection initializers work with any collection type that implements IEnumerable and has an Add method, including List, Dictionary, arrays, and custom collections. Syntax Following is the syntax for collection initialization − CollectionType collectionName = new CollectionType { item1, item2, item3 }; For objects with properties, you can combine object and collection initialization − ...

Read More

Action Delegate in C#

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

The Action delegate in C# is a built-in generic delegate type that represents a method with no return value (void). It can accept zero or more input parameters and is particularly useful for passing methods as parameters or storing method references. Syntax Following is the basic syntax for declaring an Action delegate − Action actionDelegate = MethodName; For Action delegates with parameters − Action actionDelegate = MethodName; Action actionDelegate = MethodName; Action actionDelegate = MethodName; Where T, T1, T2, etc. are the parameter types. Using Action Delegate with Single ...

Read More

Multiple Where clause in C# Linq

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

In C# LINQ, you can apply multiple where clauses to filter collections based on different conditions. Each additional where clause further narrows down the results, creating a logical AND relationship between the conditions. Syntax There are two main approaches to using multiple where clauses − Query Syntax with Multiple Where Clauses: var result = from item in collection where condition1 where condition2 ...

Read More

C# Enum IsDefined Method

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

The Enum.IsDefined method in C# determines whether a specified value exists within a given enumeration. It returns true if the value is found, either as an integral value or its corresponding string name, and false otherwise. Syntax Following is the syntax for the Enum.IsDefined method − public static bool IsDefined(Type enumType, object value) Parameters enumType − The type of the enumeration to check against. value − The value to look for in the enumeration (can be an integer or string). Return Value Returns true if the specified value exists ...

Read More

C# int.Parse Method

Sai Subramanyam
Sai Subramanyam
Updated on 17-Mar-2026 25K+ Views

The int.Parse method in C# converts a string representation of a number to an integer. If the string cannot be converted to a valid integer, the method throws a FormatException. Syntax Following is the basic syntax for int.Parse − int result = int.Parse(stringValue); The method also has overloaded versions that accept additional parameters − int result = int.Parse(stringValue, NumberStyles.Integer); int result = int.Parse(stringValue, CultureInfo.InvariantCulture); Parameters s − A string containing a number to convert. style (optional) − A bitwise combination of enumeration values that indicates ...

Read More

C# OfType() Method

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

The OfType() method in C# is a LINQ extension method that filters a collection based on the type of its elements. It returns only the elements that match the specified type, making it useful for working with heterogeneous collections containing different data types. Syntax Following is the syntax for the OfType() method − public static IEnumerable OfType(this IEnumerable source) The method can be used with both method syntax and query syntax − // Method syntax var result = collection.OfType(); // Query syntax var result = from item in collection.OfType() select item; ...

Read More

C# Linq Where Method

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

The Where method in LINQ is used to filter elements from a collection based on a specified condition (predicate). It returns a new IEnumerable containing only the elements that satisfy the given criteria. Syntax The Where method has two overloads − // Filter based on element value only public static IEnumerable Where(this IEnumerable source, Func predicate) // Filter based on element value and index public static IEnumerable Where(this IEnumerable source, Func predicate) Parameters source − The input sequence to filter predicate − A function that tests each element for a condition ...

Read More

C# Program to make a copy of an existing file

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

The File.Copy method in C# is used to copy an existing file from one location to another. This method is part of the System.IO namespace and provides a simple way to duplicate files programmatically. Syntax Following is the syntax for the File.Copy method − File.Copy(string sourceFileName, string destFileName); File.Copy(string sourceFileName, string destFileName, bool overwrite); Parameters sourceFileName − The path of the file to copy. destFileName − The path of the destination file. overwrite − Optional boolean parameter. If true, overwrites the destination file if it already exists. ...

Read More

Delete a file in C#

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

In C#, you can use the File.Delete() method from the System.IO namespace to delete files from the file system. This method permanently removes the specified file if it exists. Syntax Following is the syntax for deleting a file − File.Delete(string path); Parameters path − A string specifying the path of the file to be deleted. This can be a relative or absolute path. Using File.Delete() Method The File.Delete() method removes the file at the specified path. If the file does not exist, the method does not throw an exception ...

Read More

The "E" and "e" custom specifiers in C#

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

The "E" and "e" custom specifiers in C# are used to format numbers in scientific notation (exponential notation). When any of these specifiers appear in a format string followed by at least one zero, the number is formatted with an "E" or "e" separator between the mantissa and the exponent. Syntax The following format specifiers create exponential notation − "E0" // Uppercase E with minimum exponent digits "e0" // Lowercase e with minimum exponent digits "E+0" // Uppercase E with explicit + sign "e+0" // Lowercase ...

Read More
Showing 1461–1470 of 1,951 articles
« Prev 1 145 146 147 148 149 196 Next »
Advertisements