Results 1 to 6 of 6

Thread: Assigning null value to value type in c#: Is it possible?

  1. #1
    Join Date
    Nov 2009
    Posts
    877

    Assigning null value to value type in c#: Is it possible?

    Hi, can we assign null value to value type in c#? I want to know this. I am beginner in c#. And getting error while doing this. I want to know that this is possible or not. If not then I can try for some other trick, so please reply me as soon as possible.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Assigning null value to value type in c#: Is it possible?

    HI, which version of .net are you using to develop your code? If you are using version which is below .NET 2.0, then you will get error while assigning null value to value type in c#. After .Net 2.0 is is possible to assign null value to value type. You need to use following code to do this

    Code:
    System.Nullable<int> Num = new System.Nullable<int>;
    Try this, This would work. I had used this many time.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Assigning null value to value type in c#: Is it possible?

    Hi, you want to assign null value to value type in c#. Then it is possible. Why are you getting the error while doing this. We can do this. I have done it many times. Just use the following code and you will able to do it.

    Use the following code to assign null value to value type in C#:

    Code:
    int? var_Number = null;

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Assigning null value to value type in c#: Is it possible?

    Hello, I don't have that much idea about c#. But, yes you can do this. I think in c#2.0 one new type was introduced which is named as nullable type. By the help of which we can assign null value to the value type, as follows:
    Code:
    bool?flag=null;
    But, first you need to check the flag value as follows:

    Code:
    if(!flag.HasValue)
    {
       flag=true;
    }
    if((bool)flag)
    {
      /*code*/
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Assigning null value to value type in c#: Is it possible?

    Hi, I don't think that this would be possible. I have not seen anything which will able to assign the null value to value type with the help of C#. I have only seen follwoing types:

    Value types:
    numeric data types,
    Boolean, Char, and Date
    All structures, Enumerations,
    Byte, Short, Integer, or Long

    Reference types:
    String,
    All arrays,
    Class types,
    Delegates

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Assigning null value to value type in c#: Is it possible?

    Hi, I dont think that you can assign null value to value type with the help of c#. Don't try to waste time by doing it. I don't have any knowledge about it. I have read many books but not able to read it yet. I don't think this would be possible.

Similar Threads

  1. Replies: 1
    Last Post: 16-08-2010, 05:25 PM
  2. Replies: 1
    Last Post: 16-08-2010, 04:44 PM
  3. Assigning a reference to an object
    By Gillian Anderson in forum Software Development
    Replies: 5
    Last Post: 26-03-2010, 02:05 PM
  4. Assigning different application to CPU
    By Elieis in forum Windows Software
    Replies: 5
    Last Post: 12-02-2010, 01:52 PM
  5. Why isn't router assigning ip address
    By CHARO in forum Networking & Security
    Replies: 5
    Last Post: 29-07-2009, 02:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,180,457.42338 seconds with 16 queries