Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics method signature and standard signature identical

assume you have a class which has methods using T. And you also have standard methods of the same name.

What happens if T is of the same type as the standard method? The standard method is called.

Is there any way to force him to call the T-method anyway?

using System;

namespace ConsoleApplication3
{
class Program
{
    static void Main()
    {
        Generics<Int32> anInt = new Generics<Int32>(4);
        Generics<String> aString = new Generics<String>("test");
    }
}

public class Generics<T>
{
    public T Member;

    public String ErrorMessage;

    public Generics(T member)
    {
        this.Member = member;
    }

    public Generics(String errorMessage)
    {
        this.ErrorMessage = errorMessage;
    }
}
}
like image 696
Dee J. Doena Avatar asked Jun 09 '13 07:06

Dee J. Doena


People also ask

What are the benefits of using generic types in a class definition?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

What is generics in Java and how it works?

A generic type is a class or interface that is parameterized over types, meaning that a type can be assigned by performing generic type invocation, which will replace the generic type with the assigned concrete type.

What type of .NET exception could be reduced by using generics?

Type safety. Generics shift the burden of type safety from you to the compiler. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.


2 Answers

Sorry, there is not.

The simplest solution is to use two different method names to indicate the difference in behaviour. Since the method names in question are constructors, you have no control over the names, so you must change at least one of them into a normal method instead. For example:

public class Generics<T>
{
    public T Member;

    public String ErrorMessage;

    public Generics(T member)
    {
        this.Member = member;
    }

    private Generics()
    {
        // empty constructor just to allow the class to create itself internally
    }

    public static Generics<T> FromError(String errorMessage)
    {
        return new Generics<T> { ErrorMessage = errorMessage };
    }
}

Personally I would change both constructors to be static methods, so that the difference in behaviour is made absolutely clear to the user. However, if you only change one constructor, make it the error one.

like image 77
Christian Hayter Avatar answered Nov 03 '22 00:11

Christian Hayter


Okay, I'll make my comment into an answer.

First of all, DO NOT do this. DO NOT. You want humans to be able to read your code, not just computers. (There, I've done my duty as a member of the C# programmer space)

Second.

As described here:

If you design your code so that the compiler cannot tell when it's compiling what type you're calling with, you can force it to use the generic method.

static Generics<T> Test<T> (T parameterToTest) {
    return new Generics<T>(parameterToTest);
}

static void Main()
{
    Generics<Int32> anInt = Test<Int32>(4);
    Generics<String> aString = Test<String>("test");
}
like image 25
sq33G Avatar answered Nov 02 '22 23:11

sq33G