Assigning null value to float or int type variable in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • punitshrivastava
    New Member
    • Jul 2007
    • 22

    Assigning null value to float or int type variable in C#

    Hi to All,
    I am Punit Shrivastava .I am working on Asp.net with C#.i am new in C#.I want to assign null or blank to a variable which is of float or int type.Please suggest me what i do.
    Thanks & Regards.
    Punit Shrivastava.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by punitshrivastav a
    Hi to All,
    I am Punit Shrivastava .I am working on Asp.net with C#.i am new in C#.I want to assign null or blank to a variable which is of float or int type.Please suggest me what i do.
    Thanks & Regards.
    Punit Shrivastava.
    Is the variable a nullable type?

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      You cannot specifically asign a valuetype a null value.

      Code:
      int a;
      is as close as you can get to a null value for it.

      You can use a predefined value:
      Code:
      int a=int.MinValue;
      Or just start with a zero:
      Code:
      int a=0;

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Plater
        You cannot specifically asign a valuetype a null value.
        Unless if the value type is a nullable type.
        Originally posted by C# language specification
        For each non-nullable value type T there is a corresponding nullable type T?, which can hold an additional value null. For instance, int? is a type that can hold any 32 bit integer or the value null.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by r035198x
          Unless if the value type is a nullable type.
          ValueTypes aren't nullable, the ValueType? is no longer a ValueType but the other type (can't think of the name, it's what classes are. ObjectType?).
          I think that is what the difference between say "double" and "Double" is?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Plater
            ValueTypes aren't nullable, the ValueType? is no longer a ValueType but the other type (can't think of the name, it's what classes are. ObjectType?).
            I think that is what the difference between say "double" and "Double" is?
            Nullable types are value types.
            There are four types of value types in C# (Simple types, Enum types, Struct types and Nullable types).
            Remember everything is an object. There was no need for making nullable types reference types since their size is always known before hand anyway.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Perchance I have just been schooled.

              Comment

              Working...