Usage of keywords "in" and "out" in C#(.NET 4.0)
2011-09-29 15:55
369 查看
Variance will only be supported in a safe way - in fact, using the abilities that the CLR already has. So the examples I give in the book of trying to use a
Firstly, it will only be supported for interfaces and delegates.
Secondly, it requires the author of the interface/delegate to decorate the type parameters as
Contravariance is harder to give concrete examples for using interfaces, but it's easy with a delegate. Consider
Hope this clears it up a bit - please let me know if it doesn't make sense!
referenced from: http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-4-0
List<Banana>as a
List<Fruit>(or whatever it was) still won't work - but a few other scenarios will.
Firstly, it will only be supported for interfaces and delegates.
Secondly, it requires the author of the interface/delegate to decorate the type parameters as
in(for contravariance) or
out(for covariance). The most obvious example is
IEnumerable<T>which only ever lets you take values "out" of it - it doesn't let you add new ones. That will become
IEnumerable<out T>. That doesn't hurt type safety at all, but lets you return an
IEnumerable<string>from a method declared to return
IEnumerable<object>for instance.
Contravariance is harder to give concrete examples for using interfaces, but it's easy with a delegate. Consider
Action<T>- that just represents a method which takes a
Tparameter. It would be nice to be able to convert seamlessly use an
Action<object>as an
Action<string>- any method which takes an
objectparameter is going to be fine when it's presented with a
stringinstead. Of course, C# 2 already has covariance and contravariance of delegates to some extent, but via an actual conversion from one delegate type to another (creating a new instance) - see P141-144 for examples. C# 4 will make this more generic, and (I believe) will avoid creating a new instance for the conversion. (It'll be a reference conversion instead.)
Hope this clears it up a bit - please let me know if it doesn't make sense!
referenced from: http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-4-0
相关文章推荐
- keywords between managed c++ and c# in dotNet 2.0
- Delegates and Events in C# / .NET
- Searching and Navigating Code in VS 2010 (VS 2010 and .NET 4.0 Series)
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- .NET Versioning and Multi-Targeting - .NET 4.5 is an in-place upgrade to .NET 4.0
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- Pro .NET 2.0 Windows Forms and Custom Controls in C#
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- New Features in C# 3.0, 4.0 and 5.0 (英文差的免入)
- How to Determine if a file is a .Net assembly (in Delphi and C#)
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- Delegates and Events in C# / .NET
- Applying Domain-Driven Design and Patterns(ADDDP) With examples in C# and .NET
- Gene Expression Programming (GEP) in C# and .NET
- Professional ASP.NET 3.5: In C# and VB
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData
- Using SiteMap and MasterPages to set META Tags in ASP.NET and C#
- Professional ASP.NET 3.5: In C# and VB
- ASP.NET MVC 2.0 in Vs2010 :使用C# 4.0中使用动态类型来传递ViewData