C# object initialization of read only collection properties - c#

For the life of me, I cannot figure out what is going on in the example piece of C# code below. The collection (List) property of the test class is set as read only, but yet I can seemingly assign to it in the object initializer.
** EDIT: Fixed the problem with the List 'getter'
using System;
using System.Collections.Generic;
using NUnit.Framework;
namespace WF4.UnitTest
{
public class MyClass
{
private List<string> _strCol = new List<string> {"test1"};
public List<string> StringCollection
{
get
{
return _strCol;
}
}
}
[TestFixture]
public class UnitTests
{
[Test]
public void MyTest()
{
MyClass c = new MyClass
{
// huh? this property is read only!
StringCollection = { "test2", "test3" }
};
// none of these things compile (as I wouldn't expect them to)
//c.StringCollection = { "test1", "test2" };
//c.StringCollection = new Collection<string>();
// 'test1', 'test2', 'test3' is output
foreach (string s in c.StringCollection) Console.WriteLine(s);
}
}
}

This:
MyClass c = new MyClass
{
StringCollection = { "test2", "test3" }
};
is translated into this:
MyClass tmp = new MyClass();
tmp.StringCollection.Add("test2");
tmp.StringCollection.Add("test3");
MyClass c = tmp;
It's never trying to call a setter - it's just calling Add on the results of calling the getter. Note that it's also not clearing the original collection either.
This is described in more detail in section 7.6.10.3 of the C# 4 spec.
EDIT: Just as a point of interest, I was slightly surprised that it calls the getter twice. I expected it to call the getter once, and then call Add twice... the spec includes an example which demonstrates that.

You aren't calling the setter; you are essentially calling c.StringCollection.Add(...) each time (for "test2" and "test3") - it is a collection initializer. For it to be the property assignment, it would be:
// this WON'T work, as we can't assign to the property (no setter)
MyClass c = new MyClass
{
StringCollection = new StringCollection { "test2", "test3" }
};

I think that, beeing read only, you can't do
c.StringCollection = new List<string>();
But you can assign items to list...
Am I wrong?

The StringCollection property doesn't have a setter so unless you add one you cannot modify its value.

Related

C# 10.0 - How should I work around this particular "chicken and egg" problem involving two object that contain each other?

