locked
What is the meaning of "Guid? " type? RRS feed

  • Question

  • Hi to all,

    I saw "strange" variable declaration like:

    Guid? someVarName

    and I was wondering what kind of type is "Guid?".

    Monday, December 15, 2008 11:04 AM

Answers

  • A question mark means it's nullable, that is, can have a null value.
    For example, string can be null, but int and Guid cannot as they are structs. Add a question mark and they can have null values.

    int? i1 = null;
    int i2 = 0; //null would be illegal here

    Nullability becomes handy when there is no default value you could accept. For example, if 0 is a perfectly legal value for your integer, you want something that is obviously not legal. Make that null and you know this variable has not been initialized.

    One more sample of this is DateTime. It cannot hold a null value. If using DateTime.MinValue or DateTime.MaxValue is not good, make it DateTime? and store null in it.


    Please remember to mark the replies as answers if they answered your question :)
    • Marked as answer by matixsc Monday, December 15, 2008 12:33 PM
    Monday, December 15, 2008 11:11 AM
  • ? is use to assign null for premitive datatypes like int, string, float, guid etc.

    In general if you use int i = null will give you compilation errro as value type cant assign null vlaue. if you add ? then its now allow you to assign null.

    And if you are using Guid; ita also allow us to set it blank or emplty by assing it with Guid.Empty.
    • Marked as answer by matixsc Monday, December 15, 2008 12:33 PM
    Monday, December 15, 2008 11:23 AM

All replies

  • A question mark means it's nullable, that is, can have a null value.
    For example, string can be null, but int and Guid cannot as they are structs. Add a question mark and they can have null values.

    int? i1 = null;
    int i2 = 0; //null would be illegal here

    Nullability becomes handy when there is no default value you could accept. For example, if 0 is a perfectly legal value for your integer, you want something that is obviously not legal. Make that null and you know this variable has not been initialized.

    One more sample of this is DateTime. It cannot hold a null value. If using DateTime.MinValue or DateTime.MaxValue is not good, make it DateTime? and store null in it.


    Please remember to mark the replies as answers if they answered your question :)
    • Marked as answer by matixsc Monday, December 15, 2008 12:33 PM
    Monday, December 15, 2008 11:11 AM
  • ? is use to assign null for premitive datatypes like int, string, float, guid etc.

    In general if you use int i = null will give you compilation errro as value type cant assign null vlaue. if you add ? then its now allow you to assign null.

    And if you are using Guid; ita also allow us to set it blank or emplty by assing it with Guid.Empty.
    • Marked as answer by matixsc Monday, December 15, 2008 12:33 PM
    Monday, December 15, 2008 11:23 AM