Solved

Am I misunderstanding polymorphism in c#?

Posted on 2011-02-17
3
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

615 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