Solved

Am I misunderstanding polymorphism in c#?

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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