Create C# object using Object object = new(); versus var object = new Object(); - c#

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?

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.

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://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/new-operator

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 !!!

Related

What is the optimal data structure for storing objects with a string key and a bool auxiliary value?

I need a data structure like below, but I need to be able to change the bool value. Other two stay the as they were when they were initialized. What would you use for best performance?
Dictionary<string, (object, bool)> dic = new Dictionary<string, (object, bool)>();
I was thinking of hashtable. But hashtable is like a dictionary with key/value. The object and bool in my example are in concept not like a key/value, because other values of the external dictionary can have the same object (or better yet ... object type). I don't want to make someone looking at my code later on thinking that the object and bool are more related they really are.
EDIT: object in this example is just a place holder. In reality it's a complex object with other objects in it and so on. Procedure before this one makes a bunch of this objects and some of them are deepcopy of the others. They are passed to this procedure. All of the object are here named by some rules and stored in the dictionary. Names are obviously unique. Procedure that comes after will take this dictionary and set the bool value on and off based on the values in the objects themselves and on the values of other bools. Procedure will be recursive until some state is reached.
Number of objects (or dic. entries) is arbitrary but expected to be >100 && <500. Time complexity is O(n).
I am targeting .NET7 (standard).
but I need to be able to change the bool value.
You can just reassign value for the key:
var tuples = new Dictionary<string, (object Obj, bool Bool)>
{
{ "1", (new object(), true) }
};
tuples["1"] = (tuples["1"].Obj, false); // or tuples["1"] = (tuples["1"].Item1, false);
Or
if (tuples.TryGetValue("1", out var c))
{
tuples["1"] = (c.Obj, false);
}
Personally I would leave it at that, but for really high perf scenarios you can look into CollectionMarshall instead of second snippet:
ref var v = ref CollectionsMarshal.GetValueRefOrNullRef(tuples, "1");
if (!Unsafe.IsNullRef(ref v))
{
v.Bool = false;
}
A bit more info - here.
For the 'performance' aspect:
The .NET Dictionary uses hashes to look up the item you need, which is very fast (comparable to a HashTable). I don't expect much performance issues related to this, or at least nothing that can be improved on with other data structures.
Also, you shouldn't worry about performance unless you are doing things a million times in a row + it turns out (in practice) that something is taking a measurable amount of time.
For the 'changing a bool' aspect:
... that is quite a long story.
There are 2 tuple variants in .NET:
The value tuple, created by doing var x = (myObj, myBool), like you are doing.
The x is a struct, and therefore a Value Type. You can actually change x.Item1 or x.Item2 to a new value just fine.
However... if you put x into a Dictionary then you actually put a copy of x (with a copy of its values) into the dictionary, because that is the nature of value types.
When you retrieve it again from the Dictionary, yet another copy is made - which makes modifying the actual tuple inside the Dictionary impossible; any attempt to do so would only modify the last copy you got.
Side story: The .NET Compiler knows this, which is why its refuses to compile code like dic[yourKey].Item2 = newBool; because such code wouldn't do what you might hope it would do. You're basically telling the compiler to create a copy, modify the copy, and then... discard the copy. The compiler requries a variable to store the copy before the rest can even start, but we provided no variable.
The Tuple generic class, or rather a range of generic classes, an instance of which can be created using calls like var x = Tuple.Create(myObj, myBool). These classes however forbid that you change any of their properties, they are always readonly. Tuple class instances can be put in a Dictionary, but they will still be readonly.
So what options are there really to 'modify a value in a tuple' a Dictionary?
Keep using a value tuple, but accept that in order to "change" the tuple inside the Dictionary you'll have to make a new instance (either a copy, or from scratch), set it to the properties that you want, and put that instance (or actualy a copy...) into the dictionary:
// initialize it
var dict = new Dictionary<string, (object, bool)>();
var obj = new object();
dict["abc"] = (obj, true);
// change it
var tmpTuple = dict["abc"]; // get copy
tmpTuple.Item2 = false; // alter copy
dict["abc"] = tmpTuple; // store another copy
// or if you want to avoid the tmp variable
dict["abc"] = (dict["abc"].Item1, false)
Use a custom class instead of the value tuple or a Tuple class, and then put that into the Dictionary:
public class MyPair
{
public object O { get; set; }
public bool B { get; set; }
}
// initialize it
var dict = new Dictionary<string, MyPair>();
var obj = new object();
dict["abc"] = new MyPair { O = obj, B = true };
// change it
dict["abc"].B = false;
So both types of Tuples are OK for objects that you don't want to do a lot with. But both have certain limits in their usage, and sooner or later you may need to start using classes.

