Arlen Beiler
Arlen Beiler

Reputation: 15876

Setting the generic type param to object by default c#

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.

Upvotes: 2

Views: 233

Answers (1)

SLaks
SLaks

Reputation: 887453

Generic type parameters cannot have default values.

However, they can have overloads:

private static object GetBin(string file) { return GetBin<object>(file); }

Upvotes: 3

Related Questions