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

Asked

Viewed 73 times

1

I’m creating a class where I make 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 the two forms below:

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

    I answered now. I think you can solve your implementation. Look there.

3 answers

4


You are not making type inference in the class statement, you are making it in the variables statement when you instantiate the class. You’re just using a generic type in class.

What you want is a standard argument in the generic type, so when instantiating the class that requires you to be informed which type for the class to be created you don’t have to say which is the type, because after all, probably almost all cases are the same type to be used, exemplified string. It makes sense to want this, but unlike C++, C# doesn’t have this and is not expected to be so soon (but you can ask), will have to do in hand. But let’s face it, it’s not the end of the world. If I allowed it, it would be 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; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

It’s 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 don’t think it’s cool to create a hierarchy to facilitate typing instead of doing it to better model the problem, Seems like abuse of the mechanism, even if it works. Even the using, which does not pose serious problems other than a reduction in readability, is only used in complex cases.

  • =[... unfortunately I will have to continue making a class without generalization and extend her type, giving a new one to property...

  • 1

    Yes, that you can do, but there’s no language that allows it. Particularly generally I prefer to do so rather than create a class that creates a layer, an indirect one. At most I would make an alias with using, can do, right?

  • I didn’t know I could use the using thus...

2

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

It is necessary to be careful because it opens gaps 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 on . NET Fiddle

  • yes today is what I do, but the @Maniero response gave me a better feeback of why I can’t do this...

2

Basing me on your need to instance the 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 only his 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: https://dotnetfiddle.net/YmJRtk

  • 1

    yes today is what I do, but the @Maniero response gave me a better feeback of why I can’t do this...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.