Is the new feature of C# 4.0 - “Optional Parameters” CLS-Compliant?

后端 未结 3 792
生来不讨喜
生来不讨喜 2021-02-05 02:51

This new feature is really convenient.

Lately I read the document of the \"Microsoft All-In-One Code Framework\", and it mentions that \"Optional Parameters\" is not CLS

3条回答
  •  情书的邮戳
    2021-02-05 03:23

    I interpret your question to be about Optional Arguments.

    If so then I believe they are CLS-Compliant and you can check by using the CLSCompliant attribute:

    using System;
    
    [assembly: CLSCompliant(true)]
    
    namespace ConsoleApplication1
    {
        public class Program
        {
            public static int Test(int val=42)
            {
                return val;
            }
    
            static void Main(string[] args)
            {
                System.Console.WriteLine(Test());
            }
        }
    }
    

    This compiles with no warnings.

提交回复
热议问题