當前位置:網站首頁>C#入門筆記

C#入門筆記

2022-01-27 06:37:09 superkcl2022

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
F11逐步調試
斷點調試
在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

結構體

namespace demo01{
    

    public struct Person{
    
        public string _name;
        public int _age;
        public char _gender;
    }
    class Program{
    
        static void Main(string[] args){
    
            Person zhangsan;
            zhangsan._name = "張三";
            zhangsan._age = 21;
            zhangsan._gender = '男';
            Console.WriteLine("{0}", zhangsan);
            Console.ReadKey();
        }
    }
}

在這裏插入圖片描述
字符串轉枚舉類型
在這裏插入圖片描述

out 參數

namespace out參數
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            //寫一個方法,求一個數組中的最大值、最小值、總和、平均值
            int[] numbers = {
     1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int[] res = getMaxMinSumAvg(numbers);
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",res[0], res[1], res[2], res[3]);
            Console.ReadKey();
        }
        public static  int[] getMaxMinSumAvg(int[] nums)
        {
    
            int[] res = new int[4];
            // res[0] max
            // res[1] min ...
            res[0] = nums[0];
            res[1] = nums[0];
            res[2] = 0;
            for (int i = 0; i < nums.Length; i++)
            {
    
                if (nums[i] > res[0])
                {
    
                    res[0] = nums[i];
                }
                if (nums[i] < res[1])
                {
    
                    res[1] = nums[i];
                }
                res[2] += nums[i];
            }
            res[3] = res[2]/ nums.Length;
            return res;
        }
    }
}

namespace out參數
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            //寫一個方法,求一個數組中的最大值、最小值、總和、平均值
            int[] numbers = {
     1, 2, 3, 4, 5, 6, 7, 8, 9 };
            int max, min, avg, sum;
            Test(numbers,out max,out min,out sum,out avg);
            Console.WriteLine("{0}\n{1}\n{2}\n{3}",max,min,sum,avg);
            Console.ReadKey();
        }
      
        public static void Test(int[] nums, out int max,out int min,out int sum,out int avg)
        {
    
            max = nums[0];
            min = nums[0];
            sum = 0;
            for (int i = 0; i < nums.Length; i++)
            {
    
                if (nums[i] > max)
                {
    
                    max = nums[i];
                }
                if (nums[i] < min)
                {
    
                    min = nums[i];
                }
               sum += nums[i];
            }
            avg = sum / nums.Length;
        }
    }
}

ref參數

namespace ref參數
{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            double salary = 5000;
            jiangJin(ref salary);
            Console.WriteLine(salary);
            Console.ReadKey();
        }
        public static void jiangJin(ref double s)
        {
    
            s += 500;
        }
        public static void faKuan(ref double s)
        {
    
            s -= 500;
        }
    }
}
        static void Main(string[] args)
        {
    
            int a = 1, b = 2;
            swap(ref a, ref b);
            Console.WriteLine("{0}{1}",a, b);
            Console.ReadKey();
        }
        public static void swap(ref int  a,ref int b)
        {
    
            int t = a;
            a = b;
            b = t;
        }

param可變參數

        static void Main(string[] args)
        {
    
            //int[] s = { 100, 99, 98 };
            Test("張三", 99,98,100);
            Console.ReadKey();
        }
        public static void Test(string name,params int[] score)
        {
    
            int sum = 0;
            for(int i = 0; i < score.Length; i++)
            {
    
                sum += score[i];
            }
            Console.WriteLine(sum);
        }

namespace{
    
    public class Person
    {
    
        public string _name;
        public int _age;
        public char _gender;


        public void CHLSS()
        {
    
            Console.WriteLine("我叫{0},我今年{1}歲了,{2},我會吃喝拉撒睡",this._name,this._age,this._gender);
        }

    }
}


namespace{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            Person zhangsan = new Person();
            zhangsan._name = "張三";
            zhangsan._age = 22;
            zhangsan._gender = '男';
            zhangsan.CHLSS();
            Console.ReadKey();
        }
    }
}

