Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C#
class BaseClass<T>
{
    public void Reset() { data.Clear(); }
    public void Add(T value) { data.Add(value); }
    public int Count { get { return(data.Count); } }
    public T this[int index]
    {
        get { return (data[index]); }
        set { data[index] = value; }
    }

    List<T> data = new List<T>();
}

class DerivedClass<T> : BaseClass<T>
{
    public DerivedClass()
    {
        this.DoSomething();
    }

    private void DoSomething()
    {
        Add(200);
    }
}

When I do
DerivedClass<int> inst = new DerivedClass<int>();

inst.Add(200) will compile

but Add(200) in method DoSomething() will give a compile error "cannot convert 'int' to 'T'.
Posted
Updated 22-Nov-13 10:04am
v3

try this code...


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            DerivedClass inst = new DerivedClass();

            inst.Add(200);
 


        }
    }

    class BaseClass<T>
    {
        public void Reset() { data.Clear(); }
        public void Add(T value) { data.Add(value); }
        public int Count { get { return (data.Count); } }
        public T this[int index]
        {
            get { return (data[index]); }
            set { data[index] = value; }
        }

        List<T> data = new List<T>();
    }

    class DerivedClass : BaseClass <int> 
    {
        public DerivedClass()
        {
            this.DoSomething();
        }

        private void DoSomething()
        {
            Add(200);
        }
    }

}
 
Share this answer
 
Comments
fheyn 22-Nov-13 15:41pm    
thanks a lot
you're right, I don't need DerivedClass to be generic.
thank you so much
Karthik_Mahalingam 22-Nov-13 15:45pm    
most welcome :)
You need to cast it:

C#
private void DoSomething()
{
    Add((T)200);
}


But be careful, if 200 isn't convertable to T, it will throw an exception at runtime.
 
Share this answer
 
Comments
fheyn 22-Nov-13 15:31pm    
that's what I tried first and still get the same error-message
Ron Beyer 22-Nov-13 15:40pm    
I apologize, my solution is wrong, I will give a new one.
You can't do Add(200) in the derived class because how do you know that T is a value type?

The compiler is trying to tell you that 200 can't be converted to T because it doesn't satisfy the constraints.

In order to do what you want, you need to either do what Solution 2 has, or there is a lengthy discussion in this CodeProject article[^].

Many have said, having a generic constraint for numeric types was left out of the framework. It would be nice to have but its just not there.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900