Hard question to phrase, but if I have:
public class BunnyManager
{
public Bunny<T> GetBunny<T>(string someJson)
{
return new Bunny<T>(someJson);
}
}
public class Bunny<T>
{
T parsedJson { get; set; }
public Bunny<T>(string someJson)
{
if (!string.IsNullOrEmpty(someJson))
parsedJson = ConvertJsonStringToObject<T>(someJson);
}
}
in some cases I want to get a Bunny object without any json, because the json string is null, so I don't care what T is.
In this case, can I create an overload or something to ignore T
completely, or can I call GetBunny<null>
or GetBunny<object>
?
I'm wondering what the correct way to solve this might be.
To make a generic type optional, you have to assign the void as the default value. In the example below, even though the function takes a generic type T, still you can call this function without passing the generic type and it takes void as default.
Yes, you can define a generic method in a non-generic class in Java.
Yes, There are two level where you can apply generic type . You can apply generic type on Method level as well as Class level (both are optional). As above example you applied generic type at method level so, you must apply generic on method return type and method name as well. You need to change a bit of code.
Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem.
A generic method is a method that is declared with type parameters, as follows: The following code example shows one way to call the method by using int for the type argument: You can also omit the type argument and the compiler will infer it.
Generic methods can be overloaded on several type parameters. For example, the following methods can all be located in the same class: For more information, see the C# Language Specification.
Generic Methods (C# Programming Guide) Therefore type inference does not work with methods that have no parameters. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name.
To make a generic type optional, you have to assign the void as the default value. In the example below, even though the function takes a generic type T, still you can call this function without passing the generic type and it takes void as default.
You can have a non generic bunny class that the generic one inherits from.
public class BunnyManager
{
public Bunny<T> GetBunny<T>(string someJson)
{
return new Bunny<T>(someJson);
}
public Bunny GetBunny()
{
return new Bunny();
}
}
public class Bunny
{
}
public class Bunny<T> : Bunny
{
T parsedJson { get; set; }
public Bunny(string someJson)
{
if (!string.IsNullOrEmpty(someJson))
parsedJson = ConvertJsonStringToObject<T>(someJson);
}
}
You can always create a non-generic base class
public class Bunny
{
}
public class Bunny<T> : Bunny
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With