c#编程

goto mylabel;//建议不使用

mylabel:

return跳出循环  ,返回结束方法的· 

int num = convert.toint32();//强制转换

 

 

结构函数

 struct cutomerName
    {
        public string firstname;
        public string lastname;
        public string getname()
        {
            return firstname + lastname;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {

            cutomerName myname;
            myname.firstname = "1";
            myname.lastname = "2";
            myname.getname();
        }
    }

 

 

 

函数的重载

函数名相同,参数不同,叫作函数的重载,编译器通过不同的参数类型来判断函数

 //函数的重载
        static int MaxVaule(params int[] arry)
        {
            int maxvaule = arry[0];
            for (int i = 1; i < arry.Length; i++)
            {

                if (arry[i] > maxvaule)
                {
                    maxvaule = arry[i];
                }
            }
            return maxvaule;
        }
        static double MaxVaule(params double[] arry)
        {
            double maxvaule = arry[0];
            for (int i = 1; i < arry.Length; i++)
            {

                if (arry[i] > maxvaule)
                {
                    maxvaule = arry[i];
                }
            }
            return maxvaule;
        }

   //委托,是一种存储函数引用的类型
    //定义☞一个返回的类型和一个参数列表,可以声明一个变量,这个变量可以指向一个函数
    //声明一个委托和函数差不多,1,定义委托需要加上delegate关键字2,委托不需要函数体
    public delegate double MyDelegate(double param1, double param2);

 

 //委托,是一种存储函数引用的类型
        //定义☞一个返回的类型和一个参数列表,可以声明一个变量,这个变量可以指向一个函数
        static double Multply(double param1, double param2)
        {
            return param1 * param2;
        }
        static double Divide(double param1, double param2)
        {
            return param1 / param2;
        }

 

    static void Main(string[] args)
        {
            //委托
           MyDelegate de;//z利用定义的委托函数声明一个新的变量
            de = Multply;
            de(1.0, 3.5);

          
        }

 

面向对象

为什么:为了使编程更加清晰,把程序中的功能进行模块划分,每个模块有它们自己的功能,且每个模块都是独立的,就是结构话编程,也叫oop编程,对结构体中的变量进行结构划分。

类:创建对象的模板,每个对象都包含数据,并提供处理和访问数据的方法

类定义了类的每个对象(实例)可以包含什么数据和编程

类中的数据和函授称为类的成员

数据成员:

包含类的数据字段,常量,事件

 

函数成员:

提供操作类的数据的某些功能,方法,属性

 class Customer
    {//4字段
        public string name;
        public string address;
        public int age;
        public string buytime;

        public void show()
        {
            Console.WriteLine(name);
        }
    }

 

········································

    class Vector3
    {
        private float x, y, z;
        //为字段体提供set方法,来设置字段的值
        public void setx(float x)
        {
            //如果我们直接在方法内部访问同名的变量的时候,优先访问最近的(形参)
            //this.表示访问的是类的字段或者是方法
            this.x = x;
        }

        public void sety(float y)
        {
            this.y = y;
        }


        public float Length()
        {
            return (float)Math.Sqrt(x * x + y * y + z * z);

        }

    }

 

构造函数  new

当我们不写构造函数的时候,编译器给默认的

 class Vector3
    {
        //定义了一个构造函数
        public Vector3()
        {

        }
        public Vector3(float x, float y, float z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }

使用如下

 Vector3 ver = new Vector3(2,1,1);
           

 

 

 

 

属性的定义

public int MyINTProp{

get{}

set{}

}

属性的定义需要名字和类型

属性包含get set

访问属性和访问字段一样,当取得属性的值的时候,就会调用get,get需要一个返回值,类型就是属性的类型,

当给shux设置值的时候,就会调用属性中的set,在set块中通过value访问我们设置的值

  //定义一个属性
        public int MyIntProperty
        {
            set { Console.WriteLine("set在调用" + value); }
            get { Console.WriteLine("get在调用");return 100; }
           
        }

 

通过属性来访问字段,这样外界就不能修改字段的值,可以通过定义属性来设置和取得字段的值


        private float x, y, z;

        //属性的定义,getset方法
        public float X
        {
            get { return x; }
            set { x = value; }
        }
 

   private int age;
        public int Age
        {//通过set方法之前进行校验 属性的更多好处需要以后体会
            set { if (value >= 0) age = value; }
        }

 

设置属性只读和只写  只提供get或者

public string Name{get;set;}

 

 

 

 

匿名 

var 当类型被赋值时就被确定了

 

 

推和栈

栈空间较小,但是读取速度快

堆空间较大,但读取速度慢

GC garbage collector 

clr内存管理机制,垃圾回收器

 

 

 

值类型bool struct char floot    只需要单独的内存

 引用类型string 自定义  第一段存储实际数据  第二段是一个引用  指向数据在堆中存放的位置

如果数组是值类型,数组中存储的是值

如果数组存储引用,则存储

 

 

 

面向对象  :继承

 

c#只允许多重接口继承    c#可以派生自一个类和任意多个接口

c#都有一个基类和任意多个基接口

基类;敌人类  hp speed ai move

派生两个类  boss  

type1  type2

父类声明的对象可以使用子类去构造

一个对象是声明类型  主要看它通过什么来构造

 

 

 

虚方法   把基类函数声明为virtual ,就可以在任何派生类中重写该函数

重写该函数用override

 

 

隐藏方法

如果签名相同的方法没有virtual 和override 

要使用new

public new void A()

{}

 

 

this 和base关键字

base只能访问父类公有的方法和字段

 

 

抽象类

把类和函数声明为abstract,抽象类不能实例化,可以包含普通函数和抽象函数

抽象函数只有函数定义没有函数体    抽象函数本身也是虚拟virtual

是一个不完整的模板

abstract  class Buding {

}

定义了多少个抽象函数,继承的时候就要用override重写多少个

 

 

密封类和密封方法:类不能被继承,方法不能被重写

sealed FinalClass

{

}

什么时候使用密封类和密封方法:防止重写某些类导致代码混乱,一般用于商业原因

class DerivedClass:BaseClass
    {
        public sealed override void Move()//我们可以把重写的方法声明为密封方法,该方法不能被重写
        {
            base.Move();
        }

    }

 

 

 

派生类的构造函数

在子类中调用父类的默认构造函数(无参)

public class Myderivedclass{

public myderivedclass():base(){//base()可以直接不写

}

}

先调用父类的再调用子类的

 

 

 

 

 

关于访问修饰符protected 和static

protected保护,只有派生的才可以访问

 

static 可以修饰字段或者方法

修饰方法的时候叫静态方法,只能通过类名来访问

 //静态数据
        public static int zz;//不包含z

     //访问静态
            BaseClass.zz = 100;

 

 

定义和实现接口

在语法上和定义一个抽象类完全相同,但不允许提供接口中任何成员的实现方式,

接口只包含方法,属性,索引器和事件的声明

接口不能有构造函数,也不能有字段,也不允许运算符重载

不允许声明成员的修饰符,接口成员都是公有的

public interface Ifhander{

public void fly();

}

实现接口:

public class TYPEenemy:Ifhander{

 

}

 

派生的接口

接口之间可以彼此继承

 

 

 

 

List  集合类

  List<int> slist = new List<int>();

 

 

 

 

 

 

 

 

 

委托定义和使用

我们要把方法当参数来传递的画,就要用到委托

delegate void inme(int x);

void aa(int x)

{}

inme a;

a =aa;

 

     private delegate string Getstring();//定义一个委托类型
        static void Main(string[] args)
        {
            int x = 40;
            x.ToString();
            Getstring getstring =x.ToString ;
            string s = getstring();//通过委托实例通过x.tostring
        }

 

 private delegate string Getstring();//定义一个委托类型
        static void Main(string[] args)
        {
            int x = 40;
            x.ToString();
            Getstring getstring =x.ToString ;
            //string s = getstring();//通过委托实例通过x.tostring
            string s = getstring.Invoke();//通过invoke调用getstring的方法
            Console.WriteLine(s);


        }

 

 

 

   //把委托类型作为方法的参数
            PrintString mehod=Method1;
            mehod = Method2;
            PrintStr(mehod);//需要初始化

        }

        private delegate void PrintString();
        static void PrintStr(PrintString print)//修饰方法的时候叫静态方法,只能通过类名来访问
        {
            print();
        }
        static void Method1()
        { Console.WriteLine("method1"); }
        static void Method2()
        { Console.WriteLine("method2"); }

 

 

 

Action委托,引用返回void类型

     static void print()
        {
            Console.WriteLine("ok");
        }
        static void intprint(int i)
        {
            Console.WriteLine("int ok");
        }
        static void Main(string[] args)
        {
            int x = 100;
            Action a = print;//没有没有返回类型
            a();

            Action<int> aa = intprint;//没有返回值,有一个int参数的方法

        }

 

 

Func委托  可以带一个返回值,多个类型参数0到16个

  static int test1()
        {
            return 1;
        }
        static void Main(string[] args)
        {
            Func<int>a = test1;//func后面可以跟很多类型,最后一个类型是返回类型,前面是参数类型,参数类型必须与指向的方法的参数类型顺序一致
            Console.WriteLine(a());

        }

 

 

 

 

 

 

int 类型排序

 class Program
    {
        static void Sort(int[] sortA)
        {
            bool wapped = true;
            do
            {
                wapped = false;
                for (int i = 0; i < sortA.Length - 1; i++)
                {
                    if (sortA[i] > sortA[i + 1])
                    {
                        int temp = sortA[i];
                        sortA[i] = sortA[i + 1];
                        sortA[i + 1] = temp;
                        wapped = true;
                    }

                }
            } while (wapped);
        }


        static void commeonsort<T>(T[] sortA,Func<T,T,bool> compareMethod)
        {
            bool wapped = true;
            do
            {
                wapped = false;
                for (int i = 0; i < sortA.Length - 1; i++)
                {
                    if (compareMethod (sortA[i] , sortA[i + 1]))
                    {
                        T temp = sortA[i];
                        sortA[i] = sortA[i + 1];
                        sortA[i + 1] = temp;
                        wapped = true;
                    }

                }
            } while (wapped);
        }


        static void Main(string[] args)
        {
            int[] sortArray = new int[] { 11, 223, 2134, 234, 66, 78, 9 };
            Sort(sortArray);
            foreach (var temp in sortArray)
            { Console.Write(temp + "\n"); }

            Employee[] employees = new Employee[]
            {
                new Employee("1",312),
            new Employee("2", 125),
            new Employee("3", 123),
            new Employee("4", 133),
        };
            commeonsort<Employee>(employees, Employee.Compare);

        }
    }

 

 

 

多播委托

委托多个方法,按顺序调用最后结果

 

throw new Exception();//抛出异常,后面方法不能调用

 

            Delegate[] delegates = a.GetInvocationList();
            foreach (Delegate de in delegates)
            { de.DynamicInvoke(); }

 

 

 

//匿名方法  本质上是一个方法

    class Program
    {
        static int test1(int a1,int a2)
        {
            return a1 + a2;
        }

        static void Main(string[] args)
        {
            // Func<int, int, int> plus = test1;
            //修改成匿名方法,需要delegate
            Func<int, int, int> plus = delegate (int a1, int a2)
            {
                return a1 + a2;

            };

            Console.WriteLine(" ");
        }
    }

 

 

 

 

program事件

事件基于委托。event,为委托提供一个发布订阅机制,事件使一种具有特殊签名的委托

事件使类或对象通知发生的一种特殊签名的委托

 public delegate void MyDelegate();

        //public MyDelegate myDelegate;//声明一个委托类型的变量,作为类的成员

        public event MyDelegate myDelegate;
        static void Main(string[] args)
        {
            Program p = new Program();
            p.myDelegate = Test1;
            p.myDelegate();
        }

        static void Test1()
        {
            Console.WriteLine("test1");
        }

 

 

 

 

 

 

 

 

观察者设计

class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat("加菲猫","黄色");
            Mouse mouse1 = new Mouse("米奇1","黑色");
            Mouse mouse2 = new Mouse("米奇2","黑色");
            //注册
            cat.catCome += mouse1.RunAway;
            cat.catCome += mouse2.RunAway;

            cat.CatComing();
            // cat.CatComing(mouse1,mouse2);

        }
    }

 

 

 class Cat
    {
        private string name;
        private string color;
        public Cat(string name, string color)
        {
            this.color = color;
            this.name = name;
        }

        //public void CatComing(Mouse mouse1,Mouse mouse2)
        //{
        //    Console.WriteLine(color + "颜色" + name + "coming");
        //    mouse1.RunAway();
        //    mouse2.RunAway();
        //}
        public void CatComing()
        {
            Console.WriteLine(color + "颜色" + name + "coming");
            if(catCome!=null)
            catCome();
        }

        public Action catCome;
    }

 

 

  class Mouse
    {
        private string name;
        private string color;
        public Mouse(string name, string color)
        {
            this.color = color;
            this.name = name;
        }

        public void RunAway()
        {
            Console.WriteLine(color + "颜色" + name + "老鼠跑");
           
        }

    }

 

 

