Default operator in C#


Using the default, you can get the default value of every reference and value type. The expressions set as default are evaluated at compile-time.

To get the default for int −

default(int);

To get the default for long −

default(long)

Let us see the code to display default values −

Example

 Live Demo

using System;

public class Demo {
   public static void Main() {
      int val1 = default(int);
      long val2 = default(long);
      bool val3 = default(bool);

      // default for int
      Console.WriteLine(val1);

      // default for long
      Console.WriteLine(val2);

      // default for bol
      Console.WriteLine(val3);
   }
}

Output

0
0
False

Updated on: 22-Jun-2020

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements