Csharp Articles

Page 7 of 196

How to create a comma separated string from a list of string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 13K+ Views

A List of string can be converted to a comma separated string using built in string.Join extension method.string.Join(", " , list);This type of conversion is really useful when we collect a list of data (Ex: checkbox selected data) from the user and convert the same to a comma separated string and query the database to process further.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          List fruitsList = new List {             "banana",             "apple", ...

Read More

Why the error Collection was modified; enumeration operation may not execute occurs and how to handle it in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 21K+ Views

This error occurs when a looping process is being running on a collection (Ex: List) and the collection is modified (data added or removed) during the runtime.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    public class Program {       static void Main(string[] args) {          try {             var studentsList = new List {                new Student {                   Id = 1,                   Name = "John"                },                new Student {                   Id = 0,                   Name = "Jack"                },                new Student {                   Id = 2,                   Name = "Jack"                }             };             foreach (var student in studentsList) {                if (student.Id

Read More

How to convert byte array to string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 20K+ Views

In .Net, every string has a character set and encoding. A character encoding tells the computer how to interpret raw zeroes and ones into real characters. It usually does this by pairing numbers with characters. Actually, it is the process of transforming a set of Unicode characters into a sequence of bytes.We can use Encoding.GetString Method (Byte[]) to decodes all the bytes in the specified byte array into a string. Several other decoding schemes are also available in Encoding class such as UTF8, Unicode, UTF32, ASCII etc. The Encoding class is available as part of System.Text namespace.string result = Encoding.Default.GetString(byteArray);Exampleusing ...

Read More

How to fetch a property value dynamically in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 7K+ Views

We can make use of Reflection to fetch a property value dynamically.Reflection provides objects (of type Type) that describe assemblies, modules and types. We can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If we use attributes in our code, reflection enables us to access them.The System.Reflection namespace and System.Type class play an important role in .NET Reflection. These two works together and allows us to reflect over many other aspects of a ...

Read More

What is the main difference between int.Parse() and Convert.ToInt32 in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 6K+ Views

Convert a string representation of number to an integer, using the int.Parse or Convert.ToInt32 method in C#. If the string cannot be converted, then the int.Parse or Convert.ToInt32 method returns an exceptionConvert.ToInt32 allows null value, it doesn't throw any errors Int.parse does not allow null value, and it throws an ArgumentNullException error.Exampleclass Program {    static void Main() {       int res;       string myStr = "5000";       res = int.Parse(myStr);       Console.WriteLine("Converting String is a numeric representation: " + res);       Console.ReadLine();    } }OutputConverting String is a numeric ...

Read More

How to replace line breaks in a string in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

Let us take we have to eliminate the line breaks, space and tab space from the below string.eliminate.jpgExampleWe can make use of Replace() extension method of string to do it.using System; namespace DemoApplication {    class Program {       static void Main(string[] args) {          string testString = "Hello \r beautiful \t world";          string replacedValue = testString.Replace("\r", "_").Replace("\t", "_");          Console.WriteLine(replacedValue);          Console.ReadLine();       }    } }OutputThe output of the above code isHello _ beautiful _ worldExampleWe can also make use of ...

Read More

How can we update the values of a collection using LINQ in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 13K+ Views

If the collection is a List, then we can make use of ForEach extension method which is available as part of LINQ.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static void Main(string[] args) {          List fruits = new List {             new Fruit {                Name = "Apple",                Size = "Small"             },             new Fruit {           ...

Read More

How to find items in one list that are not in another list in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 15K+ Views

LINQ Except operator comes under Set operators category in LINQThe Except() method requires two collections and finding those elements which are not present in the second collectionExcept for the extension, the method doesn't return the correct result for the collection of complex types.Example using Except() methodusing System; using System.Collections.Generic; using System.Linq; namespace DemoApplication {    class Program {       static void Main(string[] args) {          List animalsList1 = new List {             "tiger", "lion", "dog"          };          Console.WriteLine($"Values in List1:");       ...

Read More

How can we return null from a generic method in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

Generics allows us to define a class with placeholders for the type of its fields, methods, parameters, etc. Generics replace these placeholders with some specific type at compile time. A generic can be defined using angle brackets . A primary limitation of collections is the absence of effective type checking. This means that you can put any object in a collection because all classes in the C# programming language extend from the object base class.Also, we cannot simply return null from a generic method like in normal method. Below is the error that a generic method will throw if we ...

Read More

What is the use of yield return in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

Yield keyword helps to do custom stateful iteration over a collection. Meaning when we use yield keyword the control moves back and forth from the caller function to source and vice versa.Exampleusing System; using System.Collections.Generic; namespace DemoApplication {    class Program {       static List numbersList = new List {          1, 2, 3, 4, 5       };       public static void Main() {          foreach(int i in RunningTotal()) {             Console.WriteLine(i);          }          Console.ReadLine();   ...

Read More
Showing 61–70 of 1,951 articles
« Prev 1 5 6 7 8 9 196 Next »
Advertisements