5.6. 方法(1) (2)

声明方法的语法

 

成员访问修饰符 返回值 方法名称(参数列表)

{
    // 方法的内容
}

 

// 不带参数的方法

using System;
namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Now time is {0}", CurrentTime());
        }
        public static DateTime CurrentTime()  // DataTime 是一种数据类型
        {
           return DateTime.Now;  // 这里Now是属性,因为不带括号,如果带括号,则为方法
        }
    }
}

// 带有参数的方法

 

using System;
namespace test
{
    class Method
    {
        static void Main(string[] args)
        {
            string tempName = ""; //初始化
            while (tempName != "end")
            {
                tempName = Console.ReadLine();
                MyMehod(tempName);
            }
        }
        public static void MyMehod(string strName)
        {
            Console.WriteLine("The name is " + strName + "/n");  // 为了显示清楚,is后面有空格
        }
    }
}

 

 

                                   共享变量
如果两个以上的方法要共享一个变量,则要将此变量声明在类(Class)阶层。

 

namespace test
{
    class vary
    {
        class Share
        {
            public static int i=0;
            public void Getvalue()
            {
                i=i+1;
            }
        }
        static void Main()
        {
            Share sh1 = new Share();
            sh1.Getvalue();
            Share sh2 = new Share();
            sh2.Getvalue();
            Console.WriteLine(Share.i);  // 访问静态变量的名称,可用类名.变量名形式。
        }
    }
}

 

 

                                                           方法的参数传递机制
1 值参数(Vaule Parameter)
   方法名称(参数类型 参数名称[,参数类型 参数名称])
2  引用参数(Reference Parameter)
  方法名称(ref 参数类型 参数名称[, ref 参数类型 参数名称])
3  输出参数(Output Parameter)
   方法名称(out 参数类型 参数名称[, out 参数类型 参数名称])

 

using System;
namespace Method
{
    class MyMethod
    {
        public static void ValueMethod(int i)
        {
            i++;
        }
        public static void ReferenceMethod(ref int i)
        {
            i++;
        }
        public static void OutputMethod(out int i)
        {
            i = 0;  //在输出参数调用,变量必须赋初值
            i++;
        }
        static void Main()
        {
            int i = 0;
            ValueMethod(i);
            Console.WriteLine("i=" + i);
            int j = 0;
            ReferenceMethod(ref j);   // 参数必须写上ref
            Console.WriteLine("j=" + j);
            int k = 0;
            OutputMethod(out k);
            Console.WriteLine("k=" + k);
        }
    }  
}

 

 

 

 //用数组来实现,思考运行结果为什么会变化

using System;
namespace MyMethod
{
    class Method
    {
        static void PrintArray(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                arr[i] = i;
            }
        }

        static void Main()
        {
            int[] arr1 ={ 10, 20, 30 };
            PrintArray(arr1);
            foreach (int i in arr1)
            {
                Console.Write(i + "  ");
            }
            Console.WriteLine();
        }
    }
}


//字符串传递, 不明白为什么运行结果没有变化


using System;
namespace MyMethod
{
    class Method
    {
        static void PrintStr(string str)
        {
            str = "123";
        }

        static void Main()
        {
            string s = "789";
            PrintStr(s);
            Console.WriteLine(s);
        }
    }
}

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值