I suggest you ...

Generic this

Please add support for a generic 'this', to use in (generic / abstract) types or interfaces.

The "this" would reflect the current type and is used as an generic constraint. This would create new cool possibilities with C#.

For example, the `ICloneable` interface has one method (Clone), which returns an `Object`. This is undesirable because:

- We need to cast the result (most of the times) - bad for the performance and could lead to run-time errors.
- We could implement the Clone method more easiliy wrong: the Clone method could return the wrong type (not compile time enforced)
- We could "forget" to implement the Clone method in subclasses. (example below)

The improved `ICloneable` interface would look like:

interface ICloneable2
where TThis: this //we mark TThis as the 'this' type
{

TThis Clone();
}

Example usage:

class Bar : ICloneable2
{
Bar Clone()
{.. }
}

In subclasses, we also need to implement this, so the Clone method would return the correct type:

class FooBar : Bar
{
//we need to implement the ICloneable2 correct
FooBar Clone()
{
..
//possible to implement like: (FooBar) base.Clone();
}
}

For more power, the TThis also should get restricted by other generic contraints

interface ICloneable2
where TThis : this, IOptionalRestrictions
{
TThis Clone();
}

57 votes
Vote
Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
You have left! (?) (thinking…)
Julian Verdurmen shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

3 comments

Sign in
(thinking…)
Sign in with: facebook google
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base