一个简单的例子来演示:

    public static class StringExtender
    {
        /// <summary>
        /// 字符串拼接
        /// </summary>
        /// <param name="item">原字符串</param>
        /// <param name="result">需要拼接的字符串</param>
        /// <param name="word">连接字符或字符串</param>
        /// <returns>结果</returns>
        public static string AppendWord(this string item, string result, string word = "_")
        {
            return item + word + result;
        }
    }

Program.cs

    class Program
    {
        static void Main(string[] args)
        {
            string word = "Hello";

            Console.WriteLine("默认参数:" + word.AppendWord("World"));
            Console.WriteLine("指定参数:" + word.AppendWord("World", "$"));
            Console.ReadKey();
        }
    }

编译后,我们Reflector一下。

Reflector后,我们的StringExtender:

.NET 4.0 里的默认参数原来是编译器搞的鬼啊

Reflector后,我们的Main函数:

.NET 4.0 里的默认参数原来是编译器搞的鬼啊

亲,最后要注意哦,由于默认参数是编译器搞的鬼,编译时帮你插入参数的,所以最好保证编译版本一致,即要同时更新引用的 DLL,并且重新编译目标项目。

谢谢浏览!

相关文章:

  • 2022-01-16
  • 2021-09-06
  • 2022-12-23
  • 2022-02-19
  • 2021-05-23
  • 2022-12-23
  • 2022-01-28
  • 2022-01-05
猜你喜欢
  • 2021-07-09
  • 2021-09-24
  • 2022-12-23
  • 2021-07-27
  • 2022-02-27
  • 2022-01-04
  • 2022-12-23
相关资源
相似解决方案