Introduction:
The keyword default plays several and important roles in the C# language, therefore, I will enumerate some cases where the default keyword is used.
First use case:
The keyword default is used within the switch ... case bloc, to illustrate how, I propose this example:
Color x = new Color();
switch (x)
{
case Color.Red :
MessageBox.Show("This is not a primary color");
//TO DO: Perform some task
break;
case Color.Green :
MessageBox.Show("This is not a primary color");
//TO DO: Perform some task
break;
case Color.Blue:
MessageBox.Show("This is not a primary color");
//TO DO: Perform some task
break;
default:
MessageBox.Show("This is not a primary color");
}
As you see the default play the role of somewhat the exception catcher or the else keyword within a given condition block, but you tell me OK I know this. Have you any thing new where using the keyword default is necessary. YES of course, here are two other use cases.
Second use case:
Imagine if it is necessary to reset the value of a given generic type, you can employ this code for example:
using System;
using System.Collections.Generic;
using System.Text;
public class myType<T>
{
public myType() { }
private T _Attribute1;
private T _Attribute2;
public T Attribute1

Garen YöndemPosted Mar 16, 2016, 6:41 PM
Didn't think of the third case before. Thanks.
Rajneesh RaiPosted Apr 14, 2014, 5:15 AM
Nice Explanation !! In my opinion there are only two uses of default, in switch case statements and getting default value of a Type.
TarikPosted Jan 5, 2010, 9:51 PM
I was wondering how default keyword works in different context and this article shows that clearly. Some times MSDN really bothers me with its insufficient description of code.
Umut bayrakPosted Oct 21, 2008, 2:56 AM
Thanks i never thought default keyword work like this, this topic really helpful