How to determine the size of var?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I Have a variable declared as follows:
var a = 99494;
Then I used the following to determine the size of the variable in bytes:
Marshal.SizeOf(a)
Does it get the actual size of memory occupied by this value ?
c#
add a comment |Â
up vote
6
down vote
favorite
I Have a variable declared as follows:
var a = 99494;
Then I used the following to determine the size of the variable in bytes:
Marshal.SizeOf(a)
Does it get the actual size of memory occupied by this value ?
c#
5
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".
â Damien_The_Unbeliever
yesterday
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
yesterday
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I Have a variable declared as follows:
var a = 99494;
Then I used the following to determine the size of the variable in bytes:
Marshal.SizeOf(a)
Does it get the actual size of memory occupied by this value ?
c#
I Have a variable declared as follows:
var a = 99494;
Then I used the following to determine the size of the variable in bytes:
Marshal.SizeOf(a)
Does it get the actual size of memory occupied by this value ?
c#
c#
edited yesterday
Patrick Hofman
122k18159209
122k18159209
asked yesterday
sajis997
27818
27818
5
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".
â Damien_The_Unbeliever
yesterday
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
yesterday
add a comment |Â
5
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".
â Damien_The_Unbeliever
yesterday
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
yesterday
5
5
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".â Damien_The_Unbeliever
yesterday
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".â Damien_The_Unbeliever
yesterday
1
1
The actual code will be compiled as though you wrote
int a = 99494;
since 99494
is of type int
.â Lasse VÃ¥gsæther Karlsen
yesterday
The actual code will be compiled as though you wrote
int a = 99494;
since 99494
is of type int
.â Lasse VÃ¥gsæther Karlsen
yesterday
1
1
int size = sizeof(a);
since 99494
is of type int
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
yesterday
int size = sizeof(a);
since 99494
is of type int
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
yesterday
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
10
down vote
Does it get the actual size of memory occupied by this value ?
Yes. In this case it is fairly simple, since the var
is an int
. It will always yield the same value (4). (var
isn't a dynamic type, it is determined on compile time.)
add a comment |Â
up vote
1
down vote
Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass()
. It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes
Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int
2
Why shouldn't you usevar
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.
â Patrick Hofman
yesterday
2
There is no point in discussing whether to usevar
or not, there are few cases where you must usevar
, other than that the usage is just an opinion.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
well explained answer!
â Dr. Snail
yesterday
2
I would arguevar
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at best
â Vladi Pavelka
yesterday
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
 |Â
show 1 more comment
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
10
down vote
Does it get the actual size of memory occupied by this value ?
Yes. In this case it is fairly simple, since the var
is an int
. It will always yield the same value (4). (var
isn't a dynamic type, it is determined on compile time.)
add a comment |Â
up vote
10
down vote
Does it get the actual size of memory occupied by this value ?
Yes. In this case it is fairly simple, since the var
is an int
. It will always yield the same value (4). (var
isn't a dynamic type, it is determined on compile time.)
add a comment |Â
up vote
10
down vote
up vote
10
down vote
Does it get the actual size of memory occupied by this value ?
Yes. In this case it is fairly simple, since the var
is an int
. It will always yield the same value (4). (var
isn't a dynamic type, it is determined on compile time.)
Does it get the actual size of memory occupied by this value ?
Yes. In this case it is fairly simple, since the var
is an int
. It will always yield the same value (4). (var
isn't a dynamic type, it is determined on compile time.)
answered yesterday
Patrick Hofman
122k18159209
122k18159209
add a comment |Â
add a comment |Â
up vote
1
down vote
Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass()
. It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes
Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int
2
Why shouldn't you usevar
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.
â Patrick Hofman
yesterday
2
There is no point in discussing whether to usevar
or not, there are few cases where you must usevar
, other than that the usage is just an opinion.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
well explained answer!
â Dr. Snail
yesterday
2
I would arguevar
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at best
â Vladi Pavelka
yesterday
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
 |Â
show 1 more comment
up vote
1
down vote
Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass()
. It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes
Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int
2
Why shouldn't you usevar
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.
â Patrick Hofman
yesterday
2
There is no point in discussing whether to usevar
or not, there are few cases where you must usevar
, other than that the usage is just an opinion.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
well explained answer!
â Dr. Snail
yesterday
2
I would arguevar
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at best
â Vladi Pavelka
yesterday
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass()
. It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes
Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int
Yes, var is just a compile time trick so the compiler will decide it's type at compile time. I wouldn't recommend using it besides obvious and very long class names. e.g var x = new BigBigBigClass()
. It makes your code less readable to others and makes it easier to make mistakes. C# is a statically typed language and while it takes a few more seconds to declare types, it pays off when you aren't accidentally mixing classes
Since the compiler will put x as an int, it should return 32 bits (4 bytes) as that's the size of a standard int
edited yesterday
answered yesterday
Prodigle
623212
623212
2
Why shouldn't you usevar
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.
â Patrick Hofman
yesterday
2
There is no point in discussing whether to usevar
or not, there are few cases where you must usevar
, other than that the usage is just an opinion.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
well explained answer!
â Dr. Snail
yesterday
2
I would arguevar
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at best
â Vladi Pavelka
yesterday
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
 |Â
show 1 more comment
2
Why shouldn't you usevar
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.
â Patrick Hofman
yesterday
2
There is no point in discussing whether to usevar
or not, there are few cases where you must usevar
, other than that the usage is just an opinion.
â Lasse VÃ¥gsæther Karlsen
yesterday
1
well explained answer!
â Dr. Snail
yesterday
2
I would arguevar
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at best
â Vladi Pavelka
yesterday
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
2
2
Why shouldn't you use
var
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.â Patrick Hofman
yesterday
Why shouldn't you use
var
? You didn't give any reason for your recommendation. I would remove that part from your answer since it doesn't have anything to do with the question asked.â Patrick Hofman
yesterday
2
2
There is no point in discussing whether to use
var
or not, there are few cases where you must use var
, other than that the usage is just an opinion.â Lasse VÃ¥gsæther Karlsen
yesterday
There is no point in discussing whether to use
var
or not, there are few cases where you must use var
, other than that the usage is just an opinion.â Lasse VÃ¥gsæther Karlsen
yesterday
1
1
well explained answer!
â Dr. Snail
yesterday
well explained answer!
â Dr. Snail
yesterday
2
2
I would argue
var
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at bestâ Vladi Pavelka
yesterday
I would argue
var
makes code more readable by driving you to come up with better naming. Also am not sure about the "easier to make mistakes" part, since compiler won't allow you, plus the VS code analysis would underline the "mistake" with red even before you hit build. As mentioned previously, it's just opinion based at bestâ Vladi Pavelka
yesterday
2
2
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
I disagree on the point about var - refactoring is significantly easier when you can switch a factory to return an interface instead of an implementation without having to go through everywhere that consumes it to update variable types. Life is easier for the developer, and type safety is preserved by the compiler - itâÂÂs win-win
â s3raph86
yesterday
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52271226%2fhow-to-determine-the-size-of-var%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
5
var
is just "I don't want to write the type of this variable. Compiler, please do that work and pretend I wrote the type instead".â Damien_The_Unbeliever
yesterday
1
The actual code will be compiled as though you wrote
int a = 99494;
since99494
is of typeint
.â Lasse VÃ¥gsæther Karlsen
yesterday
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
yesterday