C# 类设计

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayTest
{
    class Program
    {
        static void Main(string[] args)
        {
            int age = 3;
            Growth(age);
            //Growth(int age=7);不能再调用实参的时候定义变量,因为无法确定变量属于谁
            Growth(ref age);//实参调用的时候要加关键字ref
            int ly, ny;//可以没有初值
            Growth(age,out ly,out ny);
            Console.WriteLine("age ={0},ly = {1},nx = {2}", age,ly,ny);
            Growth1();//如果没有实参,形参就默认为5
            Growth1(age);//如果给实参,形参就接受实参的值
        }

        //定义形参可以给一个默认值
        //如果有多个参数,默认参数必须在后面
        static void Growth1(int age=5)
        {
            age++;
            Console.WriteLine("age is method growth is:{0}", age);
        }

        //方法参数传递变成引用传递(传的是地址)形参和实参
        static void Growth(ref int age)
        {
            age++;
            Console.WriteLine("age is method growth is:{0}", age);
        }

        //out关键字表示形参的值,在各方面调用完成后,传回给实参
        static void Growth(int age,out int lastyear,out int nextyear)
        {
            lastyear = age - 1;
            nextyear = age + 1;
            Console.WriteLine("age is method growth is:{0}", age);
        }

        static void Growth(int age)
        {
            age++;
            Console.WriteLine("age is method growth is:{0}", age);
        }
    }
}

 

你可能感兴趣的:(C#语言)