3

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • 5
    see [Why is there no Char.Empty like String.Empty?](http://stackoverflow.com/q/3670505/944681) – Michal Klouda Oct 10 '12 at 19:52
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Oct 10 '12 at 20:23

6 Answers6

10

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';
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • 2
    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 Oct 10 '12 at 20:00
  • 1
    Fair enough - using `\0` as `null` is a holdover from my C++ days :) – D Stanley Oct 10 '12 at 20:04
  • Nothing wrong with that... They are the same in this case, after all. – jheddings Oct 10 '12 at 20:05
  • 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! – Hendra Anggrian Oct 10 '12 at 20:36
3

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.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
2

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).

jheddings
  • 26,717
  • 8
  • 52
  • 65
  • `default(char)` gives [the null character](http://en.wikipedia.org/wiki/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"). – Jeppe Stig Nielsen Oct 10 '12 at 19:58
  • 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." – jheddings Oct 10 '12 at 20:01
2

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)

Gromer
  • 9,861
  • 4
  • 34
  • 55
1

you can set test to null

test = null;

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

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
0

I don't know the reason for your question, but if instead you use List<>, you could say

List<char> test = new List<char> { a, b, c, };
test.RemoveAt(2);

This changes the length (Count) of the List<>.

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181