Solved

Am I misunderstanding polymorphism in c#?

Posted on 2011-02-17
3
259 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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

776 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