刘步垚吧 关注:3贴子:443
  • 9回复贴,共1

C#代码记录

只看楼主收藏回复

Day01 委托
using System;
namespace Delegate
{
public class Greet
{
public delegate void GreetDelegate(string name);
public static void Greeting(string name,GreetDelegate gd)
{
gd (name);
}
public void ChineseGreeting(string name)
{
Console.WriteLine ("你好,"+name);
}
public void EnglandGreeting(string name)
{
Console.WriteLine ("Hello,"+name);
}
}
class MainClass
{
public delegate void DelegateChain();
//public delegate void MyDelegate(int para1,int para2);
public static void Main (string[] args)
{
MainClass mc = new MainClass ();
DelegateChain d1 = new DelegateChain (MainClass.staticMethod);
DelegateChain d2 = new DelegateChain (mc.method);
DelegateChain chain = null;
chain += d1;
chain += d2;
chain ();
Greet greet = new Greet ();
Greet.Greeting ("张三",greet.ChineseGreeting);
Greet.Greeting ("LISI",greet.EnglandGreeting);
Greet.Greeting ("Killer",new Greet.GreetDelegate(greet.EnglandGreeting));
/* MyDelegate d;
d = new MyDelegate (new MainClass().add);
MyDelegateMethod (d);*/
//Console.WriteLine ("Hello World1!");
}
/*public void add(int a,int b)
{
Console.WriteLine ("a+b={0}",a+b);
}
public static void MyDelegateMethod(MyDelegate mydelegate)
{
mydelegate(23,12);
}*/
public static void staticMethod()
{
Console.WriteLine ("static method");
}
public void method()
{
Console.WriteLine ("instance method");
}
}
}


