how to set a value of char array to null?

25,953

Solution 1

Use a nullable char:

char?[] test = new char?[3] {a,b,c};
test[2] = null;

The drawback is you have to check for a value each time you access the array:

char c = test[1];  // illegal

if(test[1].HasValue)
{
    char c = test[1].Value;
}

or you could use a "magic" char value to represent null, like \0:

char[] test = new char[3] {a,b,c};
test[2] = '\0';

Solution 2

You can't do it because, as the error says, char is a value type.

You could do this:

char?[] test = new char?[3]{a,b,c};
test[2] = null;

because you are now using the nullable char.

If you don't want to use a nullable type, you will have to decide on some value to represent an empty cell in your array.

Solution 3

As the error states, char is non-nullable. Try using default instead:

test[2] = default(char);

Note that this is essentially a null byte '\0'. This does not give you a null value for the index. If you truly need to consider a null scenario, the other answers here would work best (using a nullable type).

Solution 4

You could do:

test[2] = Char.MinValue;

If you had tests to see if a value was "null" somewhere in your code, you'd do it like this:

if (test[someArrayIndex] == Char.MinValue)
{
   // Do stuff.
}

Also, Char.MinValue == default(char)

Solution 5

you can set test to null

test = null;

but not test[2] because it is char - hence value type

Share:
25,953
Hendra Anggrian
Author by

Hendra Anggrian

Open source enthusiast in awe of Kotlin technologies. Spent most times following new stuff and best practices in GitHub.

Updated on July 09, 2022

Comments

  • Hendra Anggrian
    Hendra Anggrian almost 2 years

    for example when I wrote:

    Char[] test = new Char[3] {a,b,c};
    test[2] = null;
    

    it says Cannot convert null to 'char' because it is a non-nullable value type

    if I need to empty that array of char, is there a solution?

  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 11 years
    default(char) gives the null character. Why not make that explicit and say test[2] = '\0';? I would recommend that (if you want to use this character as a kind of "magic value").
  • jheddings
    jheddings over 11 years
    Just a quick suggestion might be to use default(char) for the "magic" value. IMHO, it provides a similar mental model for considering the value "uninitialized" or "unspecified."
  • jheddings
    jheddings over 11 years
    Only for consistency... I use default in many cases where the type is not know up front (i.e. generics or reflection). It helps me keep a consistent mental model that default => "unspecified" or "uninitialized."
  • D Stanley
    D Stanley over 11 years
    Fair enough - using \0 as null is a holdover from my C++ days :)
  • jheddings
    jheddings over 11 years
    Nothing wrong with that... They are the same in this case, after all.
  • Hendra Anggrian
    Hendra Anggrian over 11 years
    that '\0' works perfectly. I'm sorry I have to ignore the first 2 packs of codes above because I'm searching for the simplest one. Thanks mate!