结构体,数组,方法的重载,ref,out

1. 结构体,数组。

namespace Struct_array结构体数组
{
    //结构体的声明,关键字是Struct。
    struct people
    {
       public char sex; //结构体里面不但能存放属性,还能存放方法。
       public int age;  //如果属性不用public修饰,外面就不能访问该属性。
       public void sayHello() { Console.WriteLine("hello,Struct!"); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            people Jim;  //使用结构必须先声明变量。
            Jim.age = 11;
            Jim.sex='';
            Jim.sayHello();  //结构体里面的方法调用
            Console.WriteLine("Jim 今年"+Jim.age+"岁了。");
            Program ss = new Program();
            ss.arrayexpra();
            Console.ReadKey();
        }
       
       public void arrayexpra() 
       {
            int[] nums;                
            nums = new int[] { 1, 2, 3 };  
            int[] names=new int[10]; //声明一个最大长度为十的数组;
            int[] ages = { 3,2,1,4,5,8,7}; //直接声明一个数组。 
            Array.Sort(ages);  //对数组排序           
            for (int i = 0; i < ages.Length; i++)
            {
                Console.WriteLine(ages[i]);
            }
            Array.Reverse(ages); //反转一个数组
            for (int i = 0; i < ages.Length; i++)
            {
                Console.WriteLine(ages[i]);
            }    
        }
    }
}


 

2.函数。

namespace 方法的重载
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine(p.fun("1",2));

            int i,j=100;

            p.funOut(2, 4, out i);  //out参数方法的调用,i可以不付初始值。
            Console.WriteLine(i);

            p.funref(2, 4, ref j); //ref参数方法的调用,j不可以不付初始值。
            Console.WriteLine(j);

            Console.ReadKey();
        }

        int fun(string i, int c) //方法的重载,就是一样名字的两个不同方法。这样做为了方便程序远记忆繁杂的函数名称。
        {
            return Convert.ToInt32(i) + c;
        }
        string fun(int r, int c, int b)
        {           
            return (r + c + b).ToString();
        }

        void funOut(int r, int c, out int b)//out参数方法,b必须赋值。
        {
            b = 100;                  
            b = r + c + b;
        }
        void funref(int r, int c, ref int b)  //ref参数方法
        {           
            b = r + c + b;
        }

    }
}
原文地址:https://www.cnblogs.com/knowledgesea/p/2502684.html