Csharp Articles

Page 5 of 196

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

What is the best data type to use for currency in C#?

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

The best datatype to use for currency in C# is decimal. The decimal type is a 128-bit data type suitable for financial and monetary calculations. The decimal type can represent values ranging from 1.0 * 10^-28 to approximately 7.9 * 10^28 with 28-29 significant digits. To initialize a decimal variable, use the suffix m or M.decimal b = 2.1m;The below example shows the min and max value of decimal.Exampleusing System; namespace DemoApplication{    public class Program{       public static void Main(){          Console.WriteLine($"Deciaml Min Value: {decimal.MinValue}");          Console.WriteLine($"Deciaml Max Value: {decimal.MaxValue}");   ...

Read More

How to get the name of the current executable in C#?

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

There are several ways to get the name of the current executable in C#.Using System.AppDomain −Application domain provides isolation between code running in different app domains. App Domain is a logical container for code and data just like process and has separate memory space and access to resources. App domain also serves as a boundary like process does to avoid any accidental or illegal attempts to access the data of an object in one running application from another.System.AppDomain class provides us the ways to deal with application domain. It provides methods to create new application domain, unload domain from memory ...

Read More

What is the difference between Task.WhenAll() and Task.WaitAll() in C#?

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

The Task.WaitAll blocks the current thread until all other tasks have completed execution. The Task.WhenAll method is used to create a task that will complete if and only if all the other tasks have completed.If we are using Task.WhenAll we will get a task object that isn’t complete. However, it will not block but will allow the program to execute. On the contrary, the Task.WaitAll method call actually blocks and waits for all other tasks to complete.To understand with an example, let us say we have a task that performs some activity with the UI thread say some animation needs ...

Read More

How to force garbage collection in C#?

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

Yes it is possible to force garbage collector in C# to run by calling Collect() methodThis is not considered a good practice because this might create a performance over head. Collect() Forces an immediate garbage collection of all generations.Collect(Int32)Forces an immediate garbage collection from generation 0 through a specified generation.Exampleusing System; class MyGCCollectClass{    private const int maxGarbage = 1000;    static void Main(){       // Put some objects in memory.       MyGCCollectClass.MakeSomeGarbage();       Console.WriteLine("Memory used before collection: {0:N0}",       GC.GetTotalMemory(false));       // Collect all generations of memory.     ...

Read More

What if we are not sure of the type of value that we want to store in a variable. How to handle this in C#?

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

As C# is a strongly-typed language, every variable and constant has a pre-defined type. Before using any variable, we must tell the compiler what type of value a variable will store.If we are not sure about the type, then it is handled using dynamic programming. Dynamic programming is supported by the dynamic keyword.The dynamic keyword is used to declare dynamic types. The dynamic types tell the compiler that the object is defined as dynamic and skip type-checking at compiler time, delay type-checking until runtime. All syntaxes are checked and errors are thrown at runtime.Exampleusing System; namespace DemoDynamicKeyword{    class Program{ ...

Read More

How to store n number of lists of different types in a single generic list in C#?

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

We can store n number of lists of different types in a single generic list by creating a list of list of objects as shown below.List list = new List();Exampleusing System; using System.Collections.Generic; namespace MyApplication{    public class Program{       public static void Main(){          List list = new List();          List list1 = new List();          list1.Add(101);          list1.Add(102);          list1.Add(103);          list.Add(list1);          List list2 = new List();          list2.Add("Test1");     ...

Read More

What is the difference between int and Int32 in C#?

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

Int32 is a type provided by .NET framework whereas int is an alias for Int32 in C# language.Int32 x = 5;int x = 5;So, in use both the above statements will hold a 32bit integer. They compile to the same code, so at execution time there is no difference whatsoever.The only minor difference is Int32 can be only used with System namespace. While validating the type of a value like mentioned above we can use Int32 or int.typeof(int) == typeof(Int32) == typeof(System.Int32)ExampleThe below example shows how an integer is declared using System.Int32.using System; namespace DemoApplication{    class Program{       ...

Read More

What does the two question marks together (??) mean in C#?

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

It is the null-coalescing operator. The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn't evaluate its right-hand operand if the lefthand operand evaluates to non-null.A nullable type can represent a value that can be undefined or from the type's domain. We can use the ?? operator to return an appropriate value when the left operand has a nullable type. If we try to assign a nullable value type to a non-nullable value type without using the ?? operator, we will get ...

Read More

What is @ in front of a string in C#?

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

It marks the string as a verbatim string literal.In C#, a verbatim string is created using a special symbol @. @ is known as a verbatim identifier. If a string contains @ as a prefix followed by double quotes, then compiler identifies that string as a verbatim string and compile that string. The main advantage of @ symbol is to tell the string constructor to ignore escape characters and line breaks.Exampleusing System; using System.IO; namespace DemoApplication{    class Program{       static void Main(string[] args){          Console.WriteLine("test string test string");          Console.WriteLine(@"test string ...

Read More
Showing 41–50 of 1,951 articles
« Prev 1 3 4 5 6 7 196 Next »
Advertisements