C# 3.5 Optional and DefaultValue for parameters

前端 未结 3 2011
迷失自我
迷失自我 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:45

    Optional parameters are C# 4.0 language feature so it doesn't matter which framework you are targeting, but you have to compile it using VS 2010 or newer.

    Use this syntax in VS 2010 or newer:

    public static void MethodName(string name, string placeHolder = null)
    {
        // body
    }
    

    Or this in older one:

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

提交回复
热议问题