2

Given a method:

private static T GetBin<T>(string file)

Is it possible to set T to default to object, if I use:

public static byte[] ToJSONBytes<T>(this T obj) 

It defaults to whatever obj is set as. However, this GetBin method Deserializes a file, using the BinaryFormatter, and as you all know, it returns an object. My method explicitly casts it to T and then returns it, but I want to make T optional and default to object.

4

1 回答 1

3

Generic type parameters cannot have default values.

However, they can have overloads:

private static object GetBin(string file) { return GetBin<object>(file); }
于 2013-03-12T16:48:55.403 回答