What is the use of default keyword?

 Posted by Ddd on 3/20/2011 | Category: C# Interview questions | Views: 3392 | Points: 40
Answer:

It is used in 2 contexts.

1)With the switch statement.
example:

Console.WriteLine("enter number");
int a=Int32.Parse(Console.ReadLine());
switch(a)
{
case 100:
Console.WriteLine("correct");
break;
default:
Console.WriteLine("100 is right value");
break;
}


2) Provide default values for value types (which is 0) and null value as the default value

for the reference types:

example: int a=default(Int32);
default for a will be 0.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response