So I'm working with code in which there are two object that contain each other, inside a list of which I do not know the contents of until runtime. I have a function that needs to convert the first object to a third object, but to do such I need to convert the second object aswell, however to do that I will need the convert the first, and here is the chicken and the egg problem.
Problem Example
namespace chicken // Also I like formatting my code like this, so don't judge me
{
public class Object1 { // Object One and Two are of the same class
public List<dynamic> contents = new List<dynamic>();
public Object1() {}
public Object1(List<dynamic> contents) {
this.contents = contents;
}
}
public class Object3 {
public string name;
public Object3 friend;
public string pet;
public Object3(List<dynamic> converted) {
this.name = converted[0];
this.friend = converted[1];
this.pet = converted[2];
}
}
public class Program {
public static void Main(string[] args) {
Object1 object1 = new Object1(); // Just to create the problem, they don't
Object1 object2 = new Object1(); // get created like this in the actual code
object1.contents = new List<dynamic> {
"Steve Johnson", // This is example data, this order is supposed to be unknown
object2,
"Biscut",
};
object2.contents = new List<dynamic> {
"Bob Smith",
object1,
"Tiny",
};
Object3 final = convert(object1); // Runs the conversion function
}
public static Object3 convert(Object1 obj) {
List<dynamic> converted = new List<dynamic>(); // I need a sanitized list for Object3
for (int i = 0; i < obj.contents.Count; i++) {
if (obj.contents[i] is Object1) {
converted.Add(convert(obj.contents[i])); // Causes infinite loop due to chicken and egg problem
continue;
} converted.Add(obj.contents[i]);
}
Object3 object3 = new Object3(converted); // Again the list order is unknown
return object3;
}
}
}
I've tried using references, where there is a tunnel object and 'Object3' passes references to it's varibles to it, so I can semi construct Object3, put it in a cache, pass it to object 2 to convert it, then put in the values though the tunnel object containing the references. This got to complicated and I honestly don't know what to do without having an empty constuctor for Object 3.
The problem you're describing can be simplified down to this:
In the following code, how can we make it so Node a references b and b references a?
Node a, b;
a = new Node(new object[] { b });
b = new Node(new object[] { a });
public record Node(IEnumerable<object> Links);
You already hit on the solution: allow a Node to be constructed without all of its possible objects, so you can construct the Nodes first, and then insert them where they belong. It sounds like you don't want to give Node ("Object3") a default constructor, but the way you've implemented it at the moment, it should still be possible to add values after the fact, if you can add to its items list after its construction.
List<object> aList = new(), bList = new();
Node a = new Node(aList), b = new Node(bList);
aList.Add(b);
bList.Add(a);
If that will work for you, then the rest is just the details you've described:
using references, where there is a tunnel object and 'Object3' passes references to its variables to it, so I can semi construct Object3, put it in a cache, pass it to object 2 to convert it, then put in the values though the tunnel object containing the references.
It may be complicated, but that's pretty much what has to happen.
If, for some reason, you need your Object3 class structure to be immutable, so you cannot change its contents after its construction, then you're at an impasse: your requirements are clearly impossible. You're defining an object that must be constructed with all its dependencies, but its dependencies need it to be constructed before they can be constructed.
Here is a minimal representation of your code:
namespace chicken
{
public class Program
{
public static void Main(string[] args)
{
List<dynamic> chicken1 = new List<dynamic> { "Steve Johnson", null, "Biscut", };
List<dynamic> chicken2 = new List<dynamic> { "Bob Smith", chicken1, "Tiny", };
chicken1[1] = chicken2;
Convert(chicken1);
}
public static void Convert(List<dynamic> chicken)
{
foreach (dynamic inner in chicken)
{
if (inner is List<dynamic>)
{
Convert(inner);
}
}
}
}
}
You've just created two dynamic lists that refer to each other and then you try to recursively navigate from one list to the other infinitely.
There is nothing about an egg is your scenario that causes your problem. And, there's very little to do with chickens either, as you really only have two dynamic lists.
I suspect you have a real-world example that may have a chicken versus egg problem, but you haven't translated it into your question.
The bottom-line for me is that there are very few good uses for the keyword dynamic. In 99% of cases it's just syntactic sugar for adding bugs in your code.

Why does a combination of object and collection initializers use Add method?

