Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: allow anonymous types to implement interfaces #2016

Closed
claq2 opened this issue Apr 15, 2015 · 2 comments
Closed

Proposal: allow anonymous types to implement interfaces #2016

claq2 opened this issue Apr 15, 2015 · 2 comments
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue

Comments

@claq2
Copy link

claq2 commented Apr 15, 2015

The most immediate benefit would be for unit testing. For a simple DTO-type interface, i.e. only containing properties, I would like to type something like:

var dto = new { FirstName = "Joey", LastName = "Shabadoo", Valid = true} : IPersonDto;
var person = new Person(dto); // Person's cctor takes an IPersonDto
Assert.That(person.FullName, Is.EqualTo("Joey Shabadoo");

or

Save(dto); // Checks Valid property

then it would save a few lines of setting up a mock or creating a test class.

The anonymous type would only implement the getters of the interface's properties even if they are writable. Compilation would still fail if you try to set values on the anonymous type, and throw an exception if you try to set values using a reference to the interface.

@svick
Copy link
Contributor

svick commented Apr 15, 2015

This is a duplicate of #13, only with different syntax.

@dsaf
Copy link

dsaf commented Aug 27, 2015

Specifying the implemented interface in the very end is not C# idiomatic.

@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Sep 13, 2015
@gafter gafter closed this as completed Sep 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Language Design Feature Request Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

4 participants