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
7
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
    Sep 11 at 7:58






  • 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






  • 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















up vote
7
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
    Sep 11 at 7:58






  • 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






  • 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













up vote
7
down vote

favorite
1









up vote
7
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 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 wrote int a = 99494; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    Sep 11 at 8:02






  • 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













  • 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; since 99494 is of type int.
    – Lasse Vågsæther Karlsen
    Sep 11 at 8:02






  • 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








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













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
      Sep 11 at 8:00







    • 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






    • 1




      well explained answer!
      – Dr. Snail
      Sep 11 at 8:05






    • 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







    • 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










    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 Sep 11 at 7:56









        Patrick Hofman

        122k18160210




        122k18160210






















            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
              Sep 11 at 8:00







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              Sep 11 at 8:05






            • 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







            • 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














            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
              Sep 11 at 8:00







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              Sep 11 at 8:05






            • 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







            • 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












            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 Sep 11 at 8:03

























            answered Sep 11 at 8:00









            Prodigle

            613212




            613212







            • 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







            • 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






            • 1




              well explained answer!
              – Dr. Snail
              Sep 11 at 8:05






            • 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







            • 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




              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




              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




              well explained answer!
              – Dr. Snail
              Sep 11 at 8:05






            • 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







            • 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

















             

            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay