top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

? (question mark) after a type name in C#?

+3 votes
835 views

What does ? (question mark) after a type name mean in C#?

posted Mar 25, 2014 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

This is a short hand for System.Nullable
The question mark states that it is a nullable type.
this syntax will allows you to store a null value for value types (which would otherwise not support null values.

Example:
private char? getLetter() {
if(something) {
return 'a';
else
return null;
}

char? letter = getLetter();
if(letter.HasValue) {
//do something with it
Debug.WriteLine(letter.Value);
}
else
//it's null!

answer Mar 26, 2014 by Atul Mishra
Similar Questions
+1 vote
using System;

namespace CareerRideTest
{    
  class A
  {    
    public A()
    {
      Console.WriteLine("I am in A");
    }
  }

  class B : A
  {
    public B()
    {
      Console.WriteLine("I am in B");
    }
  }

  class C : B
  {
    static C()
    {
      Console.WriteLine("I am in Static C");
    }
    public C()
    {
      Console.WriteLine("I am in C");
    }
  }    

  class MainClass
  {
    static void Main(string[] args)
    {
      C obj = new C();    
      Console.ReadKey();    
    }
  }    
}
+3 votes

How to Sign an Assembly with a Strong Name?

...