委托和事件的区别

事件,不能通过对象调用,只能类的内部调用,可以在外部进行注册

是限制的委托,委托的特殊使用,加减

委托常常表示回调,事件表示外发的接口

 

 

 

数据初始化和LINQ使用

  var masterList = new List<MartiaArtsMaster>()
            {
                new MartiaArtsMaster(){ID = 1,Name ="1",Age =1,Menpai ="华硕",Kongfu ="kk" ,Level=1},
                 new MartiaArtsMaster(){ID = 2,Name ="2",Age =2,Menpai ="华硕2",Kongfu ="kk2" ,Level=2},
            };
            var kongfulist = new List<Kongfu>() {
                new Kongfu(){ID =1,Name = "kk",Power = 10},
                 new Kongfu(){ID =2,Name = "kk2",Power = 10},
            };

            //查询所有级别大于1的
            var res = new List<MartiaArtsMaster>();
            foreach (var temp in masterList)
            {
                if (temp.Level > 1)
                { res.Add(temp); }
            }


            //s使用linq做查询
            var res1 = from m in masterList
                       where m.Level > 1
                       select m;

 

  //linq扩展方法的写法
          var res2=  masterList.Where(test1);
        }


        //过滤方法
        static bool test1(MartiaArtsMaster master)
        {
            if (master.Level > 1) return true;
            return false;
        }

 

 

 var res3 = masterList.Where(m => m.Level > 1);

 

 

 

 

