C# Object name concept explanation





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















With a dictionary with a nested class, for example: Dictionary<int, BankAccount>,
what's the difference between creating the class first as an object, then linking it to a new Dictionary, and creating the object directly into the Dictionary itself, for example:




  • dict.Add(1, new BankAccount());

  • var acc = new BankAccount();
    dict.Add(1, acc);


Is there any benefit of using one over another?










share|improve this question































    0















    With a dictionary with a nested class, for example: Dictionary<int, BankAccount>,
    what's the difference between creating the class first as an object, then linking it to a new Dictionary, and creating the object directly into the Dictionary itself, for example:




    • dict.Add(1, new BankAccount());

    • var acc = new BankAccount();
      dict.Add(1, acc);


    Is there any benefit of using one over another?










    share|improve this question



























      0












      0








      0








      With a dictionary with a nested class, for example: Dictionary<int, BankAccount>,
      what's the difference between creating the class first as an object, then linking it to a new Dictionary, and creating the object directly into the Dictionary itself, for example:




      • dict.Add(1, new BankAccount());

      • var acc = new BankAccount();
        dict.Add(1, acc);


      Is there any benefit of using one over another?










      share|improve this question
















      With a dictionary with a nested class, for example: Dictionary<int, BankAccount>,
      what's the difference between creating the class first as an object, then linking it to a new Dictionary, and creating the object directly into the Dictionary itself, for example:




      • dict.Add(1, new BankAccount());

      • var acc = new BankAccount();
        dict.Add(1, acc);


      Is there any benefit of using one over another?







      c# class dictionary .net-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 0:32









      Kyle

      4,43122035




      4,43122035










      asked Nov 25 '18 at 0:06









      Hristo DishkovHristo Dishkov

      61




      61
























          2 Answers
          2






          active

          oldest

          votes


















          1














          The advantage of creating the object first, and adding it by reference is, that you hold the reference in the current method, and thus have full access to it.



          If you create the object in line with the add method, you would have to fetch the object from the dictionary to gain access.



          I do not see any other differences.
          Creating the object first, could have code-maintainability benefits, when you find out later that the object needs to be modified.






          share|improve this answer































            0














            The only real difference I could imagine is if you use the first option, the garbage collector doesn't have to hold onto a variable reference and can release the memory sooner. Other than that, it is more concise to choose the first option. Functionally, your options accomplish the same task.






            share|improve this answer
























            • Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

              – Chris
              Nov 25 '18 at 0:20






            • 1





              Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

              – Scott Chamberlain
              Nov 25 '18 at 0:39














            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',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            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%2f53463525%2fc-sharp-object-name-concept-explanation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            The advantage of creating the object first, and adding it by reference is, that you hold the reference in the current method, and thus have full access to it.



            If you create the object in line with the add method, you would have to fetch the object from the dictionary to gain access.



            I do not see any other differences.
            Creating the object first, could have code-maintainability benefits, when you find out later that the object needs to be modified.






            share|improve this answer




























              1














              The advantage of creating the object first, and adding it by reference is, that you hold the reference in the current method, and thus have full access to it.



              If you create the object in line with the add method, you would have to fetch the object from the dictionary to gain access.



              I do not see any other differences.
              Creating the object first, could have code-maintainability benefits, when you find out later that the object needs to be modified.






              share|improve this answer


























                1












                1








                1







                The advantage of creating the object first, and adding it by reference is, that you hold the reference in the current method, and thus have full access to it.



                If you create the object in line with the add method, you would have to fetch the object from the dictionary to gain access.



                I do not see any other differences.
                Creating the object first, could have code-maintainability benefits, when you find out later that the object needs to be modified.






                share|improve this answer













                The advantage of creating the object first, and adding it by reference is, that you hold the reference in the current method, and thus have full access to it.



                If you create the object in line with the add method, you would have to fetch the object from the dictionary to gain access.



                I do not see any other differences.
                Creating the object first, could have code-maintainability benefits, when you find out later that the object needs to be modified.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 0:25









                ChrisChris

                31219




                31219

























                    0














                    The only real difference I could imagine is if you use the first option, the garbage collector doesn't have to hold onto a variable reference and can release the memory sooner. Other than that, it is more concise to choose the first option. Functionally, your options accomplish the same task.






                    share|improve this answer
























                    • Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                      – Chris
                      Nov 25 '18 at 0:20






                    • 1





                      Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                      – Scott Chamberlain
                      Nov 25 '18 at 0:39


















                    0














                    The only real difference I could imagine is if you use the first option, the garbage collector doesn't have to hold onto a variable reference and can release the memory sooner. Other than that, it is more concise to choose the first option. Functionally, your options accomplish the same task.






                    share|improve this answer
























                    • Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                      – Chris
                      Nov 25 '18 at 0:20






                    • 1





                      Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                      – Scott Chamberlain
                      Nov 25 '18 at 0:39
















                    0












                    0








                    0







                    The only real difference I could imagine is if you use the first option, the garbage collector doesn't have to hold onto a variable reference and can release the memory sooner. Other than that, it is more concise to choose the first option. Functionally, your options accomplish the same task.






                    share|improve this answer













                    The only real difference I could imagine is if you use the first option, the garbage collector doesn't have to hold onto a variable reference and can release the memory sooner. Other than that, it is more concise to choose the first option. Functionally, your options accomplish the same task.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 25 '18 at 0:08









                    ZacZac

                    1,37242053




                    1,37242053













                    • Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                      – Chris
                      Nov 25 '18 at 0:20






                    • 1





                      Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                      – Scott Chamberlain
                      Nov 25 '18 at 0:39





















                    • Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                      – Chris
                      Nov 25 '18 at 0:20






                    • 1





                      Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                      – Scott Chamberlain
                      Nov 25 '18 at 0:39



















                    Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                    – Chris
                    Nov 25 '18 at 0:20





                    Garbage Collection cannot clean up the memory as long as the object is in the dictionary.

                    – Chris
                    Nov 25 '18 at 0:20




                    1




                    1





                    Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                    – Scott Chamberlain
                    Nov 25 '18 at 0:39







                    Also, it does not matter if you have a object in a variable or not, the garbage collector can still collect it if the variable is no longer used in the method after that point. Because it can do this is why GC.KeepAlive(object) exists.

                    – Scott Chamberlain
                    Nov 25 '18 at 0:39




















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463525%2fc-sharp-object-name-concept-explanation%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini