Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to structure a generic method so T is optional?

Tags:

c#

generics

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.

like image 796
NibblyPig Avatar asked Apr 10 '15 14:04

NibblyPig


People also ask

How do I make generic optional?

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.

Can a non generic class have a generic method?

Yes, you can define a generic method in a non-generic class in Java.

Can you have a generic method in a non generic class C#?

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.

What is a generic method and when should it be used?

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.

What is a generic method in Java?

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.

Can generic methods be overloaded on several type parameters?

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.

Does type inference work with generic methods?

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.

How to make a generic type Optional in C++?

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.


2 Answers

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);
    }
}
like image 161
Magnus Avatar answered Oct 13 '22 15:10

Magnus


You can always create a non-generic base class

public class Bunny 
{
}

public class Bunny<T> : Bunny
{
}
like image 36
Steve Mitcham Avatar answered Oct 13 '22 15:10

Steve Mitcham