C# assigning null value

Deathjester

Limp Gawd
Joined
Dec 23, 2004
Messages
194
I apologize in advance for the dumb question... however I searched for about an hour and couldn't find anything (as simplistic as it is, I assume)

In C#, How do you assign null to the char value? I tried two apostrophes, but got the following error

Code:
grade2.cs(27,21): error CS1011: Empty character literal

I also tried just using null, even though i sorta knew that wouldnt work either. Any help?
 
Do you want the null character? That's '\0'. It's generally not too useful in managed code; are you sure you need it?
 
Deathjester said:
I apologize in advance for the dumb question... however I searched for about an hour and couldn't find anything (as simplistic as it is, I assume)

In C#, How do you assign null to the char value? I tried two apostrophes, but got the following error

Code:
grade2.cs(27,21): error CS1011: Empty character literal

I also tried just using null, even though i sorta knew that wouldnt work either. Any help?

You can't have an empty char '' (two apostrophes) you can have an empty string "" (double quotes).
 
mikeblas said:
Do you want the null character? That's '\0'. It's generally not too useful in managed code; are you sure you need it?


My program is really basic and calculates a students grades values and ultimately outputs a letter grade. I was trying to be as efficient as possible by using a char value instead of a string value for the final letter grade (not that this particular program takes alot of memory in the first place but I'm trying to get used to doing it that way). Or is the difference so miniscule I shouldn't bother?

In any case thanks alot for the info
 
Deathjester said:
My program is really basic and calculates a students grades values and ultimately outputs a letter grade. I was trying to be as efficient as possible by using a char value instead of a string value for the final letter grade (not that this particular program takes alot of memory in the first place but I'm trying to get used to doing it that way). Or is the difference so miniscule I shouldn't bother?

I wouldn't bother; you're using managed code, so your program has a huge working set even if you're only printing "Hello world".

You should use whatever datatype makes sense for you. Maybe that's an enum (since you have A, B, C, D, and not E, but F); or maybe you have A+, A, A-, B+, B, and so on. Or, maybe you just use a string. Or maybe you use an int, ant convert to the appropriate letter only when you do your outputting.

There's more than one way to skin the cat; the art is deciding which is most appropriate. But I don't think that worrying about data size will be one of them for you.
 
if you're on a computer with 256 megs of ram, the size of your data type isn't going to matter much unless you're grading thousands and thousands of papers.

for instance...

if you wanted to store a letter grade in a string, you'd need two bytes per student (assuming you want to save this somehow).. and to use a char, 1 byte. (assuming there are no +/-'s)

.....256 megs won't fill up very fast in either case.


..but to answer the original question, if c++ will let you do this, perhaps c# will as well..

char x;

x= (char) 0;

maybe that'll work?

edit: the proper c++ syntax is

char x;

x = ((char)NULL);

but i would just do

x = '\0';
 
Back
Top