C#学习之路

文章目录

        • c#组成、类型、实例化、枚举
        • 插箱与装箱、字节操作、字符串操作
        • 流程控值语句
        • 数据集合操作
        • 属性、方法、参数类型、重载
        • 结构和类

c#组成、类型、实例化、枚举

using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3  //建立命名空间
{
    class Program    //类
    {
        //2.创建新类型
        class C {
            public int value = 0;  //声明公共int类型变量value
        }
        //3.值类型 和引用类型
        public class stamp
        {
            public string name { get; set; }  //string是引用类型,get和set代表可获取与可修改
            public int Age { get; set; }
        }
        //4.枚举类型
        enum MyDate {
            Sum=0,
            Mon=1,
            Tue=2,
            Web=3,
            Thu=4,
            Fri=5,
            Sat=6
        }

        static void Main(string[] args)    //ain方法
        {
            Console.WriteLine("Hello word");
            //Console.ReadLine();   //等待任意输入继续
            //1.变量类型
            int ls = 927;   //整形
            byte shj = 255;  //字节类型
            Console.WriteLine("ls={0}", ls);
            Console.WriteLine("shj={0}", shj);
            float theMySUM = 9.27f;
            float theMuSums = 1.12F;   //使用F或者f强制指定为浮点型
            double myDou = 927d;
            double mudou = 112D;   //使用D或者d强制指定为浮点型


            //引用类型
            C r1 = new C();
            C r2 = r1;
            r2.value = 112;
            Console.WriteLine("Values:{0},{1}", r1.value,r2.value);   //输出引用类型对象的value值
    
            //实例化
            stamp s1 = new stamp { name = "permist", Age = 25 };
            int age = s1.Age;
            string names = s1.name;
            Console.WriteLine("name:{0},age:{1}", s1.name,s1.Age);   //输出引用类型对象的value值
            Console.WriteLine("name:{0},age:{1}", names,age);   //输出引用类型对象的value值
    
            //枚举
            int k = (int)DateTime.Now.DayOfWeek;  //代表星期几的返回值
            switch (k)
            {
                case (int)MyDate.Sum:Console.WriteLine("今天是星期日");break;
                case (int)MyDate.Mon: Console.WriteLine("今天是星期一"); break;
                case (int)MyDate.Tue: Console.WriteLine("今天是星期二"); break;
                case (int)MyDate.Web: Console.WriteLine("今天是星期三"); break;
                case (int)MyDate.Thu: Console.WriteLine("今天是星期四"); break;
                case (int)MyDate.Fri: Console.WriteLine("今天是星期五"); break;
                case (int)MyDate.Sat: Console.WriteLine("今天是星期六"); break;
    
            }
    
            Console.ReadLine();


        }
    }

}
插箱与装箱、字节操作、字符串操作
using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3  //建立命名空间
{
    class Program    //类
    {
    

        static void Main(string[] args)   
        {
            //类型转化,显式转换(强制转换)
            double x = 19810927.0112;
            int y = (int)x;   //doble强转为int
            Console.WriteLine(y);
            int y2=Convert.ToInt32(x);
            Console.WriteLine(y2);
            
            //拆箱与装箱
            //装箱将值类型转为引用类型过程
            //插箱是引用类型转为值类型
            //装箱实例如下
            int i = 2019;
            object obj = i;
            Console.WriteLine("1、i的值为{0} ;装箱的值对象为{1}", i, obj);
            i = 987;
            Console.WriteLine("2、i的值为{0} ;装箱的值对象为{1}", i, obj);

            //拆箱示例
            int j = 112;
            object obj2 = j;
            Console.WriteLine("1、j的值为{0} ;装箱的值对象为{1}", j, obj2);
            int h = (int)obj2;
            Console.WriteLine("2、j的值为{0} ;装箱的值对象为{1}", j, h);
            //特殊运算符
            //is
            int a1 = 0;
            bool result = a1 is  int;
            Console.WriteLine(result);
            //?:
           string yesno= result ? "是int" : "不是int";
            Console.WriteLine("判断结果是:{0}", yesno);
            
            //new运算符,用户数组创建、对象创建、代表创建
            string[] phone = new string[5];
            phone[0] = "1";
            phone[1] = "2";
            phone[2] = "3";
            phone[3] = " ";
            phone[4] = "5";
            Console.WriteLine(phone[0]);
            Console.WriteLine(phone);
            //typeof运算符
            Type mytype = typeof(int);   //获取int类型的原始类型
            Console.WriteLine("类型:{0}",mytype);
            
            //字符串类Sting的使用  Compare与CompaTo使用上区别不大
            string s1 = "你好" + "1";
            string s2 = "你好";
            if (String.Compare(s1, s2) == 0)
            {
                Console.WriteLine("相等");
            }
            else {
                Console.WriteLine("不相等");
            }
            //equals方法1
            if (s1.Equals(s2))
            {
                Console.WriteLine("s1与s2相等");
            }
            else
            {
                Console.WriteLine("s1与s2不相等");
            }
            //equals方法2
            if (String.Equals(s1,s2))
            {
                Console.WriteLine("s1与s2相等");
            }
            else
            {
                Console.WriteLine("s1与s2不相等");
            }
            //Format格式化
            string news = String.Format("{0}{1}!!!!", s1, s2);
            Console.WriteLine(news);
            //截取字符串
            string s3 = s1.Substring(0, 2);
            Console.WriteLine(s3);
            //分割字符串
            string ss1 = "社会工业,团结发展";

            String[] splistrings = new String[100];
            splistrings=ss1.Split(','); //返回的是字符串数组,分割字节分割。
            for (int q = 0; q < splistrings.Length; q++)
            {
                Console.WriteLine("item{0}:{1}",q,splistrings[q]);
            }

            //插入字符和填充字符
            string ss2=ss1.Insert(0, "我说得,");
            string ss3=ss2.Insert(4, ",绝对");
            Console.WriteLine(ss3);
            //左右侧填充
            string ss4 = "xxxxx";
            string ss5 = ss4.PadLeft(6, '(');
            Console.WriteLine(ss5);
            String ss6 = ss5.PadRight(7, ')');
            Console.WriteLine(ss6);
                 //获取字符串长度
            Console.WriteLine( ss6.Length+1);
            //删除字符串
            Console.WriteLine(ss4.Remove(3)); //从3之后字节开始删除
            Console.WriteLine(ss4.Remove(0,2));//删除0到字节
            //复制字符
           string ss7 = String.Copy(ss4);
            Console.WriteLine(ss7);

            //替换
            string b1 = ss4.Replace('x', 'c');
            Console.WriteLine(b1);
            //可变字符stringBuilder(字符串,分配大小)
            StringBuilder hostStr = new StringBuilder("窗前明月光,疑是地上霜",100);
           //结尾追加
            hostStr.Append("VS 举头望明月");
            Console.WriteLine(hostStr);
            //结尾追击2
            hostStr.AppendFormat("{0:s}", " 低头思故乡");
            Console.WriteLine(hostStr);
            //位置插入
            hostStr.Insert(0, "静夜思");
            Console.WriteLine(hostStr);
            //字符串替换
            hostStr.Replace("静夜思", "静夜思\t李白\t");
            Console.WriteLine(hostStr);
            //截取从0到。长度减去6的字符串
            hostStr.Remove(0, hostStr.Length - 6);
            Console.WriteLine(hostStr);
            
            Console.ReadLine();
        }
    }
}

