C#学习记录

做个记录,以后看,student为自定义的类

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

namespace TestCSharp
{
    class student:IEnumerable
    {
       int d = 200;//普通变量
       public static int e = 300;//静态变量
       public  const int c = 100;//常量字段必须初始化赋值,const修饰的值,常量本身就是静态了,
        //在编译时值就确定了,因此必须初始化
       public readonly int f;//只读字段可以在运行时赋值决定
       public delegate void studentHandler(object sender, EventArgs e);//委托的声明
       public event studentHandler dohomework;//委托事件

       public void callEvent() 
       {
           dohomework(this,null);//触发事件
       }
       public void show()
       {
            Console.WriteLine("{0}", d);
       }
       //实现student可以枚举,通过System.Array来实现IEnumerator
       private int[] array = new int[]{1,2,3,4};
       public IEnumerator GetEnumerator() 
       {
          // return array.GetEnumerator();
           foreach(int i in array)
           {
               yield return i;
           }
       }
       public student() 
       {
           f = 200;
       }
      
 

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

namespace TestCSharp
{
    class Program
    {
       
        static void Main(string[] args)
        {
           
            student s = new student();
            s.dohomework += s_dohomework;
            s.callEvent();

        }

        static void s_dohomework(object sender, EventArgs e)
        {
            Console.WriteLine("helloworld");
        }

        //out修饰符,如果方法定义了输出参数,就必须在退出方法之前为这个参数赋一个有效值。
        static void add(int x, int y, out int ans) 
        {           
            ans = x + y;               
        }
        //ref修饰符,将值类型进行引用参数传递,引用参数必须在它们被传递给方法之前初始化。
        static void swap(ref int a, ref int b) 
        {
            int temp;
            temp = a;
            a = b;
            b = temp;
        }

     
    }
}
原文地址:https://www.cnblogs.com/guoyuanwei/p/2517088.html