1楼2018-01-14 19:28回复
    Day02 事件机制
    using System;
    namespace TEXT2
    {
    public class BridgeGroom
    {
    public delegate void MarryHandler(string msg);
    public event MarryHandler marryevent;
    public void SendMarriageMsg(string msg){
    if (marryevent != null) {
    marryevent(msg);
    }
    }
    }
    public class Friend
    {
    public string name;
    public Friend (string _name)
    {
    name = _name;
    }
    public void RecieveMsg(string msg)
    {
    Console.WriteLine (this.name+"收到消息");
    Console.WriteLine (msg);
    }
    }
    class MainClass
    {
    public static void Main (string[] args)
    {
    BridgeGroom b =new BridgeGroom();
    Friend f1 = new Friend ("z3");
    Friend f2 = new Friend ("L4");
    Friend f3 = new Friend ("w2");
    b.marryevent += new BridgeGroom.MarryHandler (f1.RecieveMsg);
    b.marryevent += new BridgeGroom.MarryHandler (f2.RecieveMsg);
    b.marryevent += new BridgeGroom.MarryHandler (f3.RecieveMsg);
    b.SendMarriageMsg ("我要结婚了");
    Console.WriteLine ("Hello World!");
    }
    }
    }


    2楼2018-01-14 20:20
    回复
      广告
      立即查看
      Day 03 值类型&引用类型
      using System;
      namespace Program
      {
      public class A
      {
      public int num;
      }
      class MainClass
      {
      public static void Main (string[] args)
      {
      A a = new A ();
      a.num =1;
      mul (a);
      Console.WriteLine ("a :"+a.num);
      /*int num = 233;
      add (num);
      Console.WriteLine ("num :"+num);*/
      }
      /*public static void add(int addNumber)
      {
      addNumber = addNumber + 1;
      Console.WriteLine ("addNum: "+addNumber);
      }*/
      public static void mul(A a)
      {
      a.num = a.num * 7;
      Console.WriteLine ("a.num : "+a.num);
      }
      }
      }


      3楼2018-01-16 22:44
      回复
        Day 04 泛型
        using System;
        namespace Program
        {
        public class CompareClass<T> where T : IComparable
        {
        public static T CompareValue(T value1,T value2)
        {
        if (value1.CompareTo (value2) > 0) {
        return value1;
        } else {
        return value2;
        }
        }
        }
        class MainClass
        {
        public static void Main (string[] args)
        {
        int result = CompareClass<int>.CompareValue (32,56);
        string resultStr = CompareClass<string>.CompareValue ("killer","Near");
        Console.WriteLine ("result ={0} : ",result);
        Console.WriteLine ("resultStr ={0} : ",resultStr);
        }
        }
        }


        4楼2018-01-17 22:11
        回复
          using System;
          using System.Collections;
          using System.Collections.Generic;
          using System.Diagnostics;
          namespace Program
          {
          class MainClass
          {
          public static void TestGeneric1()
          {
          Stopwatch sw = new Stopwatch ();
          ArrayList arrayList = new ArrayList ();
          sw.Start ();
          for (int i = 0; i < 1000000; i++) {
          arrayList.Add (i);
          }
          sw.Stop ();
          TimeSpan ts = sw.Elapsed;
          string time = string.Format ("{0:00},{1:00},{2:00},{3:00}",ts.Hours,ts.Minutes,
          ts.Seconds,ts.Milliseconds);
          Console.WriteLine ("time1 : " +time);
          }
          public static void TestGeneric()
          {
          Stopwatch stopwatch = new Stopwatch ();
          List<int> list = new List<int> ();
          stopwatch.Start ();
          for (int i = 0; i < 1000000; i++) {
          list.Add (i);
          }
          stopwatch.Stop ();
          TimeSpan ts = stopwatch.Elapsed;
          string time = string.Format ("{0:00},{1:00},{2:00},{3:00}",ts.Hours,ts.Minutes,
          ts.Seconds,ts.Milliseconds);
          Console.WriteLine ("time : " +time);
          }
          public static void Main (string[] args)
          {
          MainClass.TestGeneric ();
          MainClass.TestGeneric1 ();
          //Console.WriteLine ("resultStr ={0} : ");
          }
          }
          }


          5楼2018-01-17 22:38
          收起回复
            Day 05 泛型约束
            using System;
            using System.Collections;
            using System.Collections.Generic;
            using System.Diagnostics;
            namespace Program
            {
            class MainClass
            {
            public class SingleTon<T>where T:new()
            {
            private static T instance;
            public static T getInstance(){
            if (instance == null)
            {
            instance = new T ();
            }
            return instance;
            }
            public static T Instance
            {
            get
            { if (instance == null)
            {
            instance = new T ();
            }
            return instance;
            }
            }
            public void Print()
            {
            Console.WriteLine ("666");
            }
            }
            public class Person :SingleTon<Person>
            {
            public void PersonPrint()
            {
            Console.WriteLine ("233");
            }
            }
            public class Animal :SingleTon<Animal>
            {
            public void AnimalPrint()
            {
            Console.WriteLine ("999");
            }
            }
            public static void Main (string[] args)
            {
            Person.getInstance ().PersonPrint ();
            Person.getInstance ().Print ();
            Animal.Instance.AnimalPrint ();
            //SingleTon.Instance.Print ();
            //SingleTon.getInstance ().Print ();
            //Console.WriteLine ("resultStr ={0} : ");
            }
            }
            }


            6楼2018-01-20 22:32
            回复
              using System;
              using System.Collections;
              using System.Collections.Generic;
              using System.Diagnostics;
              namespace Program
              {
              class MainClass
              {
              public delegate void Mydelegate<DelegateT>(DelegateT num);
              public static void Method(int num)
              {
              Console.WriteLine ("method method method,num:" +num);
              }
              public static void Method2(Mydelegate<string> d)
              {
              d ("Killer");
              //Console.WriteLine ("method method method,num:" +num);
              }
              public static void Print(string str)
              {
              Console.WriteLine ("str:" +str);
              }
              public static void Main (string[] args)
              {
              Mydelegate<int> d;
              d = new Mydelegate<int> (MainClass.Method);
              d (12);
              MainClass.Method2 (MainClass.Print);
              //Console.WriteLine ("resultStr ={0} : ");
              }
              }
              }


              7楼2018-01-20 22:56
              回复
                Day 06 匿名方法
                using System;
                using System.Collections;
                using System.Collections.Generic;
                using System.Diagnostics;
                namespace Program
                {
                class MainClass
                {
                public delegate void ClosuerDelegate();
                public delegate void voteDelegate(string name);
                public static ClosuerDelegate ClourseMethod()
                {
                string CaptureVar = "能够被内部函数捕获的外部变量";
                ClosuerDelegate del = delegate {
                string localVar = "匿名方法内部的变量";
                Console.WriteLine (CaptureVar + " " + localVar);
                };
                del();
                return del;
                }
                public static void Main (string[] args)
                {
                ClosuerDelegate del = MainClass.ClourseMethod ();
                //del ();
                MainClass.ClourseMethod ();
                voteDelegate vote = delegate(string nickname) {
                Console.WriteLine ("nick is {0}", nickname);
                };
                vote("Jack");
                }
                }
                }


                8楼2018-01-25 22:33
                回复
                  广告
                  立即查看
                  Day 07 初始化对象&隐式类型
                  using System;
                  using System.Collections;
                  using System.Collections.Generic;
                  using System.Diagnostics;
                  namespace Program
                  {
                  public class Person{
                  public string Name{get;set;}
                  public int Age{ get; set;}
                  public string Sex{ get; set;}
                  public int ID{ get; set;}
                  }
                  class MainClass
                  {
                  public static void Main (string[] args)
                  {
                  List<string> list = new List<string> ();
                  list.Add ("y");
                  list.Add ("oooooooooo");
                  foreach (string item in list) {
                  Console.Write (item );
                  }
                  Person p = new Person (){Name="Killer",Age=12,Sex="M",ID=233 };
                  Console.WriteLine (p.Name+" "+p.Age+" "+p.Sex+" "+p.ID);
                  }
                  }
                  }


                  9楼2018-01-26 20:59
                  回复