Solved

Am I misunderstanding polymorphism in c#?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now