Dealing with complex objects

I'm relatively new to C# programming and have had my first encounter with objects. A method that I am using is returning an object and I am setting it to an object of my own as such,
object target = searchResponse.Result.Parameters.Values;
From here I'm attempting to extract the data from the object, but it seems to be a "complex" object (just came up with that terminology, it's probably wrong, but I don't know the correct term). According to the Visual Studio locals menu, the object's value is count = 2. But, 'inside' the object is the data that I want, as shown below:
How would I get to these pieces of data?
As #UnholySheep suggested use var wherever possible if you don't know the DataType beforehand.
But for instance as you have stored the data in target and as in the picture it's of type Dictionary, you can cast it
Dictionary<string, object> dict = target as Dictionary<string, object>;
Now you can access your data from dict
EDIT 1:
I assumed you may want to know how to access data from a Dictionary, so here is a short snippet:
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
//here _int_ will be the key, _string_ will be your data
myDictionary.Add(1, "abc"); //1 is key, abc is data
myDictionary.Add(2, "def");
myDictionary.Add(3, "ghk");
string myData = myDictionary[2]; //pass the value to be fetched
//myData = def

C# returning Dictionary references safely

I'm considering three approaches for returning references to internal Dictionary instances (C#) in regards to code safety and impact on the code readability/visually for a project I'm working on.
I've narrowed it down to the following three approaches, but am open to better suggestions. Currently I prefer #3 as the best balance of safety without extra boiler plate.
1) Use a second ReadOnlyDictionary instance to wrap internal Dictionary, only ever letting the ReadOnlyDictionary escape the class:
2) Return the Dictionary instance as an IReadOnlyDictionary, but recasting would allow it to be modified so not as safe as option #1 or #3.
3) Return Dictionary.ToImmutableDictionary() as a ImmutableDictionary when it escapes the containing class so that the returned object is an immutable view of the inner dictionary, although this will make a new copy for every call incurring a higher cost, that should be fine with small simple dictionaries (which mine are).
private readonly Dictionary<string, string> innerDictionary = new Dictionary<string, string>();
// Only required for Example #1
private readonly IReadOnlyDictionary<string, string> readonlyInnerDictionary;
public ExampleClass() {
// Only required for Example #1
readonlyInnerDictionary = new ReadOnlyDictionary<string, string>(innerDictionary);
}
public IReadOnlyDictionary<string, string> GetExampleOne() {
// Requires a second dictionary which is more boiler plate but the object being returned is truly readonly
return readonlyInnerDictionary;
}
public IReadOnlyDictionary<string, string> GetExampleTwo() {
// Requires InnerDictionary be defined as Dictionary (Not IDictionary) but doesn't require the second dictionary be defined
// which is less boiler plate, but the object returned could be re-cast to it's mutable form meaning it's not truly mutation safe.
return innerDictionary;
}
public ImmutableDictionary<string, string> GetExampleThree() {
// Truly immutable object returned, but a new instance is built for every call; fortunately all of my dictionaries are small (containing at most 9 keys)
return innerDictionary.ToImmutableDictionary();
}
Option 1 is the way to go. You can recast ReadOnlyDictionary to IDictionary, but that will throw an Exception when trying to mutate:
void CastingTest()
{
var dic1 = new Dictionary<string, string>();
dic1.Add("Key", "Value");
var dic2 = new ReadOnlyDictionary<string, string>(dic1);
var castedDic = (IDictionary<string, string>)dic2;
castedDic.Add("AnotherKey", "Another Value"); //System.NotSupportedException, Collection is read only
}
The ReadOnlyDictionary doesn't create another Dictionary. It points to the same reference of the first one, encapsulating it. So if you do:
void AddTest()
{
var dic1 = new Dictionary<string, string>();
dic1.Add("Key", "Value");
var dic2 = new ReadOnlyDictionary<string, string>(dic1);
dic1.Add("Key2", "Value2"); //Now dic2 have 2 values too.
}
Never expose your innerDictionary and you'll be fine.
Determined that the neatest, easiest and safest; but not the most performant solution is to use a ConcurrentDictionary internally which ensures thread safety (from System.Collections.Concurrent) and then to use the System.Collections.Immutable to call dictionary.ToImmutableDictionary() which creates the dictionary which escapes the inner class. The interface signature is for ImmutableDictionary<KeyType, ValueType>.
This is not the most performant solution, but in my case with dictionaries with less than 12 keys and small simple objects representing state in most cases that is not a concern.

