Introduction:
As per MSDN default keyword in C# used to assign the default value of the type. In some situation its comes very handy where we don’t want to create object and directly assign the default value of it.The basic syntax of default is default(T) where t is any reference type of value type and If T is a value type, whether it will be a numeric value or struct.
Default Keyword usage:
We can use Default keyword in variety of cases.Assigning value to nullable Type:
using System; namespace ConsoleApplication3 { class Program { static void Main() { int? i = default(int); Console.WriteLine(i); } } }
Here output will be.
Default Keyword in switch case:
using System; namespace ConsoleApplication3 { class Program { static void Main() { IsMatch("Hello"); IsMatch("Jalpesh"); } static void IsMatch(string matchvalue) { switch (matchvalue) { case "Jalpesh": Console.WriteLine("Perfect Name"); break; default: Console.WriteLine("Not written proper name"); break; } } } }
Output of this code will be
There are many more example I can write!!. So it’s a really useful keyword. Hope you like it. Stay tuned for more.
0 comments:
Post a Comment
Your feedback is very important to me. Please provide your feedback via putting comments.