0

sorry, my english is poor. how to create MyGeneric object, and allow T is null? thanks very muck.

public class MyGeneric<T>
{
    public int result { get; set; }

    public string message { get; set; }

    public T data { get; set; }
}

//...how to create MyGeneric object, and allow T is null?
var myGeneric = new MyGeneric<null> { 
    result = 1, 
    message = "" 
};

Console.WriteLine(myGeneric.result);
3
  • 4
    null is not a type. Why do you need this? What are you trying to do? do you want the value of data to be null? Commented Jan 2, 2016 at 1:55
  • yes, I want the value of date to be null. Commented Jan 2, 2016 at 2:29
  • can you use a MyGeneric<string>?. The default value of string is null. Commented Jan 2, 2016 at 2:33

1 Answer 1

1

In C#, generics vary based on types, not values. This is because generics were intended to be used to allow the designing developer to create types that support members, parameters, and/or return values that vary based on what types a client developer wants to use the generic with.

For example, the built-in List<> type allows developers to store lists of whatever they want and List<> supports this as if the collection was a type-safe collection made just for that type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.