在這裏插入圖片描述
在這裏插入圖片描述

        public string name
        {
    
            get{
     return name; }
            set{
     name = value; }
        }

在這裏插入圖片描述

namespace{
    
    public class Person
    {
    
        private string _name;
        public string Name
        {
    
            get{
     return _name; }
            set{
     _name = value; }
        }
        private int _age;
        public int Age
        {
    
            get {
     return _age; }
            set {
    
                if(value < 0 || value > 100)
                {
    
                    value = 0;
                }
                _age = value;
            }
        }
        private char _gender;
        public char Gender
        {
    
            get {
     return _gender;}
            set {
    
                if (value != '男' && value != '女')
                {
    
                    value = '男';
                }
                _gender = value;
            }
        }


        public void CHLSS()
        {
    
            Console.WriteLine("我叫{0},我今年{1}歲了,{2},我會吃喝拉撒睡",this.Name,this.Age,this.Gender);
        }

    }
}

namespace{
    
    class Program
    {
    
        static void Main(string[] args)
        {
    
            Person zhangsan = new Person();
            zhangsan.Name = "張三三";
            zhangsan.Age = 22;
            zhangsan.Gender = '男';
            zhangsan.CHLSS();
            Console.ReadKey();
        }
    }
}

在這裏插入圖片描述
靜態類中只能有靜態方法,不能聲明實例成員
靜態類不能創建對象

構造函數

        public Person(string name,int age,char gender)
        {
    
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }

在這裏插入圖片描述
在這裏插入圖片描述
構造函數嵌套

        public Person(string name,int age,char gender)
        {
    
            this.Name = name;
            this.Age = age;
            this.Gender = gender;
        }
        public Person(string name):this(name,99,'男')
        {
    
            
        }

析構函數

        //當程序結束的時候 析構函數才執行
        // 幫助我們釋放資源
        // GC garbage collection
        ~Person()
        {
    
            Console.WriteLine("我是析構函數");
        }

namespace

引用其他項目的類
在這裏插入圖片描述
在這裏插入圖片描述

string

            string str = "abcdef";
            char[] chars = str.ToCharArray();
            chars[0] = 'k';
            str = new string(chars);

在這裏插入圖片描述

            char[] chs = {
     ' ', ',' };
            string[] strs = str.Split(chs);

        static void Main(string[] args)
        {
    
            string s = "2008-08-08 ";
            char[] chs = {
     '-' };
            //移除空白
            string[] strs = s.Split(chs, StringSplitOptions.RemoveEmptyEntries);
            for(int i = 0; i < strs.Length; i++)
            {
    
                Console.WriteLine(strs[i]);
            }
            Console.ReadKey();
        }

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

ArrayList

  static void Main(string[] args)
        {
    


            ArrayList list = new ArrayList();
            list.Add(1);
            list.Add(3.14);
            list.Add("hhh");
            list.Add(200m);
            list.Add(new int[] {
     1,2,3,4});
            list.Add(list);
            for(int i = 0; i  < list.Count; i++)
            {
    
                if(list[i] is int[])
                {
    
                    for(int j = 0; j < ((int[])list[i]).Length; j++)
                    {
    
                        Console.WriteLine(((int[])list[i])[j]);
                    }
                }
                else
                {
    
                    Console.WriteLine(list[i]);
                }
               
            }

            Console.ReadKey();
        }
    }
            //添加集合元素
            list.AddRange(new int[] {
    1,2,3,4});

在這裏插入圖片描述
在這裏插入圖片描述

HashTable

        static void Main(string[] args)
        {
    

            Hashtable ht = new Hashtable();
            ht.Add(1, "zs");
            ht.Add(2, "ls");
            ht.Add(3, "wem");
            ht[4] = 4;
            // h[1] 就是 "zs"
          
            Console.WriteLine(ht[1]);
            Console.WriteLine(ht[2]);
            Console.WriteLine(ht[3]);

            Console.ReadKey();
        }
            //遍曆鍵
            foreach(var item in ht.Keys)
            {
    
                Console.WriteLine(ht[item]);
            }

