Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Am I misunderstanding polymorphism in c#?

Posted on 2011-02-17
3
Medium Priority
?
269 Views
Last Modified: 2012-05-11
It's a simple scenario.

I have two interfaces like:

public interface IBaseExample {}

public interface IExample : IBaseExample { string Hello { get;set; } }

Open in new window


Then I have two classes like :

public class Example : IExample
{
    public string Hello { get; set; }
}

public class SolidClass<T> where T : IBaseExample
{
     public SolidClass()
    {
          T tmp = new Example();
     }
}

Open in new window


But this doesn't compile. Error: "Cannot implicitly convert type 'Example' to 'T'"

Am I missing something??
0
Comment
Question by:KiasChaos83
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 34923126
T represents a class that it implements IBaseExample, but that class is not necessarily Example.

Let's say you have another class Example2 below.  The statement 'Example2 tmp = new Example();' is not valid, because "Cannot implicitly convert type 'Example' to 'Example2'".  That's similar to what you said with "T tmp = new Example()".

public class Example2 : IExample
{
    public string Hello { get; set; }
}

Does it make sense?
0
 
LVL 5

Author Closing Comment

by:KiasChaos83
ID: 34923145
The fog has cleared. Cheers.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 34923149
In the case of generics, you can think of T as a "variable" that's replaced by the actual class.

For example, SolidClass<Example2> will "translate" into:

public class SolidClass<Example2>
{
     public SolidClass()
    {
          Example2 tmp = new Example();
     }
}

For this reason the construction is not valid.

This is not how it happens internally.  I put it here for illustration only.

The correct syntax would be:

public class Example : IExample
{
    public string Hello { get; set; }
}

public class SolidClass<T> where T : IBaseExample
{
     public SolidClass()
    {
          T tmp = new T();
     }
}

Open in new window

0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question