Tag: C#

How to generate a random number in C# and .NET?

If you want to generate a random number in your .NET application using C#, you can use the random class to generate one. How to generate a random number in C# and .NET? Below is a sample code snippet on how to generate random numbers in C#. Try to keep the instance of the Random class and reuse it Incase you are generating more than…

@ Operator in C#

Although it is not the best practise , If you want use the reserve words and the variable name , you could use the @ prefixed before the variable name . Below is a sample sourcecode that demonstrates the use of @ symbol. static void Main() { int @if = 10; MessageBox.Show(@if.ToString()); }

How to Print Leading Zeroes for a Number in C# ?

The Leading Zeroes for a number can be printed using the format string “D” in .NET . Below is a sample code snippet that demonstrates printing of 3 leading zeroes for a number 5 digit number in C#. How to Print Leading Zeroes for a Number in C# ?

How to Combine URL in C#?

The .NET Framework includes the Path.Combine feature which can be used to combine the strings into a path. How about having the same functionality to combine URL? How to Combine URL in C#? This is where the Uri class comes handy. The Uri class includes the constructor which can be used to combine URL. For example, if I need to combine “http://www.abundantcode.com” and “blogs/article1.html”, I…

Character Escape Sequences in C#

Below are some list of the Character Escape Sequences available in C# \\ – Backslash \’ – Single quote \” – Double quote \b – Backspace \n – New line \r – Carriage return \t – Horizontal tab \f – Form feed \a – Alert \v – Vertical tab \0 – Null

How to check if the Character is a Letter in c# using Char.IsLetter ?

The Char.IsLetter function can be used to find if the character is a Letter (Unicode) . If the specified character is a Letter , then the function Char.IsLetter returns true else returns false. How to check if the Character is a Letter in c# using Char.IsLetter ? Below is a sample sourcecode demonstrated on how to use Char.IsLetter .

How to mark a class as Obsolete or Deprecated in C# ?

Sometimes , you may want to mark a class or method as deprecated so that the you dont want to use the class any more and also want to let know , other developers that the class is obsolete . You can do that by using the Obsolete attribute as shown below . The Obsolete attribute also includes 2nd parameter (boolean) . When this valus…

How to Decode HTML Characters in C# ?

The .NET Framework 4.0 and above provides the WebUtility.HtmlDecode class which lets the developers to convert the string which are encoded with HTML characters to plain string . How to Decode HTML Characters in C# ? Below is a sample code snippet that demonstrates how to decode HTML characters in C#.

C# Keywords

Below are list of few Keywords in C# abstract as base break case catch checked class const continue default delegate do double else enum event explicit extern finally fixed for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this…

What is the difference between struct and class in C# ?

Below are some differences between struct and class in C# classes are reference types where as struct are value types. Null value cannot be assigned to the Struct because it is a non-nullable value type . Whereas the object of a class can be assigned a null value. Classes support inheritance but the struct doesn’t. struct cannot have destructor but a class can have. struct…