第九节 C# 可选参数、重构—提起方法、局部变量、作用域、常用字符串的处理方法

View Code
案例1:C# 可选参数

 static void Main(string[] args)
        {
            /*C#4.0在方法中提供了可选参数,为方法中的参数赋值
             * 该参数就是一个可选参数。如果我们在调用方法的时候不给可选
             * 参数传参,那么就意味着使用可选参数的默认值否则将对
             * 可选参数的重新赋值使用实参的值*/
            double d = Vc(3);
            Console.WriteLine(d);
           d=Vc(3, 4);
            Console.WriteLine(d);
            d=Vc(3, 4, 5);
            Console.WriteLine(d);
            Console.ReadKey();

        }
        /// <summary>
        /// 计算圆柱的体积
        /// </summary>
        /// <param name="r">半径</param>
        /// <param name="h"></param>
        /// <param name="PI">PI</param>
        /// <returns>返回体积</returns>
         //可选参数
        static double Vc(double r, double h = 1, double PI = 3.14)
        {
            return PI * r * r * h;
        }

——————————————————————————————————————————————————————————

案例2:重构—提起方法

class Program
    {
        /// <summary>
        /// 重构——提起方法!
        /// 选中代码—右键
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int num1 = 5;
            int num2 = 10;
            Console.WriteLine("调用前:{0},{1}",num1,num2);
            Swap(ref num1, ref num2);
            Console.WriteLine("调用前:{0},{1}",num1,num2);
            Console.ReadKey();

        }

        private static void Swap(ref int num1, ref int num2)
        {
            int temp;

            temp = num1;
            num1 = num2;
            num2 = temp;
        }
    }

————————————————————————————————————————————————————————————

案例3:局部变量


        /*在方法中定义的变量称为局部变量,作用域范围在这个
         * 方法的左大括号和右大括号之间,不能在同一个方法
         * 体内定义重名的变量名称,但是可以在不同的方法中
         * 定义名称相同的变量,因为他们的作用域不同
         * */
        
        
        static void A()
        {
            int x = 10;
            Console.WriteLine("A方法中的x:{0}", x);
        }

        static void B()
        {
            int x = 20;
            Console.WriteLine("B方法中的x:{0}",x);
        }
        static void Main(string[] args)
        {
            A();
            B();
            int x = 30;
            Console.WriteLine("Main方法中的x:{0}", x);
            Console.ReadKey();
        }

 

——————————————————————————————————————————————————————

案例4:作用域

static void Main(string[] args)
        {
           /*如果在程序的代码(如:while 循环的左大括号和右大括号之间)
            * 定义了一个变量,那么这个变量只能在该作用域范围中使用
            * */
            
            int i = 0;
            while (i < 5)
            {
                int s = 5;
            }

            if (i<4)
            {
                int t = 5;//只能在if的{.......}之间被访问
            }

 

        }

————————————————————————————————————————————————————————

案例5:Equals()、Join(),Split(),ndexOf()方法

static void Main(string[] args)
        {
            string str1 = "hello world";
            string str2 = "hello world";
            /*字符串的Equals方法用于比较两个字符串是否相等
             * 该方法返回一个bool值,如果相等返回true,反之亦然*/
            Console.WriteLine("str1和str2是否相等:{0}", str1.Equals(str2));


            //把一个数组中的元素用你所指定的分隔符连接起来
            //返回一个字符串
            string[] str ={ "张三", "李四", "王五", "赵六", "田七" };
            String joinstr = string.Join("||", str);
            Console.WriteLine(joinstr);


            /*Split 方法,将一个字符串通过特定的符号进行分割,
            *该方法的返回值是一个字符串的组*/
            string str3 = "张三,李四,王五,赵六,田七";
            string[] name = str3.Split(',');
            Console.WriteLine("name数组的长度为:" + name.Length);
            for (int i = 0; i < name.Length; i++)
            {
                Console.WriteLine("第{0}个元素是:{1}", (i + 1), name[i]);
            }

            //indexOf方法,用于在字符串中查找某个字符所在的位置
            string str4 = "hello world!";
            int index = str4.IndexOf('w');
            Console.WriteLine("索引为:{0}", index);
            //假设在字符串中所搜索的字符不存在该字符中
            index = str4.IndexOf('t');
            if (index == -1)
            {
                Console.WriteLine("不能在str字符串中找到相应的字符:'t'");
            }

            else
            {

                Console.WriteLine("索引为:{0}", index);
            }
            Console.ReadKey();


        }

————————————————————————————————————————————————————

案例:比较两个数的大小,返回1或-1

class Program
    {

        static void Main(string[] args)
        {
            Console.Write("请输入num1:");
            int num1 = int.Parse(Console.ReadLine());
            Console.Write("请输入num2:");
            int num2 = int.Parse(Console.ReadLine());

            int d = dx(num1, num2);
            Console.WriteLine(d);
            Console.ReadKey();


        }

        static int dx(int num1, int num2)
        {

            if (num1 > num2)
            {
                return 1;

            }
            else if (num1 == num2)
            {
                return 0;

            }
            else
            {
                return -1;

            }

        }

 

    }

 

posted @ 2012-06-20 10:37  ComBat  阅读(266)  评论(0编辑  收藏  举报