How to set object value instead of reference?

I've asked that question in a different manner and noone answered me, now I'm asking this with an example, hope It's crystal clear what I'm trying to do.
List<object> l1 = new List<object>() {"string1", "string2"};
Dictionary<string, object> map = new Dictionary<string, object>();
map.Add("aKey", l1[l1.Count - 1]);
object obj = map["aKey"];
What to do with obj in order to change l1[1] value it currently points to?
obj = "newString"; will set obj to "newString" and leave l1[1] - that is "string2" unchanged, because object is a reference type. but I don't want that
Or at least get 1 and l1 out of it.
My whole design is such that I have a storage in the form of two List.
For example you call Engine.Save
Save will get last element from list 2 by default, though if it's given a key as argument it will get the coresponding element from one of the two lists. Than will decide the element's type and save it accordingly or log error message.
I can't easily explain that, nor can I post that much code.
In the dictionary, you could store a reference to the list and the index separately, using tuples:
// Set up the list.
var myList = new List<object>() {"string1", "string2"};
// Set up the dictionary.
var myDict = new Dictionary<string, Tuple<List<object>, int>>();
myDict.Add("myKey", new Tuple<List<object>, int>>(myList, myList.Count - 1));
// Update the list by using the dictionary.
var theTuple = myDict["myKey"];
var theList = theTuple.Item1;
var theIndex = theTuple.Item2;
theList[theIndex] = "newString";

Changing dictionary if I send in function

I have one problem with dictionary. Little example:
Dictionary<int, bool> dict = new Dictionary<int, bool>();
for (int i = 0; i < 5; i++)
{
dict.Add(i, true);
}
var dict2 = dict;
dict2.Clear();
after this, I have to clear dictionary. What I need to do, if I need one changing dictionary and one unchanging dictionary?
Create a copy instead of just copying reference:
var dict2 = new Dictionary<int, bool>(dict);
Since Dictionary is a class, so it is a reference type.
When you write
var dict2 = dict;
You actually assign dict and dict2 to the same references. That's why when you want to use Clear() method, it effects both of them. You should copy them instead of copying their references. Like;
var dict2 = new Dictionary<int, bool>(dict);
From C# Language Specification;
Variables of value types directly contain their data whereas variables
of reference types store references to their data, the latter being
known as objects. With reference types, it is possible for two
variables to reference the same object and thus possible for
operations on one variable to affect the object referenced by the
other variable. With value types, the variables each have their own
copy of the data, and it is not possible for operations on one to
affect the other.

Categories

Resources