Csharp Articles

Page 4 of 196

What is the purpose of the StringBuilder class in C#?

Akshay Khot
Akshay Khot
Updated on 11-Mar-2026 558 Views

In C#, strings are immutable. That means you cannot modify a string once it's created. Any modification to the string returns a new string that contains the modification, leaving the original string intact.string word = "aaabbbccc"; string newWord = word.Replace('b', 'd'); Console.WriteLine(word); // prints aaabbbccc Console.WriteLine(newWord); // prints aaadddcccStringBuilder class represents a string-like object that can be modified, i.e. a mutable string of characters. It's implemented differently than the string type, which represents an immutable string of characters.As modifying a string object creates a copy, repeatedly modifying string objects can incur a performance penalty. For small repetitions it's negligible, but ...

Read More

How to set a property having different datatype with a string value using reflection in C#?

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

Reflection is when managed code can read its own metadata to find assemblies. Essentially, it allows code to inspect other code within the same system. With reflection in C#, we can dynamically create an instance of a type and bind that type to an existing object. Moreover, we can get the type from an existing object and access its properties. When we use attributes in our code, reflection gives us access as it provides objects of Type that describe modules, assemblies, and types.Let us say we have a property of type double and in the runtime we actually have the ...

Read More

How to return a string repeated N number of times in C#?

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

Use string instance string repeatedString = new string(charToRepeat, 5) to repeat the character "!" with specified number of times.Use string.Concat(Enumerable.Repeat(charToRepeat, 5)) to repeat the character "!" with specified number of times.Use StringBuilder builder = new StringBuilder(stringToRepeat.Length * 5); to repeat the character "!" with specified number of times.Using string instanceExampleusing System; namespace DemoApplication{    public class Program{       static void Main(string[] args){          string myString = "Hi";          Console.WriteLine($"String: {myString}");          char charToRepeat = '!';          Console.WriteLine($"Character to repeat: {charToRepeat}");          string repeatedString ...

Read More

How to flatten a list using LINQ C#?

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

Flattening a list means converting a List to List. For example, let us consider a List which needs to be converted to List.The SelectMany in LINQ is used to project each element of a sequence to an IEnumerable and then flatten the resulting sequences into one sequence. That means the SelectMany operator combines the records from a sequence of results and then converts it into one result.Using SelectManyExampleusing System; using System.Collections.Generic; using System.Linq; namespace DemoApplication{    public class Program{       static void Main(string[] args){          List listOfNumLists = new List{           ...

Read More

What is the difference between | and || operators in c#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 949 Views

|| is called logical OR operator and | is called bitwise logical OR but the basic difference between them is in the way they are executed. The syntax for || and | the same as in the following −bool_exp1 || bool_exp2bool_exp1 | bool_exp2Now the syntax of 1 and 2 looks similar to each other but the way they will execute is entirely different.In the first statement, first bool_exp1 will be executed and then the result of this expression decides the execution of the other statement.If it is true, then the OR will be true so it makes no sense to execute ...

Read More

How to convert an integer to string with padding zero in C#?

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

There are several ways to convert an integer to a string in C#.PadLeft − Returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode characterToString − Returns a string that represents the current object.String Interpolation − The $ special character identifies a string literal as an interpolated string. This feature is available starting with C# 6.Example using string padding−Exampleusing System; namespace DemoApplication{    class Program{       public static void Main(){          int number = 5;          Console.WriteLine("Number: ...

Read More

How to convert an integer to hexadecimal and vice versa in C#?

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

Converting Integer to HexadecimalAn integer can be converted to a hexadecimal by using the string.ToString() extension method.Integer Value: 500 Hexadecimal Value: 1F4Converting Hexadecimal to Integer −A hexadecimal value can be converted to an integer using int.Parse or convert.ToInt32int.Parse − Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.Hexadecimal Value: 1F4 Integer Value: 500Convert.ToInt32 −Converts a specified value to a 32-bit signed integer.Hexadecimal Value: 1F4 Integer Value: 500Converting Integer to Hexadecimal −string hexValue = integerValue.ToString("X");Exampleusing System; namespace DemoApplication{    public class Program{       public static void Main(){ ...

Read More

How to replace multiple spaces with a single space in C#?

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

There are several ways to replace multiple spaces with single space in C#.String.Replace − Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.Replace(String, String, Boolean, CultureInfo)String.Join Concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member.Regex.Replace −In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.Example using Regex −Exampleusing System; using System.Text.RegularExpressions; namespace DemoApplication{    class Program{       public ...

Read More

How to get a path to the desktop for current user in C#?

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

The desktop path of the current user can be fetched by using Environment.SpecialFolder. The Environment.SpecialFolder gets the path to the system special folder that is identified by the specified enumeration.string desktopPath =Environment.GetFolderPath(Environment.SpecialFolder.Desktop)The System.Environment Class provides information about the current environment and platform. The System.Environment Class uses to retrieve Environment variable settings, Version of the common language runtime, contents of the call stack etc. This class cannot be inherited.Environment class is static class which Provides the system configuration, Current program execution Environment as wel some properties for string manipulation such as news line, System Namespace represents the Environment Class.Environment class is ...

Read More

How to validate an email address in C#?

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

There are several ways to validate an email address in C#.System.Net.Mail −The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.System.Text.RegularExpressions − Represents an immutable regular expression.Use the below expression@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1, 3}\.[0-9]{1, 3}\.[0-9]{1, 3}\.)|(([a-zA-Z0-9\-]+\.)+))([azA-Z]{2, 4}|[0-9]{1, 3})(\]?)$"We can use the MailAddress class of the System.Net.Mail namespace to validate an email addressExampleusing System; using System.Net.Mail; namespace DemoApplication{    class Program{       public static void Main(){          try{             string email = "hello@xyzcom";             Console.WriteLine($"The email is {email}");   ...

Read More
Showing 31–40 of 1,951 articles
« Prev 1 2 3 4 5 6 196 Next »
Advertisements