[Proposal]: Add the composition capability to C# #7406
Replies: 4 comments 6 replies
-
@ufcpp thanks for that link. Its syntax is however not a simple syntax and it does not provide a method to compose a type, rather than type members. Let me give you an example of my real requirement.
Now I want to create a In other words, the best option is to have this code:
And this inclusion can be a very simple file-based textual inclusion. In other words, the content of the |
Beta Was this translation helpful? Give feedback.
-
The experience isn't where it should be when it comes to composition in C# but it's not like it's forbidden you just need to make a bit work to implement it.
These statements 🤦 I mean inheritance is just as powerful so it really depends on the problem you're trying to solve, advocating to use composition just because say in React it makes more sense doesn't mean everything should be constructed like this, you can see insane amount of examples in .NET, Roslyn and pretty much anywhere where inheritance was the right tool for the job, I'm not saying that composition is not awesome where it's appropriate and applicable but it's a tool to express a certain relationship and that's how we should look at it, comparing it to inheritance and say that it's more powerful is misleading. Blazor components that are written in Razor support similar composition that exists in React. private string? Color { get; set; }
private RenderFragment? ChildContent { get; set; }
private RenderFragment FancyBorder()
{
return @<div class=@($"FancyBorder FancyBorder-{Color}")>
@ChildContent
</div>;
} |
Beta Was this translation helpful? Give feedback.
-
Hello
I've been working with C# for more than a decade now. And I was happy.
Yet after learning React and feeling firsthand how Composition over inheritance is much more powerful, now I wish C# had composition too.
I can start a story for why it's amazing, but since it's just a discussion, I first wait to see what others might say. Maybe this discussion won't even be noticed at all.
Beta Was this translation helpful? Give feedback.
All reactions