2

I have a class Data<T> which I use to store data received on a communication link. The Data<T> class contains a Value property of type T and a Timestamp property of type DateTime.

Is it possible to determine in the class constructor that the requested type's default value is null, and in that case instantiate it?

I've looked at the new() class constraint. But then I'm having trouble using the class with a string type, as I'm getting errors about string not having a parameterless constructor.

Here's an example causing a NullReferenceException on dataValue4.Value, since the Value property of type Foo is not instantiated.

public class Data<T> {
    public Data() {
        _timestamp = DateTime.Now;
    }

    private T _value;
    public T Value {
        get { return _value; }
        set { 
            _value = value;
            _timestamp = DateTime.Now;
        }
    }

    private DateTime _timestamp;
    public DateTime Timestamp {
        get { return _timestamp; }
    }
}

public class Foo {
    public int Value1 { get; set; }
    public bool Value2 { get; set; }
}

public class Program {
    public static void Main() {

        var dataValue1 = new Data<int>();
        var dataValue2 = new Data<bool>();
        var dataValue3 = new Data<string>();
        var dataValue4 = new Data<Foo>();
        //dataValue4.Value = new Foo();

        Console.WriteLine(dataValue1.Value);  // Output is "0"
        Console.WriteLine(dataValue2.Value);  // Output is "False"
        Console.WriteLine(dataValue3.Value);  // Output is ""
        Console.WriteLine(dataValue4.Value.Value1);  // Output is "0"
        Console.WriteLine(dataValue4.Value.Value2);  // Output is "False"
    }
}
Oystein
  • 1,232
  • 11
  • 26
  • This actually looks like you want `Foo` to be a `struct` instead of a `class`. You could force the `Data` constructor to take a `T`, but you're still going to have to create the `Foo` outside the `Data` class because you just will not know what the constructor for `T` needs. – juharr Dec 13 '17 at 18:15
  • Does question marked as duplicate answers your question? – Evk Dec 13 '17 at 18:42
  • @Evk It actually helped me to find a working solution, but I wouldn't say my question is "an exact duplicate" as stated in the message. I want to add my solution as an answer, but I cannot. Is it because the question is marked? – Oystein Dec 13 '17 at 18:45
  • 1
    Yes, but I reopened it, because also feel it is not a duplicate. – Evk Dec 13 '17 at 18:46

1 Answers1

0

You can create a instance with Activator.

var type = typeof(T);
var value = Activator.CreateInstance(type) as T;

This will create a instance with the default value of the type.

gabemilani
  • 54
  • 1
  • 4
  • How can this be used with value types? It says I have to use a `class` constraint on the class, which then causes errors when trying to use the class with value types. – Oystein Dec 13 '17 at 18:30
  • That's not the best way with value types, try `default(T)` instead – gabemilani Dec 13 '17 at 18:43