-1

I have the same problem that IList<int> throws Null Reference Exception when adding values, and the solution is initialize the list field in the constructor with "new List();", but in my program I have a structure, no a class. What can I do with C# if the structures in C# are parameterless?

public struct ListStruct<T> : IEnumerable<T>, IList<T>
{
    private List<T> value;

    //public ListStruct()
    //{
    //    this.value = new List<T>();
    //}

    // ...

    public void Add(T item)
    {
        ((IList<T>)value).Add(item); // Execution time: NullReferenceException was unhandled
    }
}
Community
  • 1
  • 1
Joe
  • 1
  • 1
  • 8
    Wrapping a `List` inside of a `struct` seems like a bad idea to me. Why are you doing this? FYI the reference is the `List` inside of your `struct`. You should initialize it `private List value = new List();`. – juharr May 04 '16 at 14:35
  • 2
    Why do you comment out the constructor? You already have the code to solve the problem. (Well, almost. You need to define parameters for the constructor. This kind of goes back to the comment above, trying to understand what this struct is actually meant to *be*.) – David May 04 '16 at 14:38
  • This is not a duplicate as marked by doctor and David. The issue is that struct can't have default constructor, so there is no way to initialize it's member stored by reference, unless you provide constructor with parameter (https://msdn.microsoft.com/en-us/library/aa288208(v=vs.71).aspx) or abandon your idea to have it as `struct` – zmechanic May 04 '16 at 14:44
  • @zmechanic: It's a duplicate in the sense that the value is `null` and needs to be initialized. It can be initialized in a constructor, in the `Add()` method, somewhere else, etc. The `NullReferenceException` itself is pretty clear. – David May 04 '16 at 14:45
  • 2
    @David No, a `struct` cannot have a parameterless constructor, so you *cannot* initialize that value in the constructor (or at least you cannot rely on it to be initialized from a constructor). Likewise, structs cannot initialize fields. – Servy May 04 '16 at 14:56
  • @Servy: True, the OP would need to define at least one parameter to be used to construct the struct. Which brings the whole thing back to the question of what this struct is supposed to represent, which isn't really clear. – David May 04 '16 at 14:57
  • @David Even then, the struct could still be created without providing any arguments, resulting in the list not being initialized. – Servy May 04 '16 at 14:59
  • 1
    @juharr structs can't initialize fields. – Servy May 04 '16 at 14:59
  • @Servy: Which also goes back to the duplicate question. Overall I don't think it's entirely clear what the OP is doing, but the root of the problem is pretty straightforward. He needs to initialize the `value` variable before using it. – David May 04 '16 at 15:00
  • @David That's not the root of the problem at all. The surface level of the problem is that it's impossible to initialize the field of a struct in any way; this is by design. The *root* of the problem is that he shouldn't be using a struct here at all, because his object isn't conceptually a value. He should be using a class, and once he makes that change his problems will go away. – Servy May 04 '16 at 15:01
  • @Servy: While I suppose we could argue semantics about the root vs. the surface, on that last point we completely agree. I can't imagine the benefit of using a struct here at all. (Technically he *could* initialize the field directly in the `Add()` method, in an `if (value == null)` block. Which would also have to happen in almost every method. That would solve the *error*, but not necessarily the *problem*.) – David May 04 '16 at 15:03
  • IMO the heart of the question was not NullReferenceException but struct vs class difference (which is whole other opera) or if struct is really required, finding good ways to automate struct initialization. (EnsureInitialized()-like vs forcing callers to do it). The latter would have been interesting. – Imre Pühvel May 04 '16 at 15:06
  • You are very fast marking as duplicate, guys. As said @David, I've added a null checking in the the Add() method (and if the list is null, is created). I can't use a class because I can't overload operators in a class. – Joe May 04 '16 at 15:09
  • @Servy I guess I would have known that if I had ever tried it. But I've never tried to create a struct that contains reference types. – juharr May 04 '16 at 16:33
  • @juharr It doesn't matter whether its a reference type or not. No structs can initialize any fields, ever. – Servy May 04 '16 at 16:37

1 Answers1

0

If you really needed this, you could have the Add() method test for nullity and assign a new list if necessary.

It's hard to see why you would need this though. It's not semantically a value type, so it's not something that should generally be a struct, and it doesn't make much sense to abuse it for performance reasons and then go through a more convoluted path than you could otherwise.

Jon Hanna
  • 102,999
  • 9
  • 134
  • 232