The following combination of object and collection initializers does not give compilation error, but it is fundamentally wrong (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers#examples), because the Add method will be used in the initialization:
public class Foo
{
public List<string> Bar { get; set; }
}
static void Main()
{
var foo = new Foo
{
Bar =
{
"one",
"two"
}
};
}
So you'll get NullReferenceException. What is the reason for making such an unsafe decision while developing the syntax of the language? Why not to use initialization of a new collection for example?
First, it's not only for combination of object and collection initializers. What you are referring here is called nested collection initializers, and the same rule (or issue by your opinion) applies to nested object initializers. So if you have the following classes:
public class Foo
{
public Bar Bar { get; set; }
}
public class Bar
{
public string Baz { get; set; }
}
and you use the following code
var foo = new Foo
{
Bar = { Baz = "one" }
};
you'll get the same NRE at runtime because no new Bar will be created, but attempt to set Baz property of the Foo.Bar.
In general the syntax for object/collection initializer is
target = source
where the source could be an expression, object initializer or collection initializer. Note that new List<Bar> { … } is not a collection initializer - it's an object creation expression (after all, everything is an object, including collection) combined with collection initializer. And here is the difference - the idea is not to omit the new, but give you a choice to either use creation expression + object/collection initializer or only initializers.
Unfortunately the C# documentation does not explain that concept, but C# specification does that in the Object Initializers section:
A member initializer that specifies an object initializer after the equals sign is a nested object initializer, i.e. an initialization of an embedded object. Instead of assigning a new value to the field or property, the assignments in the nested object initializer are treated as assignments to members of the field or property. Nested object initializers cannot be applied to properties with a value type, or to read-only fields with a value type.
and
A member initializer that specifies a collection initializer after the equals sign is an initialization of an embedded collection. Instead of assigning a new collection to the target field, property or indexer, the elements given in the initializer are added to the collection referenced by the target.
So why is that? First, because it clearly does exactly what you are telling it to do. If you need new, then use new, otherwise it works as assignment (or add for collections).
Other reasons are - the target property could not be settable (already mentioned in other answers). But also it could be non creatable type (e.g. interface, abstract class), and even when it is a concrete class, except it is a struct, how it will decide that it should use new List<Bar> (or new Bar in my example) instead of new MyBarList, if we have
class MyBarList : List<Bar> { }
or new MyBar if we have
class MyBar : Bar { }
As you can see, the compiler cannot make such assumptions, so IMO the language feature is designed to work in the quite clear and logical way. The only confusing part probably is the usage of the = operator for something else, but I guess that was a tradeoff decision - use the same operator = and add new after that if needed.
Take a look at this code and the output of it due to the Debug.WriteLine():
public class Foo
{
public ObservableCollection<string> _bar = new ObservableCollection<string>();
public ObservableCollection<string> Bar
{
get
{
Debug.WriteLine("Bar property getter called");
return _bar;
}
set
{
Debug.WriteLine("Bar allocated");
_bar = value;
}
}
public Foo()
{
_bar.CollectionChanged += _bar_CollectionChanged;
}
private void _bar_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Debug.WriteLine("Item added");
}
}
public MainWindow()
{
Debug.WriteLine("Starting..");
var foo = new Foo
{
Bar =
{
"one",
"two"
}
};
Debug.WriteLine("Ending..");
}
The output is:
Starting..
Bar property getter called
Item added
Bar property getter called
Item added
Ending..
For you questions:
What is the reason for making such an unsafe decision while developing the syntax of the language? Why not to use initialization of a new collection for example?
Answer:
As you can see the intention of the designer of that feature was not to reallocate the collection but rather to help you add items to it more easily considering that you manage your collection allocation by yourself.
Hope this clear things out ;)
Consider the following code:
class Program
{
static void Main()
{
var foo = new Foo
{
Bar =
{
"one",
"two"
}
};
}
}
public class Foo
{
public List<string> Bar { get; set; } = new List<string>();
}
The compiler does not know whether you already created a new list instance within the class constructor (or in another method).
Recall that collection initializer is a series of calls to Add method on an existing collection!
See also:
Custom Collection Initializers
Also note that this initializer applies to a collection that was exposed as a property. Hence the collection initializer is possible as part of the outer object initializer (the Foo object in your example).
However, if it was a simple variable, the compiler would not let you to intialize the collection this way. Here is an example:
List<string> list =
{
"one",
"two"
};
This will throws a compilation error.
As last example, the output of the following code will be: "one, two, three, four, ". I think that now you understand why.
Pay attention to the list static instance, as well as to the private modifier in the "set" of the Bar property, which does not matters because the initializer just calls the Add method, which is accessible even when the Bar "set" is private.
class Program
{
static void Main()
{
var foo1 = new Foo
{
Bar =
{
"one",
"two"
}
};
var foo2 = new Foo
{
Bar =
{
"three",
"four"
}
};
PrintList(foo1.Bar);
}
public static void PrintList(List<string> list)
{
foreach (var item in list)
{
Console.Write(item + ", ");
}
Console.WriteLine();
}
}
public class Foo
{
private static readonly List<string> _bar = new List<string>();
public List<string> Bar { get; private set; } = _bar;
}
I believe the key thing to understand here is that there are two syntactic sugar flavors at play (or at least, there should be):
Object Initialization
Collection Initialization
Take away the List for a moment and look at the field as an object:
public class Foo
{
public object Bar { get; set; }
}
When using Object Initialization, you assign an object (or null):
var foo = new Foo()
{
Bar = new object(); //or Bar = null
}
Now, let's go back to your original example and slap Collection Initialization on top of this. This time around, the compiler realizes this property implements IEnumerable and the array you have provided is of the right type, so it attempts to call the Add method of the interface. It must first go looking for the object, which in your case is null because you haven't initialized it internally. If you debug through this, you will find that the getter gets called and returns null, hence the error.
The correct way of mixing both features then would be for you to assign a new object that you initialize with your values:
var foo = new Foo()
{
Bar = new List<string>(){ "one", "two" }
};
If you debug this version, you will find that the setter is called instead, with the new instance you initialized.
Alternatively, you can initialize your property internally:
public List<string> Bar { get; set; } = new List<string>();
If you debug this version, you will find that the property is first initialized with a value and your version of the code then executes without error (by calling the getter first):
var foo = new Foo()
{
Bar = {"one", "two"}
};
To illustrate the syntactic sugar aspect, Collection Initialization only works within the confines of a constructor calling statement:
List<string> bar = {"one", "two" }; //ERROR: Can only use array initializer expressions to assign to array types. Try using a new expression instead.
List<string> bar = new[] { "one", "two" }; //ERROR: Cannot implicitly convert type 'string[]' to 'System.Collections.Generic.List<string>'
List<string> bar = new List<string>() { "one", "two" }; //This works!
If you wish to allow initialization like in your original example, then the expectation is that the variable will be set to an instance before the Add method can be called. This is true whether you use syntactic sugar or not. I could just as well run into the same error by doing this:
var foo = new Foo();
foo.Bar.Add("one");
So you may want to initialize the variable in order to cover all bases, unless of course a null value has a semantic meaning in your application.