流程控值语句
using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication3  //建立命名空间
{
    class Program    //类
    {
    

        static void Main(string[] args)   
        {
            //if语句
            string str1 = "A";
            if (str1=="A")
            {
                Console.WriteLine("是的");
            }
            else if (str1=="B")
            {
                Console.WriteLine("不是");
                
            }
            //switch语句
            switch (str1)
            {
                case "A":
                    Console.WriteLine("是的A");
                    break;
                case "B":
                    Console.WriteLine("是的B");
                    break;
                default:
                    Console.WriteLine("都不是的");
                    break;
            }
            //while语句
            while (str1.Length>0)
            {
                Console.WriteLine("不是空值");
                break;
            }
            int i = 0;
            do
            {
                Console.WriteLine("当前值是:{0}",i);
                i++;
            } while (i<10);

            //for
            for (int j = 0; j < 10; j++)
            {
                Console.WriteLine("你好,{0}",j);
            }
            //foreach 语法
            ArrayList alt = new ArrayList();
            alt.Add("你");
            alt.Add("的");
            alt.Add("名");
            alt.Add("字");
            foreach (string item in alt)
            {
                Console.WriteLine(item);
            }
            //continue
            for (int k = 0; k < 5; k++)
            {
                if (k==3)
                {
                    continue;
                }
                Console.WriteLine(k);
            }
            //goto语句
            ceshi1:
                Console.WriteLine("goto语句测试");
                
            goto ceshi1;  //出现无限循环


        }
    }
}
数据集合操作
using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication3  //建立命名空间
{
    class Program    //类
    {
    

        static void Main(string[] args)   
        {
            //数组
            int[] arr = new int[10];
            int[] brr = new int[] { 1,2,3,4,5,6,7,8,9,10};
            int[] crr = new int[] { 1, 22, 3, 43, 51, 6, 7, 8, 9, 10 };
            //二维数组

            int[,] erArr = new int[,] { { 1, 2 }, { 3, 4 } };
            int[,] erBrr = { { 1, 2 }, { 3, 4 } };
            int[,] erCrr = new int[2, 2] { { 1, 2 }, { 3, 4 } };

            for (int i = 0; i < brr.Length; i++)
            {
                Console.WriteLine(brr[i]);
            }

            for (int j = 0; j < erBrr.GetLength(0); j++)
            {
                for (int k = 0; k < erBrr.GetLength(1); k++)
                {
                    Console.WriteLine(erBrr[j,k]);
                }
            }
            //foreach
            foreach (int a3 in brr)
                Console.WriteLine(a3);
            //数组排序 正
            Array.Sort(crr);
            foreach (int a4 in crr)
                Console.WriteLine(a4);
            //数据排序 反
            Array.Reverse(crr);
                 foreach (int a5 in crr)
                            Console.WriteLine(a5);
            //数组合并
            int[] drr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] err = new int[] { 11,12,13,14,15 };
            int[] f = new int[drr.Length + err.Length];
            Console.WriteLine(f.Length);
            for (int q = 0; q < drr.Length; q++)
            {
                f[q] = drr[q];
            }
            for (int w = 0; w < err.Length; w++)
            {
                f[drr.Length+w] = err[w];
            }
            for (int ai = 0; ai < f.Length; ai++)
            {
                Console.WriteLine(f[ai]);
            }
            //ArrayList高级动态数据
            int [] a1 = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            ArrayList List = new ArrayList(a1);
            //Arraylist的固定大小判断
            Console.WriteLine(List.IsFixedSize);
            //是否为只读
            Console.WriteLine(List.IsReadOnly);
            //是否同步对ArrayList的访问
            Console.WriteLine(List.IsSynchronized);
            //获取索引值的元素
            Console.WriteLine(List[1]);
            //获取ArrayList访问的对象
            Console.WriteLine(List.SyncRoot);
            //Add方法
            List.Add(8);
            //获取ArrayList的长度,并追击到最后一个
            Console.WriteLine(List.Count);
            List.Insert(List.Count,9);
            //遍历输出
            for (int array = 0; array < List.Count; array++)
            {
                Console.WriteLine(List[array]);
            }
            //遍历输出2
            foreach (int num1 in List)
            {
                Console.WriteLine(num1);
            }
            //ArrayList的删除
            //搜索匹配的值,并删除
            List.Remove(8);//删除数字8
   
            //根据索引删除
            List.RemoveAt(0);
            //根据索引一处一定范围的
            List.RemoveRange(0, 3);
            foreach (int num1 in List)
            {
                Console.WriteLine(num1);
            }
            List.Clear();  //清除数组所有
            Console.WriteLine("数组大小长度是{0}",List.Count);
            //Hashtable 哈希表的数组集合
            Hashtable hash = new Hashtable();   //实例化hash的对象
            hash.Add("id", "123");
            hash.Add("name", "ss");
            Console.WriteLine(hash.Count); //获取长度
            Console.WriteLine( hash["id"]);//获取指定值

            //判断是否包含key
            Boolean iskeyvalue = hash.Contains("id");
            Boolean isKey = hash.ContainsKey("id");
            Console.WriteLine(isKey);
            //判断是否包含value
            Boolean isValue = hash.ContainsValue("ss");
            Console.WriteLine(isValue);
            //获取所有键
            Console.WriteLine(hash.Keys);
            foreach (String key in hash.Keys)
            {
                Console.WriteLine(hash[key].ToString());
            }

            foreach (String values in hash.Values)
            {
                Console.WriteLine(values.ToString());
            }
            //删除
            //指定键位,删除
            hash.Remove("id");
            //清除所有
            hash.Clear();

      


        }
    }
}