进程  线程

 class Program
    {
        static void Test(int i)
        {
            Console.WriteLine("test1" +i);
        }
        static int Test1(int i)
        {
            Console.WriteLine("test1" + i);
            Thread.Sleep(100);
            return i+1;
        }

        static void Main(string[] args)
        {
            //委托action
            Action<int> a = Test;
            try
            {
                a.BeginInvoke(100, null, null);//开启一个新的线程去执行a所用的方法
            }
            catch { }


            Func<int,int> a1 = Test1;
          //  IAsyncResult ar = a1.BeginInvoke(100, null, null);
          
            IAsyncResult ar=   a1.BeginInvoke(100, null, null);//开启一个新的线程去执行a所用的方法,取得当前线程的状态
           
         

            Console.WriteLine("main");

            while (ar.IsCompleted ==false)
            {
                Console.Write(".");
                Thread.Sleep(10);
            }
            int res = a1.EndInvoke(ar);//取得异步的返回值

        }

 

 

    //检测线程结束,等待线程结束
           bool isend = ar.AsyncWaitHandle.WaitOne(200);

 

  //通过回调,检测线程结束,倒数第二个是委托类型的参数,当线程结束时会调用这个委托指向的方法
            Func<int, int> a2 = Test1;
            IAsyncResult ar2 = a2.BeginInvoke(100, OnCallBack,a2);

 

 

 


            //通过回调,检测线程结束,倒数第二个是委托类型的参数,当线程结束时会调用这个委托指向的方法
            Func<int, int> a2 = Test1;
            // IAsyncResult ar2 = a2.BeginInvoke(100, OnCallBack,a2);
             a2.BeginInvoke(100, ar2 =>
             {
                 int res = a2.EndInvoke(ar2);
             },null);

 

 

 

 

  static void Download()
        {
            Console.WriteLine("1开始下载,id为" + Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(100);
            Console.WriteLine("1下载完成");
        }
        static void Main(string[] args)
        {
            Thread t = new Thread(Download);//异步执行的方法,这个方法没有调用
            t.Start();


            Thread t1 = new Thread(() =>
            {
                Console.WriteLine("2开始下载,id为" + Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(200);
                Console.WriteLine("2下载完成");
            });
            t1.Start();
        }

 

 

    MyThread my = new MyThread("xxx", "http:123.com");
         Thread t3 =   new Thread(my.DownLoad);
            t3.Start();

 

 

后台线程和前台线程

前台程序一直运行到结束,后台线程会被强制关闭

thread默认创建是前台,线程池是后台

//设置后台进程, t3.IsBackground = true;

线程的优先级

控制线程,是否开始

thread.abort()可以停止线程

//t3.Join();让当前线程休眠,等待t3的完成

 

 

 

 

 

 

 

 

线程池:后台线程

一般做小任务

 

 static void ThreadMethod(object state)
        {
            Console.WriteLine("线程开始"+Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(100);
            Console.WriteLine("线程结束");
        }
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(ThreadMethod);
            ThreadPool.QueueUserWorkItem(ThreadMethod);
            ThreadPool.QueueUserWorkItem(ThreadMethod);
            ThreadPool.QueueUserWorkItem(ThreadMethod);
            Console.ReadKey();
        }

 

 

通过任务

 Task t = new Task(ThreadMethod);//传递一个需要线程去执行的方法
            t.Start();
               


            TaskFactory tf = new TaskFactory();
            Task t1 = tf.StartNew(ThreadMethod);

连续任务

  //连续任务
            Task t2 = new Task(ThreadMethod);
            Task t3 = t2.ContinueWith(ThreadMethod);

任务有父子关系

 

 

 static  void change(object o)
        {
            MyThread m = o as MyThread;
            //while(true)
            //{
            //    m.Changetate();
            //}
            while (true)
            {
                lock(m)
                m.Changetate();
            }
        }

        static void Main(string[] args)
        {
            MyThread m = new MyThread();
            // Thread t = new Thread(m.Changetate);
            Thread t = new Thread(change);
            t.Start(m);

            new Thread(change).Start(m);
            Console.ReadKey();
        }

 

 

 

 

 

 

socket编程

服务器

  static void Main(string[] args)
        {
            //创建sokect
            Socket tcpserver = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            //开始绑定ip和端口号
            IPAddress iPAddress = new IPAddress(new byte[] { 192,168,2,103 });
            EndPoint point = new IPEndPoint(iPAddress,7788);//IP加端口号进行封装
            tcpserver.Bind(point);
            //开始监听,等待客户端连接,最大连接数
            tcpserver.Listen(100);

            Socket clientsocket =   tcpserver.Accept();//暂停当前线程,直到有客户端链接进来
            //使用返回的socket跟客户端通信
            string message = "hello";
            byte[]data= Encoding.UTF8.GetBytes(message);
            clientsocket.Send(data);
            Console.WriteLine("Hello World!");
        }

 

 

 

static void Main(string[] args)
        {
            //创建socket
            Socket tcpclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //发起建立连接的请求
            IPAddress ipaddres = IPAddress.Parse("192.168.2.103");
            EndPoint point = new IPEndPoint(ipaddres,7788);
            tcpclient.Connect(point);//通过ip加端口号一个连接的服务器端
            byte[] data = new byte[1024];
            int length = tcpclient.Receive(data);
            string message = Encoding.UTF8.GetString(data,0,length);

            Console.WriteLine("客户端接收到的"+message);


            //向服务器端发送消息
            string message2 = Console.ReadLine();
            tcpclient.Send(Encoding.UTF8.GetBytes(message2));


        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

XML可扩展标记文本

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非鱼.li

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值