I am working with an existing C# codebase that is initializing a lot of objects using what seems to be a generic new()
command that doesn't include the object type. I am coming from Java to C#, and I have always used the = new Object()
approach.
Here are the two different approaches for creating objects in the code. Both appear to be used inconsistently through the codebase.
var object1 = new Object();
Object object2 = new();
I have read up creating anonymous objects in C# using the syntax new{ }
but I can't find much on just using new with parens for initialization.
All the code is doing at this point is initializing new instances of these objects for later use. Following the debugger through, it appears as if both approaches result in identical objects. However, is it truly the case that both approaches are equivalent? Is there any reason why I should be using one of these approaches versus the other, either technically or as a best practice professionally?
CodePudding user response:
The end result is absolutely the same IL code. So if we are talking about any performance difference - there is no difference. It's just a "syntax sugar" to let us write code more conveniently.
As for what is worth to use, first of all, use that way which is already used in your project. Don't mix up different styles. If you've just started your project then as for me it's better to use the full name of a type, it's easier to read and understand. But it's on you, you can google any C# Coding Conventions and follow it throughout the project.
CodePudding user response:
var
is kind of a placeholder. If the object-type is known to the compiler you may use var as a placeholder. I personally try to not use it too often as I find the coding to be less clear and debugging harder.
Example:
Dictionary<string, int> dict = new Dictionary<string, int>();
var dict = new Dictionary<string, int>();
similarly (beginning from C# 9.0), the object type may be omitted from object creation if the variable-type is known:
Dictionary<string, int> dict = new Dictionary<string, int>();
Dictionary<string, int> dict = new ();
To conclude:
In your case, where empty objects are newly created in variables, it does not make a difference. They both do exactly the same, they are just switched vice versa. It is merely a convenience feature. That beeing said, a convenience feature while writing code, but a pain when coming back to a project and trying to understand whats going on.
# all the same:
Dictionary<string, int> dict = new Dictionary<string, int>();
var dict = new Dictionary<string, int>();
Dictionary<string, int> dict = new ();
However, in code you would use var
for creating a new variable while using new()
for assigning a new empty object to a variable:
# creates variable Object[] and assigns a value
var inhabitants = allInhabitants.ToArray();
# assigns an empty object to variable
Object[] inhabitants = new();
More details here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/new-operator
CodePudding user response:
As it was told already it is C# 9's new feature. It is a matter of style to use or not to use it, code behind will be absolutely the same. But be carefull, I noticed a small syntax pitfall , but it also shows the difference between the anonymos types and this new c# feature
var list = new List<int> {1,2,3,4}; //OK
List<int> list = new List<int> {1,2,3,4}; //OK
List<int> list = new() {1,2,3,4}; //OK
List<int> list = new {1,2,3,4}; // Error, anonymos type !!!