C# Default Parameters

前端 未结 4 1342
闹比i
闹比i 2020-12-30 08:32

This is, probably, a very simple answer for someone. I have a method with an Optional Parameter like so;

public static Email From(string emailA         


        
相关标签:
4条回答
  • 2020-12-30 09:11

    Optional parameters have been supported in the CLR since 1.0. Languages like VB.Net's have been using them since the start. While the first version of C# to support them is 4.0, it can still generate valid code for a 2.0 CLR and in fact does so. Hence you can use default parameters in 2010 if you are targeting the 3.5 CLR (or 2.0, 3.0, etc ...)

    This type of support is not limited to default parameters. Many new C# features can be used on older version of the framework because they do not rely on CLR changes. Here are a few more which are supported on CLR versions 2.0 and above

    • Named Arguments: Added C# 4.0
    • Lambda Expressions: Added C# 3.0
    • Auto Properties: Added C# 3.0
    • Extension Methods: Added C# 3.0
    • Co/Contra Variance: Added C# 4.0
    0 讨论(0)
  • 2020-12-30 09:21

    If you compile that up and examine the output using a tool like ILDASM you'll see that the optional parameter is simply implemented by adding an attribute to the metadata that describes the method's formal parameters. As long as that attribute class is available on the target platform there should be no problem with using the emitted code on a downlevel platform.

    0 讨论(0)
  • 2020-12-30 09:22

    C# 4.0 is included with Visual Studio 2010, and the C# compiler understands optional parameters. So yes, the C# 4.0 language definition is something different than .NET 4.0. I guess a method with optional parameters compiled for .NET 3.5 will show overloaded methods when opening with eg. VS 2008

    0 讨论(0)
  • 2020-12-30 09:26

    The language version is independent of the framework version. For C# they happen to run mostly in parallel, i.e. C# 4 and framework 4.0 came with Visual Studio 2010. (However there is no 3.5 version of C#.)

    With VB the version numbers are different, as VB 7 was the first VB.NET version. So, VB 10 comes at the same time as framework 4.0.

    The optional parameters is a language feature introduced in C# 4. When you use VS 2010 you use C# 4, even if you target framework 2.0, so you can use all C# 4 features.

    0 讨论(0)
提交回复
热议问题