c# alternative to '? extends class' (java)

Hi,

I'm converting some java code to c# and am wondering if there is a direct alternative for java's '? extends MyClass' i.e. i have a class type that inherits from an abstract class that implements an interface, how do i pass an instance of the child class into a method that takes any class that extends my abstract class?

Currently i have the sig in c# taking an interface but this isn't accepting my class even though its parent implements the interface
basil365Asked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
You haven't displayed the definition of FractionalOddChart, but barring that, shouldn't the constructor for IntegerKeyLookupMap be defined as:

public IntegerKeyLookupMap(Dictionary<int, ILookup<int>> lookupMap)
{

}

Open in new window

0
 
käµfm³d 👽Commented:
Have you tried setting the signature of the method to accept objects of type "AbstractClass" (that is, your abstract class name)? As long as a child is within the inheritance chain of the abstract class, you should be able to pass it into the function without incident.
0
 
basil365Author Commented:
unfortunately that doesn't seem to work either
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
käµfm³d 👽Commented:
Can you provide a crude C# example of what you are doing?
0
 
käµfm³d 👽Commented:
Here is an example of what I was referring to:

using System;

namespace Application
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Employee e = new Employee() { FirstName = "John" };
            Manager m = new Manager() { FirstName = "Fred" };
            
            PrintName(e);
            PrintName(m);
            
            Console.ReadKey();
        }
        
        public static void PrintName(Person p)
        {
            Console.WriteLine(p.FirstName);
        }
    }
    
    abstract public class Person
    {
        public string FirstName { get; set; }
    }
    
    public class Employee : Person { }
    
    public class Manager : Person { }
}

Open in new window

0
 
basil365Author Commented:
what i have is:

interface:

public interface ILookup<T>
    {
        T KeyValue { get; set; }
    }

Open in new window


abstract:
public abstract class AbstractLookup<T> : ILookup<T>
    {
        private T keyValue;

        public T KeyValue
        {
            get { return keyValue; }
            set { keyValue = value; }
        }

    }

Open in new window


implementation:

 public class FOChart:AbstractLookup<int>
    {

        private DecimalKeyLookupMap fractionalOddMap;

        public DecimalKeyLookupMap FractionalOddMap
        {
            get { return fractionalOddMap; }
        }

        public  FOChart(int chartId, DecimalKeyLookupMap map)
        {
             KeyValue = chartId;
            this.fractionalOddMap = map;
        }

    }

Open in new window



(problem here is in another objects constructor)

 public class IntegerKeyLookupMap 
    {

        public IntegerKeyLookupMap(Dictionary<int,  FractionalOddChart> lookupMap)

        {

        }

        public ILookup<int> getEntry(int entryId)
        {
            //do stuff
        }

in the java version  the 'ILookup<int>' was replaced with '? extends'ILookup<int>'.

In the above version i get an error when i try to pass a dictionary of 'Dictionary<int, ILookup<int>> '

    }

Open in new window

0
 
käµfm³d 👽Commented:
What version of the framework are you targeting?
0
 
basil365Author Commented:
4.0
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.