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;
}
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
add a comment |
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
add a comment |
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
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
c# class dictionary .net-core
edited Nov 25 '18 at 0:32
Kyle
4,43122035
4,43122035
asked Nov 25 '18 at 0:06
Hristo DishkovHristo Dishkov
61
61
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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 whyGC.KeepAlive(object)
exists.
– Scott Chamberlain
Nov 25 '18 at 0:39
add a comment |
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
});
}
});
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
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 25 '18 at 0:25
ChrisChris
31219
31219
add a comment |
add a comment |
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.
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 whyGC.KeepAlive(object)
exists.
– Scott Chamberlain
Nov 25 '18 at 0:39
add a comment |
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.
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 whyGC.KeepAlive(object)
exists.
– Scott Chamberlain
Nov 25 '18 at 0:39
add a comment |
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.
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.
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 whyGC.KeepAlive(object)
exists.
– Scott Chamberlain
Nov 25 '18 at 0:39
add a comment |
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 whyGC.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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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