I'm browsing through the EF7 code on Github and found a line that looks like this:
public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class => _setInitializer.Value.CreateSet<TEntity>(this);
I have seen that syntax before on a class level, like this:
public class SomeClass<T> where T : class
Which says that T
should be of type class. But the line from the EF7 source confuses me. I'm not sure what it does.
It's an expression-bodied member, a new syntax in C# 6.
It's a method, not a property. C# doesn't allow generic properties.
It's the same as
public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class
{
return _setInitializer.Value.CreateSet<TEntity>(this);
}
This syntax is a bit confusing indeed, but actually the lambda construct here has nothing to do with generic constraints. It's just an Expression-Bodied Method that happened to have a generic constraint.
You can think of it as:
public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class
{
return _setInitializer.Value.CreateSet<TEntity>(this);
}
See Roslyn Wiki
It's a C# 6.0 feature called Expression Bodied Method.
Read here about it.
The code is equivalent to:
public virtual DbSet<TEntity> Set<TEntity>() where TEntity : class
{
return _setInitializer.Value.CreateSet<TEntity>(this);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With