Return a constant string array

I created a string[] getter to get some information on a class. I want it to always return the same value, and not create a new object on each call.
I have it implemented now like this:
string[] _someStrings = { "foo", "bar" };
protected string[] someStrings {
get {
return _someStrings;
}
}
which seem to be OK. However, my first inkling was to write it like this:
protected string[] someStrings {
get {
return { "foo", "bar" };
}
}
but that doesn't work (I get the error ; expected).
Why?
(this is mainly a "getting-to-understand-C# question).
update I made a typo. I do not want to create a new object on each call.
The correct syntax would be this:
return new [] { "foo", "bar" };
The reason is that the short syntax without new [] is only valid for an assignment.
As you correctly note in a comment, this will create a new object on every call. The only way to avoid this is with a field that stores the created instance and return that field. This is exactly the solution you already have.
Please note however, that this allows consumers to change the contents of the array and affect other consumers:
var a1 = foo.SomeStrings;
var a2 = foo.SomeStrings;
a1[0] = "Some other value";
Assert.Equal("Some other value", a2[0]); // will pass
As an alternate approach, may I suggest, if the contents are supposed to be constant, using a read-only collection instead, such that:
private readonly ReadOnlyCollection<string> UnderlyingReadOnlyStrings;
// populate the read-only collection, then...
public ReadOnlyCollection<string> ReadOnlyStrings {
get { return UnderlyingReadOnlyStrings; }
}
The benefit here is that your collection truly is read-only. And practically constant. It can't be re-assigned to, and the contents cannot be altered. You could even declare the underlying collection as static and populate in a static constructor.
Your second example doesn't work, as previously explained, because you're trying to return an "inline array", so to speak, and the syntax is not correct, and if it were, you would be newing the array each time - which goes against your requirements.
Your syntax is incorrect. Try this:
protected string[] someStrings
{
get
{
return new string[] { "foo", "bar" };
}
}
You can't have const array, but you can have a readonly one that will work as you expect (can also be static, obviously):
public readonly string[] someStrings = { "foo", "bar" };

Interface collection member strange behavior during object initialization

I run into runtime NullReferenceException exception in the following code:
public class Container
{
public IList<string> Items { get; set; }
}
class Program
{
static void Main(string[] args)
{
var container = new Container() { Items = {"Test"} };
}
}
It's logical that compiller couldn't create interaface instance but I got a runtime exception, not a compile time. I was confused even more when I investigated this behavior further:
var container = new Container() { Items = {} }; //Legal, Items is null after initialization
var container = new Container() { Items = { "Test" } }; //Legal, throws exception
container.Items = {}; //Illegal doesn't compile
container.Items = {"Test"}; //Illegal doesn't compile
Is this some kind of bug or I don't understand something?
And I'm using .net framework 4.0
It compiles, because compiler has no knowledge about is the List already initialized somewhere else. You can make it work by adding initialization into a constructor:
public class Container
{
public IList<string> Items { get; set; }
public Container()
{
Items = new List<string>();
}
}
Or changing the property to hide a field, which is initialized when class instance is created:
private IList<string> items = new List<string>();
public IList<string> Items
{
get { return items; }
set { items = value; }
}
Then, var container = new Container() { Items = { "Test" } }; works just fine.
At runtime .Add() method is called for every item in collection initializer group. When property is not initialized with new List<string> it has null value, and that's why NullReferenceException is thrown.
Object and Collection Initializers (C# Programming Guide)
By using a collection initializer you do not have to specify multiple
calls to the Add method of the class in your source code; the compiler
adds the calls.
you didn't initialize the List
var container = new Container() { Items = new List<string>() { "Test" } };
By the way the below is legal as for the compiler there is nothing wrong with it (syntax is correct,etc)
var container = new Container() { Items = {} };
but because the compiler doesn't know that Items list has not been initialized (you are not passing any item in the collection initializator {}) the .Add method won't be called on the List and the run-time won't know that the Items object is null
On the other hand the below is legal for the compiler but it throws an exception at run time because you try to initialize the the list passing an item (it is correct for the compiler for the same reason explained above) so when the run time will call the .Add method behind the scene, it will throw a null reference exception because Items has not been initialized
var container = new Container() { Items = { "Test" } };

C# initialization question

This might be lame, but here:
public interface Interface<T>
{
T Value { get; }
}
public class InterfaceProxy<T> : Interface<T>
{
public T Value { get; set; }
}
public class ImplementedInterface: InterfaceProxy<Double> {}
Now I want to create an instance of the ImplementedInterface and initialize it's members.
Can this be done somehow like this (using initialization lists) or the same behavior can only be achieved using the constructor with Double argument?
var x = new ImplementedInteface { 30.0 };
Can be done by:
var x = new ImplementedInteface { Value = 30.0 };
var x = new ImplementedInterface() { Value = 30.0 };
The only way to achieve what you're after is if your class implements IEnumerable<T> and has an Add method:
public class MyClass : IEnumerable<double>
{
public void Add(double x){}
}
Then you can do:
MyClass mc = new MyClass { 20.0 };
Obviously that's not what you want, because that doesn't set your Value and it allows you to add multiple values:
MyClass mc = new MyClass { 20.0, 30.0 , 40.0 };
Just go with the standard object initializes like others have pointed out:
var x = new ImplementedInterface() { Value = 30.0 };
You should be able to do:
var x = new ImplementedInterface {Value = 30.0};
var instance = new ImplementedInterface { Value = 30.0 }; will work. However, this isn't really the same set of operations as C++ initializer lists -- this is an object initializer. It initializes the new instance via the default constructor and then invokes the property setters for each property.
In other words, the object is constructed before the property setters run. If you want the values for the properties set before construction of ImplementedInterface completes, you'd have to write a constructor, as you noted. This distinction in behavior usually doesn't matter, but it's good to be aware of.
I am not sure if you have a special reason to use the interfaces that way but the following code might work for you.
public class ImplementedInterface2 : List<double> { }
public class test
{
public void x()
{
var x = new ImplementedInterface2() { 30.0 };
}
}
var x = new ImplementedInterface { Value = 30.0 };
You can definitely use an initialization list, but you have to specify what 30.0 is (this is true for any initialization list, not just the code you have):
var x = new ImplementedInteface { Value=30.0 };

Categories

Resources