Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generics: Simplify type signature [duplicate]

Tags:

c#

generics

If I have a generic Item class that looks like this:

abstract class Item<T>
{
}

And a Container of Items that looks like this:

class Container<TItem, T>
    where TItem : Item<T>
{
}

Since TItem depends on T, is it possible to simplify the type signature of Container so that it takes only one type parameter? What I really want is something this:

class Container<TItem>
    where TItem : Item   // this doesn't actually work, because Item takes a type parameter
{
}

So I can instantiate it as follows:

class StringItem : Item<string>
{
}

var good = new Container<StringItem>();
var bad = new Container<StringItem, string>();

The compiler should be able to deduce that T is string when TItem is StringItem, right? How do I make this happen?

Desired usage:

class MyItem : Item<string>
{
}

Container<MyItem> container = GetContainer();
MyItem item = container.GetItem(0);
item.MyMethod();
like image 983
Brian Berns Avatar asked Apr 18 '13 15:04

Brian Berns


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C programming used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

This should do what you want I think. Obviously you're now doing Container<string> not Container<StringItem> but as you've not included usage examples I can't see it being a problem.

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var myContainer = new Container<string>();

            myContainer.MyItems = new List<Item<string>>();
        }
    }

    public class Item<T> { }

    public class Container<T>
    {
        // Just some property on your container to show you can use Item<T>
        public List<Item<T>> MyItems { get; set; }
    }
}

How about this revised version:

using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var myContainer = new Container<StringItem>();

            myContainer.StronglyTypedItem = new StringItem();
        }
    }

    public class Item<T> { }

    public class StringItem : Item<string> { }

    // Probably a way to hide this, but can't figure it out now
    // (needs to be public because it's a base type)
    // Probably involves making a container (or 3rd class??)
    // wrap a private container, not inherit it
    public class PrivateContainer<TItem, T> where TItem : Item<T> { }

    // Public interface
    public class Container<T> : PrivateContainer<Item<T>, T>
    {
        // Just some property on your container to show you can use Item<T>
        public T StronglyTypedItem { get; set; }
    }
}
like image 116
George Duckett Avatar answered Sep 21 '22 08:09

George Duckett


I think one possible solution to your problem is adding interface IItem and the code structure will be like following.

interface IItem { }

abstract class Item<T> : IItem { }

class Container<TItem> where TItem : IItem { }

class StringItem: Item<string> { }

And now you can have the Container<StringItem>:

var container = new Container<StringItem>();
like image 37
hazzik Avatar answered Sep 18 '22 08:09

hazzik