5
Reply

int i=null is wrong? how it make corect in .net

sainath reddy

sainath reddy

14y
7.5k
0
Reply


    We can declare like this:
    int? i = null;




    Hi,

    primitive types can't be null. but to reduce the impedance missmatch between database and programing languages like C# MS has come up with the nullable types.

     

    Thanks,

    Chinna

     

    int? i=null; //nulable type

    Explaination:
    • Nullable types represent value-type variables that can be assigned the value of null.

    • The syntax T? is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.

    • Assign a value to a nullable type in the same way as for an ordinary value type,
       for example int? x = 10; or double? d = 4.108;

    • Use the System.Nullable.GetValueOrDefault property to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();

      For more information visit this link:
      http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx




    int is value type and therefore cannot be assigned a "null" value. You just have to
    declare the variable in the following form -

    int? i = null;

    Now this is valid.

    int is not an object, it is primitive type which i think can not be null. You can set 0 of negative number.

    I am not sure what you want to accomplish here. If you can give your motive then we can provide solution for that.