C#4.o 特性

using System;

namespace C4特性
{
    public class FeatureOne
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }

    class Program
    {
        //dynamic
        public static void DemoOne()
        {
            //在c#中使用js动态语言的特性,前提是将一个变量声明为FeatureOne类型。
            dynamic o = new FeatureOne { Key = "key1", Value = "value1" };
            Console.WriteLine(string.Format("demo key is {0} and value is {1}", o.Key, o.Value));
            Console.ReadLine();
        }

        //可选参数
        //value1默认值为value1  value2默认值为value2   调用时如果没有传此参数  则使用默认值
        public static void DemoTwo(string name, string value1 = "value1", string value2 = "value2")
        {
            Console.WriteLine(string.Format("demo key is {0} and value1 is {1} and value2 is {2}", name, value1,value2));
            Console.ReadLine();
            
        }

        //重载了方法DemoTwo  如果有个和可选参数方法某个参数相同的方法签名的方法时会优先执行没有可选参数的方法
        public static void DemoTwo(string name, string value)
        {
            Console.WriteLine(string.Format("demo key is {0} and value is {1}", name, value));
            Console.ReadLine();

        }

        //命名参数
        public static void DemoThree(int id, string content)
        {
            Console.WriteLine(string.Format("demo key is {0} and value1 is {1}", id, content));
            Console.ReadLine();

        }

        static void Main(string[] args)
        {
            //DemoOne();

            //DemoTwo("it's");
            //DemoTwo("it's","it's value");
            //DemoTwo("it's","who");

            //调用时参数名字需要与方法中的参数名字一致  id content
            DemoThree(id: 1, content: "i am conten");
        }
    }
}

 

posted @ 2012-11-03 01:48  sidihu  阅读(196)  评论(0编辑  收藏  举报