属性、方法、参数类型、重载
using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication3  //建立命名空间
{
    class Program    //类
    {
        //自定义属性
        private string id = "";
        private string name = "";
        public string ID {
            get {
                return id;
            }
            set {
                id = value;
            }

        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        //params参数。对于可变的方法参数
        public void Username(params string[] list) {
            for (int i = 0; i < list.Length; i++)
            {
                Console.WriteLine(list[i]);
            }
        }
        //ref参数,引用船值,即是传入变量,接收方法的返回值
        public  void method(ref int i) {
            i = 200;
        }
        //out参数,引用船值,即是传入变量,接收方法的返回值
        public void methodOut(out int i)
        {
            i = 200;
        }

        //静态方法
        public  static void fangfa()
        {
            Console.WriteLine("静态方法,需要类名调用");
        }
        非静态方法
        public void fangfa1() {
            Console.WriteLine("非静态方法,通过实例调用");
        }
        //重载
        public int add(int a, int b) {
            return a + b;
        }
        public double add(double a, double b) {
            double c=a+b;
            return c;
        }


        static void Main(string[] args)   
        {
            Program pro = new Program();
            pro.ID = "123";
            pro.Name = "zhang";
            Console.WriteLine("ID为{0},name为{1}",pro.id,pro.name);

            //param方法
            string[] s1 = new string[] { "zhang","王","李","你还哦" };
            pro.Username(s1);
            //ref方法
            int values = 1;  //需要初始化值
            pro.method(ref values);
            Console.WriteLine(values);
            //out方法
            int outValue;  //不需要初始化值
            pro.methodOut(out outValue);
            Console.WriteLine(outValue);
            //调用静态方法
            Program.fangfa();
            //调用非静态的方法
            pro.fangfa1();
            //重载方法调用
            Console.WriteLine(pro.add(1,2));
            Console.WriteLine(pro.add(1.1, 2.2));

        }
    }
}

结构和类
using System;       //多个命名空间。与python的import功能形似
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace ConsoleApplication3  //建立命名空间
{
    //结构
    public struct Rect {
        public double w;
        public double h;
        public double Area() {
            return w * h;
        }

    }
    //构造函数与析构函数
    public class publics {
        public Double a1 = 123.1;
        public Double a2 = 123.123;
        public Double a3;
        //构造函数与类名一样
        public publics()
        {
            a3 = a1 + a2;
        }
        //析构函数~
        ~publics()
        {
            Console.WriteLine("已经释放了publics的调用");
        }

    }
    //封装、继承、多态
    public class myClass{
        private int a = 0;
        private int b = 0;
        public int A{
            set {
                a = value;
            }
            get {
                return a;
            }

        }

        public int B
        {
            set
            {
                b = value;
            }
            get {
                return b;
            }

        }
        public int Add() {
            return a + b;
        }

    }
    //继承
    public class myClass1 : myClass {
        private int c;
        public int C
        {
            set
            {
                c = value;
            }
            get
            {
                return c;
            }
        
        }
        public int Add2()   //这个方法就可以做为多态的一种
        {
            return A + B+C;
        }


    }
    class Program    //类
    {
        static void Main(string[] args)
        {
            //使用结构体
            Rect rect;
            rect.w = 123.123;
            rect.h = 1234.123;
            Console.WriteLine("面积为{0}",rect.Area());
            //调用publics体验构造函数和析构
            publics cs=new publics();
            Console.WriteLine(cs.a3);
            //使用类,体验封装,继承,多态
            myClass my1 = new myClass();
            my1.A = 123;
            my1.B = 345;
            Console.WriteLine(my1.Add());
            myClass1 my2 = new myClass1();
            my2.A = 123;
            my2.B = 456;
            my2.C = 120;
            Console.WriteLine(my2.Add2());


        }


        }
}

你可能感兴趣的:(c#)