You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expression-bodied members were a great addition to C#6.
Expanding from that feature and syntax, being able to simplify the common pattern of a class implementing an interface by containing an instance of another class that implements that field would be very helpful. Similarly to with automatic properties, at compile time expand this to common properties and methods directly calling the corresponding properties and methods on the internal field.
Example:
public interface IAnimationPlayer
{
bool IsPlayingAnimation { get; }
void PlayAnimation(AnimationData animationData);
}
public class AnimationPlayer :
IAnimationPlayer
{
public bool IsPlayingAnimation { get; private set; }
public void PlayAnimation(AnimationData animationData)
{
// Do work to play animation...
IsPlayingAnimation = true;
}
}
public class Actor :
IAnimationPlayer,
IOtherActorInterface
{
// IAnimationPlayer interface implementation is
// delegated to private field
public IAnimationPlayer => animationPlayer;
public Actor(IAnimationPlayer animationPlayer)
{
this.animationPlayer = animationPlayer;
}
// Implement additional functionality...
private readonly IAnimationPlayer animationPlayer;
}
With Actor compiling to:
public class Actor :
IAnimationPlayer,
IOtherActorInterface
{
public bool IsPlayingAnimation
{
get
{
return animationPlayer.IsPlayingAnimation;
}
}
public void PlayAnimation(AnimationData animationData)
{
animationPlayer.PlayAnimation(animationData);
}
public Actor(IAnimationPlayer animationPlayer)
{
this.animationPlayer = animationPlayer;
}
// Implement additional functionality...
private readonly IAnimationPlayer animationPlayer;
}
The text was updated successfully, but these errors were encountered:
Expression-bodied members were a great addition to C#6.
Expanding from that feature and syntax, being able to simplify the common pattern of a class implementing an interface by containing an instance of another class that implements that field would be very helpful. Similarly to with automatic properties, at compile time expand this to common properties and methods directly calling the corresponding properties and methods on the internal field.
Example:
With Actor compiling to:
The text was updated successfully, but these errors were encountered: