How to create a class with type inference that can have a default type in C #

1

I'm creating a class where I do the type inference, as in the example below:

public class Foo<T> {
    public T Bar { get; set; }
}

Is there any way I can set T to a default type like string ? My intention is to be able to use both of the following ways:

public class Program {
    public static void Main(string[] args) {
        var a = new Foo<int>();
        var b = new Foo();
    }
}
    
asked by anonymous 16.10.2018 / 20:48

3 answers

3

You are not making type inference in the class declaration, you are doing in the declaration of variables when you instantiate the class. You are only using a generic type in the class.

What you want is a default argument in the generic type, so when instantiating the class that requires you to be told which type to create the class does not need to say what type it is, because after all, probably almost every case is the same type to be used, exemplified string . It makes sense to want this, but unlike C ++, C # does not have this and has no forecast to have it so early (but you can request ), will have to do in hand. But let's face it, it's not the end of the world. If it were allowed, it would look something like this:

public class Foo<T = string> {
    public T Bar { get; set; }
}

There are some cases that a generic method may have the type inferred by the argument used within it, but it is not what you want.

You can create an alias with using and avoid full use:

using Foo = Foo<string>;

public class Program {
    public static void Main() {
        var a = new Foo<int>();
        var b = new Foo();
    }
}
public class Foo<T> {
    public T Bar { get; set; }
}

See running on .NET Fiddle . And no Coding Ground . Also put it in GitHub for future reference .

It is also possible to create a class that encapsulates this as shown in the other answers, but this can be problematic for a very small gain, I do not think it's cool to create a hierarchy to make it easier to type rather than to model the problem better. seems to abuse the mechanism, even if it works. Even using , which does not cause serious problems other than a reduction in legibility, is only used in complex cases.

    
16.10.2018 / 21:20
2

You can create a class with the generic type "fixed".

You have to be careful because this opens up loopholes for someone to modify the derived class and it does not conform to the original.

class Foo : Foo<string> 
{
}

Executable example:

using System;

class Program
{
    static void Main()
    {
        var a = new Foo<int> { Bar = 1 };
        var b = new Foo { Bar = "Hey" };

        Console.WriteLine(a.Bar);
        Console.WriteLine(b.Bar);
    }
}

class Foo<T> 
{
    public T Bar { get; set; }
}

class Foo : Foo<string> 
{
}

See working in .NET Fiddle

    
16.10.2018 / 22:00
2

Based on your need to instantiate classes with var a = new Foo<int>(); or var b = new Foo(); the implementation below resolves.

Note that the code of class Program is the same as your example.

public class Foo<T> {
    public T Bar { get; set; }
}

//Aqui você define o tipo padrão. 
public class Foo : Foo<string> { }

public class Program {
    public static void Main(string[] args) {
        var a = new Foo<int>();
        var b = new Foo();
    }
}

See the example running here: link

    
16.10.2018 / 22:46