在這裏插入圖片描述

Path類

using System.IO;

 static void Main(string[] args)
        {
    
            string str = @"C:\Users\Thinkpad\Desktop\01.txt";

            string res = Path.GetFileName(str);
            string res2 = Path.GetFileNameWithoutExtension(str);
            string res3 = Path.GetExtension(str);
            string res4 = Path.GetDirectoryName(str);
            string res5 = Path.GetFullPath(str);
            string res6 = Path.Combine(@"C:\Users\", "a.avi");
            Console.WriteLine(res); //01.txt
            Console.WriteLine(res2); //01
            Console.WriteLine(res3); // .txt
            Console.WriteLine(res4); //C:\Users\Thinkpad\Desktop
            Console.WriteLine(res5);
            Console.WriteLine(res6);
            Console.ReadKey();
        }

File類

        static void Main(string[] args)
        {
    
            //創建一個文件
            File.Create(@"C:\Users\Thinkpad\Desktop\02.txt");
            //删除一個文件
            File.Delete(@"C:\Users\Thinkpad\Desktop\02.txt");
            //複制一個文件
            File.Copy(@"C:\Users\Thinkpad\Desktop\02.txt", @"C:\Users\Thinkpad\Desktop\03.txt");
        }
  • 讀文本
        static void Main(string[] args)
        {
    
           string[] strs =  File.ReadAllLines(@"C:\Users\Thinkpad\Desktop\02.txt");
            foreach(var item in strs)
            {
    
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
           string strs =  File.ReadAllText(@"C:\Users\Thinkpad\Desktop\02.txt");
      
           Console.WriteLine(strs);
           Console.ReadKey();

相對路徑

        static void Main(string[] args)
        {
    
           string strs =  File.ReadAllText(@"02.txt");
      
           Console.WriteLine(strs);
           Console.ReadKey();
        }

        static void Main(string[] args)
        {
    
           File.WriteAllLines(@"02.txt",new string[] {
     "213","11","45"});
     
           Console.ReadKey();
        }

覆蓋寫入

           File.WriteAllText(@"02.txt","superkcl");
     
           Console.ReadKey();

追加寫入

            File.AppendAllText(@"02.txt", "superkcl");
           Console.ReadKey();

List泛型

 static void Main(string[] args)
        {
    
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(3);
            foreach(var item in list)
            {
    
                Console.WriteLine(item);
            }
            //list轉為數組
            int[] nums = list.ToArray();
            //數組轉為list
            List<int> list2 = nums.ToList();
            //char數組轉list
            char[] chs = new char[] {
     'a', 'n' };
            List<char> list3 = chs.ToList();
            Console.ReadKey();
        }

Dictionary

  static void Main(string[] args)
        {
    
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(1, "zs");
            dic.Add(2, "ls");
            dic.Add(3, "wrm");
            dic[1] = "hhh";
            foreach(var item in dic.Keys)
            {
    
                Console.WriteLine(dic[item]);
            }
            foreach(KeyValuePair<int,string> item in dic)
            {
    
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value);
            }
            Console.ReadKey();
        }

多態之虛函數


namespace 多態
{
    
    public class Person
    {
    
        private string _name;
        public string Name
        {
    
            get {
     return _name; }
            set {
     _name = value; }
        }
        public Person(string name)
        {
    
            this.Name = name;
        }
        //虛方法,這個函數被子類重寫
        public virtual void SayHello()
        {
    
            Console.WriteLine("我是person");
        }
    }
    public class Chinese : Person
    {
    
        public Chinese(string name) : base(name)
        {
    

        }
        //重寫父類虛方法
        public override void SayHello()
        {
    
            Console.WriteLine("我是chinese,我叫{0}",this.Name);
        }
    }
    public class Japanese: Person
    {
    
        public Japanese(string name) : base(name)
        {
    

        }
        public override void SayHello()
        {
    
            Console.WriteLine("我是Japanese,我叫{0}", this.Name);
        }
    }
    public class American : Person
    {
    
        public American(string name) : base(name)
        {
    

        }
        public override  void SayHello()
        {
    
            Console.WriteLine("American,我叫{0}", this.Name);
        }
    }
    class Program
    {
    
        static void Main(string[] args)
        {
    
            
            Chinese cn1 = new Chinese("韓梅梅");
            Chinese cn2 = new Chinese("李雷");
            Japanese j1 = new Japanese("樹下君");
            Japanese j2 = new Japanese("井邊子");
            American a1 = new American("tom");
            American a2 = new American("jack");
            Person[] persons =  {
     cn1, cn2, j1, j2, a1, a2 };
            for(int i = 0; i < persons.Length; i++)
            {
    
                persons[i].SayHello();
                if(persons[i] is Chinese)
                {
    
                    ((Chinese)persons[i]).SayHello();
                }
            }
            Console.ReadKey();
        }
    }
}

抽象類

FileStream

            // FileStream操作字節的
            string path = @"C:\Users\Thinkpad\Desktop\02.txt";
            //常用的有openOrCreate append
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[1024 * 1024 * 2]; // 2M
            //本次實際讀取到的有效字節數
            int r = fs.Read(buffer,0,buffer.Length);
            //將字節數數組中每個元素解碼
            string s = Encoding.UTF8.GetString(buffer, 0, r);
            //關閉流
            fs.Close();
            //釋放流所占用的資源
            fs.Dispose();
         using (filestream fs = new filestream(@"c:\users\thinkpad\desktop\02.txt", filemode.openorcreate, fileaccess.write))
            {
    
                string str = "哈哈哈哈";
                byte[] buffer = encoding.utf8.getbytes(str);
                fs.write(buffer, 0, buffer.length);
            }
            console.writeline("寫入ok");
            console.readkey();

Directory

private void button1_Click(object sender, EventArgs e)
        {
    
             Directory.CreateDirectory(@"C:\Users\Thinkpad\Desktop\a");
             MessageBox.Show("創建成功");
            Directory.Delete(@"C:\Users\Thinkpad\Desktop\a",true);

            Directory.Move(@"C:\Users\Thinkpad\Desktop\a", @"C:\Users\Thinkpad\Desktop\b");

            //獲得該文件夾下面所有文件的全路徑
            string[] path = Directory.GetFiles(@"C:\Users\Thinkpad\Pictures\壁紙","*.jpg");
            for(int i = 0; i < path.Length; i++)
            {
    
                MessageBox.Show(path[i]);
            }

打開文件對話框

 private void button1_Click(object sender, EventArgs e)
        {
    
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "請選擇要打開的文件";
            ofd.Multiselect = true; //多選
            ofd.InitialDirectory = @"C:\Users\Thinkpad\Desktop";
            ofd.Filter = "文本文件|*.txt|媒體文件|*.wmv";
            ofd.ShowDialog();

            //獲得打開對話框中文件的路徑
            string path = ofd.FileName;
            if (path == "") return;
            using (FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read))
            {
    
                byte[] buffer = new byte[1024 * 1024 * 2];
               int l =  fs.Read(buffer, 0, buffer.Length);
                textBox1.Text = Encoding.UTF8.GetString(buffer, 0, l);

            }
        }

保存文件對話框

private void button1_Click(object sender, EventArgs e)
        {
    
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "請選擇要保存的路徑";
            sfd.InitialDirectory = @"C:\Users\Thinkpad\Desktop";
            sfd.Filter = "文本文件|*.txt";
            sfd.ShowDialog();

            string path = sfd.FileName;
            if (path == "") return;
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
    
                byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text);
                fs.Write(buffer, 0, buffer.Length);
            }
            MessageBox.Show("保存成功");
        }

字體顏色對話框

在這裏插入圖片描述

版權聲明
本文為[superkcl2022]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201270637083363.html

隨機推薦