While learning C#, this question came to my mind. What is the difference between void
and var
?
Here are the two example I would like to share:
void * voidInt = (void *) 7;
void * voidChar = (void *) 'F';
void * voidCharArray = (void *) "AbcString";
And this is example of var
:
var varInt = 7;
var varChar = 'F';
var varCharArray = "AbcString";
void
an anonymous datatype? var
and void
?Can someone help me clear this situation?
The other answers here are pretty good but I think they do not get clearly to the fundamentals. It is the fundamentals you are confused about, so let's address those.
So voidInt
, voidChar
, voidCharArray
, varInt
, varChar
and varCharArray
are all variables, and they all have types associated with them. Each variable can be assigned a value of that type or produce a value of that type, depending on whether the variable is being written to or read from.
OK, so now what are pointers?
void *
type is a special pointer type.T*
may be dereferenced to produce a variable of type T
. T*
must not be void*
.void*
. void*
value may be explicitly converted to any pointer type value. And what is var
in C#?
var
is a "syntactic sugar" that tells the compiler to deduce the type of the variable from the initialzier rather than requiring that it be written out.And what are "anonymous types" in C#?
So now we can look at your program and see what each line does.
void * voidInt = (void *) 7;
voidInt
is a variable of type void*
. The value assigned to it is the conversion of the integer 7 to a pointer, which is almost certainly a garbage pointer on any modern operating system. This code is essentially nonsensical.
More sensible code would be:
int myInt = 7;
int* intPtr = &myInt;
void* voidInt = intPtr;
This means that myInt
is a variable which holds the value 7
, intPtr
is a variable which holds a pointer; when that pointer is dereferenced it produces variable myInt
. voidInt
is a variable which holds any pointer, and the value read from intPtr
is a pointer. So now voidInt
and intPtr
both hold a pointer to variable myInt
.
void * voidChar = (void *) 'F';
Same thing here. The character F
is treated as a number and converted to a pointer value, which is stored in the variable. This is not sensible. Sensible code would be something like:
char myChar = 'F';
void *voidChar = &myChar;
But this makes perfect sense:
void * voidCharArray = (void *) "AbcString";
A string literal in C++ is convertible to a char*
which is a pointer to the storage for the first character, and that pointer is convertible to void*
.
What about this?
var varInt = 7;
var varChar = 'F';
var varCharArray = "AbcString";
This is just a pleasant way to write
int varInt = 7;
char varChar = 'F';
string varCharArray = "AbcString";
Each variable has its given type, and each assignment stores a value of that type in the variable.
What about anonymous types?
var anon = new { X = 123, Y = 456 };
This makes a variable of anonymous type, where the anonymous type has two properties X and Y both of type int
. The type has no name, so there is no way to write out the type in the declaration, hence var
must be used.
The key thing here is to make sure that you have a grasp of the fundamentals: pointers are values, they may be dereferenced, and doing so produces a variable. Since pointers are values they may themselves be stored in variables of pointer type. This has almost nothing to do with var
, which is a pleasant way in C# to make the compiler do the work of figuring out what type a variable should have.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With