C# 3.5 Optional and DefaultValue for parameters

前端 未结 3 2013
迷失自我
迷失自我 2021-02-12 16:15

I am using C# .net 3.5 to build an application. I have been working with optional parameter attributes in .net 4.0 with no problems. I did notice that with 3.5 there is the opti

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 16:39

    The Optional attribute has been available since C# 1.0, and is used when interoperating with external code, it has no effect on method calls in your own code.

    As there is no optional parameters in C# 3, you can use overloading instead:

    public static void MethodName(string name, string placeHolder) {
      ...
    }
    
    public static void MethodName(string name) {
      MethodName(name, null);
    }
    

    (Side note: There is no C# version 3.5, that is a framework version.)

提交回复
热议问题