How to determine the size of var?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












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 ?










share|improve this question



















  • 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; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    yesterday






  • 1




    int size = sizeof(a); since 99494 is of type int (Int32) it takes 32 bits = 4 bytes
    – Dmitry Bychenko
    yesterday















up vote
6
down vote

favorite
1












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 ?










share|improve this question



















  • 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; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    yesterday






  • 1




    int size = sizeof(a); since 99494 is of type int (Int32) it takes 32 bits = 4 bytes
    – Dmitry Bychenko
    yesterday













up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





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 ?










share|improve this question















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#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 wrote int a = 99494; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    yesterday






  • 1




    int size = sizeof(a); since 99494 is of type int (Int32) it takes 32 bits = 4 bytes
    – Dmitry Bychenko
    yesterday













  • 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; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    yesterday






  • 1




    int size = sizeof(a); since 99494 is of type int (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













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.)






share|improve this answer



























    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






    share|improve this answer


















    • 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







    • 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






    • 1




      well explained answer!
      – Dr. Snail
      yesterday






    • 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







    • 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










    Your Answer





    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.)






    share|improve this answer
























      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.)






      share|improve this answer






















        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.)






        share|improve this answer













        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.)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Patrick Hofman

        122k18159209




        122k18159209






















            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






            share|improve this answer


















            • 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







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              yesterday






            • 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







            • 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














            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






            share|improve this answer


















            • 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







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              yesterday






            • 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







            • 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












            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






            share|improve this answer














            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            Prodigle

            623212




            623212







            • 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







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              yesterday






            • 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







            • 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




              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




              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




              well explained answer!
              – Dr. Snail
              yesterday






            • 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







            • 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

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke