I have gotten used to using size_t in my C++ code by way of C due to a professor's insistence and I was curious if 'var' in C# is the same sort of thing?
There is a vast difference between the two.
size_t is used to represents the size of an object, return by sizeof. It is not a generic way to declare variables.
var is an implicit way to declare variables.
They have nothing to do with each other!
var
is used for automatic type inference (the compiler figures out the type of the variable based on the initializer's return type (like auto
in C++1x)).
size_t
is just an unsigned integer type in C.
It's not. I don't know about c++ size_t, but I do know that var in C# is exactly the same as declare the variable explicitly, and there is nothing to do with it a runtime.
No, var
and size_t
are not the same.
They serve a similar function: they both declare the type of a local variable as part of a statement. But that's where their similarities end.
The variable that both var
and size_t
declare has a specific type: size_t
has the type size_t
, which is usually typedef
ed as an unsigned int
. var
declares that the type must be inferred by the compiler from the rest of the statement; it could be an int
, could be a string
, could be a List<MyClass>
... the compiler figures that out by analyzing the rest of the statement.
C# var
keyword is most analogous to C++0x 'auto' keyword which is to do with type inference, where the storage type is inferred from the expression, rather than a platform independant way to specify size of the object.
There is no direct equivalent for this in C.
If you are asking whether the use of var
is like size_t
in that you don't actually need to use size_t
, you can get away with an int
, then yes, they are kind of similar in that respect.
However, they differ in that size_t
is about making your code more explicit, whilst var
is about removing some redundant explicitness for the benefit of readability.
So the readability of the following code can be improved with var
:
Dictionary<string, Dictionary<int, char>> mapping = new Dictionary<string, Dictionary<int, char>>();
-- vs --
var mapping = new Dictionary<string, Dictionary<int, char>>();
However, excess use of var
can harm readability, like in var f = Foo();
.
Unlike size_t
, there is absolutely no difference in the code that gets compiled with var
as opposed to the explicit type.
With size_t
, one of the benefits is portability. Object sizes might not be an int
on all platforms, but size_t
will always be large enough to hold them. This means not using size_t
could mean your code doesn't compile correctly on some platform.
However, with var
the code that gets compiled at the end of it is exactly the same. You can use var
everywhere you possibly can, or you could never use var
(with the exception of anonymous types) and it would make no difference.
So whilst it is recommended you always use size_t
where possible in C, var
is more of a judgement call. It's what you think makes the code more readable.