Skip to content

with expression + interfaces #4158

Answered by Joe4evr
JanEggers asked this question in Q&A
Discussion options

You must be logged in to vote

You can't right now.

For the next version of C#, the language team intends to add [Factory]: an attribute that indicates a method return is always a fresh instance, so it's okay to put an object initializer after such a call. If you then define a method named With that takes no parameters and returns the same object type, the compiler will use that pattern to allow with as a keyword on instances of that object.

public interface IMyModel
{
    [Factory]
    IMyModel With();
}

See here.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@333fred
Comment options

Answer selected by JanEggers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants