How to determine the size of var?
Clash Royale CLAN TAG#URR8PPP
up vote
7
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
7
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
Sep 11 at 7:58
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
Sep 11 at 8:02
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
Sep 11 at 8:11
add a comment |Â
up vote
7
down vote
favorite
up vote
7
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 Sep 11 at 7:59
Patrick Hofman
122k18160210
122k18160210
asked Sep 11 at 7:55
sajis997
28318
28318
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
Sep 11 at 7:58
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
Sep 11 at 8:02
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
Sep 11 at 8:11
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
Sep 11 at 7:58
1
The actual code will be compiled as though you wroteint a = 99494;
since99494
is of typeint
.
â Lasse VÃ¥gsæther Karlsen
Sep 11 at 8:02
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytes
â Dmitry Bychenko
Sep 11 at 8:11
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
Sep 11 at 7:58
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
Sep 11 at 7:58
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
Sep 11 at 8:02
The actual code will be compiled as though you wrote
int a = 99494;
since 99494
is of type int
.â Lasse VÃ¥gsæther Karlsen
Sep 11 at 8:02
1
1
int size = sizeof(a);
since 99494
is of type int
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
Sep 11 at 8:11
int size = sizeof(a);
since 99494
is of type int
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
Sep 11 at 8:11
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
Sep 11 at 8:00
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
Sep 11 at 8:03
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:13
 |Â
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 Sep 11 at 7:56
Patrick Hofman
122k18160210
122k18160210
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
Sep 11 at 8:00
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
Sep 11 at 8:03
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:13
 |Â
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
Sep 11 at 8:00
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
Sep 11 at 8:03
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:13
 |Â
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 Sep 11 at 8:03
answered Sep 11 at 8:00
Prodigle
613212
613212
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
Sep 11 at 8:00
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
Sep 11 at 8:03
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:13
 |Â
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
Sep 11 at 8:00
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
Sep 11 at 8:03
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:13
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
Sep 11 at 8:00
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
Sep 11 at 8:00
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
Sep 11 at 8:03
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
Sep 11 at 8:03
1
1
well explained answer!
â Dr. Snail
Sep 11 at 8:05
well explained answer!
â Dr. Snail
Sep 11 at 8:05
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
Sep 11 at 8:11
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
Sep 11 at 8:11
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
Sep 11 at 8:13
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
Sep 11 at 8:13
 |Â
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
Sep 11 at 7:58
1
The actual code will be compiled as though you wrote
int a = 99494;
since99494
is of typeint
.â Lasse VÃ¥gsæther Karlsen
Sep 11 at 8:02
1
int size = sizeof(a);
since99494
is of typeint
(Int32
) it takes 32 bits = 4 bytesâ Dmitry Bychenko
Sep 11 at 8:11