5

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?

2

6 Answers 6

11

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';
Sign up to request clarification or add additional context in comments.

4 Comments

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."
Fair enough - using \0 as null is a holdover from my C++ days :)
Nothing wrong with that... They are the same in this case, after all.
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!
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.

Comments

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)

Comments

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

2 Comments

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

you can set test to null

test = null;

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.