天天看點

C# winform學習筆記一、C#基本文法1、注釋 

目錄

一、C#基本文法

1、注釋

2、變量

3、表達式

4、數組

5、函數

6、面向對象OOP

7、集合、字典

一、C#基本文法

C#是微軟公司釋出的一種面向對象的、運作于.NET Framework 和 .NET Core(完全開源、跨平台)之上的進階程式設計語言,由C和C++衍生出的面向對象的變成語言。

//引用命名空間 
using System;            
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//項目的命名空間    
namespace WindowsFormsApp1     
{
    //類
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
           

1、注釋

單行注釋://

多行注釋:

文檔注釋:///

2、變量

C# winform學習筆記一、C#基本文法1、注釋 

3、表達式

C# winform學習筆記一、C#基本文法1、注釋 

x || y   或

4、數組

數組是一組想同類型的資料,數組中的資料需要通過數字索引來通路。

數組的聲明:

  • 數組的聲明需要使用new關鍵字
  • 在聲明數組時,可以使用()來初始化數組中的元素。
  • 如果在數組聲明之初沒有使用大括号來初始化數組中的元素,則需要指定數組的大小。
  • 在聲明初始化有元素的數組時,也可以指定數組大小。
//聲明沒有元素的數組
int[] arr = new int[6];
//聲明初始化有元素的數組
int[] arr1 = new int[] { 1, 2, 3 };
//聲明初始化有元素數組并指定數組大小
int[] arr2 = new int[3] { 1, 2, 3 };
           

通過索引擷取數組中的元素:

  • 給數組指定長度時,數組準備存放多少元素,長度就設定為多少。
  • 用索引擷取數組内的元素時,索引從0開始擷取。
  • 是以數組中最大的索引數字,比指定數組長度小1.

5、函數

參數修飾符:

C# winform學習筆記一、C#基本文法1、注釋 

6、面向對象OOP

class Class1
    {
        public int id { get; set; }
        public String name { get; set; }
        public void fun()
        {
            MessageBox.Show("id="+id+"; name="+name);

        }
    }



    //調用
    //Class1 cla = new Class1();
    Class1 cla = new Class1(){id = 123,name="張大"};
    //cla.id = 123;
    //cla.name = "張大";
    cla.fun();    
           

靜态方法:

  • 靜态和屬性方法通過static關鍵字修飾
  • 靜态和屬性可以通過類型直接擷取,非靜态則必須通過執行個體化的對象擷取。

靜态類:

  • 靜态類通過static關鍵字修飾
  • 一般情況下類型不需要使用靜态修飾,隻有當類型中存在擴充方法時需要使用靜态類。

7、集合、字典

集合與數組相似,用于存放一組值。

數組的優缺點:

  • 優點:
    • 數組在記憶體中是連續存儲的,是以它的索引速度很快,而且指派與修改元素也很簡單。
  • 缺點
    • 在數組的兩個資料中間插入資料很麻煩
    • 在聲明數組的時候,必須同時指明數組的長度,數組的長度過長,會造成記憶體浪費,數組的長度過短,會造成資料溢出的錯誤。

ArrayList 的使用:

  • ArrayList是.NET Framework提供的用于資料存儲和檢索的專用類
  • 它是命名空間System.Collections下的一部分。
  • 優點:
    • ArrayList的大小是按照其中存儲的資料來動态擴充與收縮的
    • 在聲明ArrayList對象時不需要指定它的長度。
    • ArrayList可以很友善的進行資料的添加、插入和移除。
  • 缺點:
    • ArrayList在存儲資料時使用object類型進行存儲的
    • ArrayList不是類型安全的,使用時很可能出現類型不比對的錯誤
    • 就算都插入同一類型的資料,但在使用的時候,也需要将他們轉化為對應的原類型來處理。
    • ArrayList的存儲存在裝箱和拆箱操作,導緻其性能低下。
  • 泛型:限制隻能存儲單一類型資料的手段
ArrayList array = new ArrayList();
//将資料添加到結尾處
array.Add("abc");
//修改指定索引處的資料
array[0] = 123;
//移除指定索引處的資料
array.RemoveAt(0);
//移除内容為123的資料
array.Remove(123);
//在指定索引處插入資料
array.Insert(0,"插入資料");
           

List集合:

  • List集合與ArrayList由于都繼承了相同的接口,故使用與ArrayList相似。
  • 在聲明List集合時,需要同時為其聲明List集合内資料的對象類型。
  • List<int>  li = new List<int>() 

class Person

    {

        public string name { get; set; }

        public int age { get; set; }

        public int height { get; set; }

    }

List<Person> list = new List<Person>();

Person person = new Person { name = "熊大", age = 20, height = 180 };

list.Add(person);

list.Add(new Person { name = "熊二", age = 18, height = 180 }) ;

Dictionary 字典:

            Dictionary<int, string> dic = new Dictionary<int, string>();

            dic.Add(1, "a");

            dic.Add(2, "b");

            //索引器指派

            dic[1] = "c";

            //初始化指派

            Dictionary<int, string> dic2 = new Dictionary<int, string> { 

                { 1, "a" },

                { 2, "b" },

                { 3, "c" }

            };

            string value = dic2[1];

            //删除鍵值為1的鍵值對

            dic2.Remove(1);

foreach的使用:

  • foreach操作數組

            int[] arrs = { 1, 2, 3, 4, 5, 6 };

            foreach(int arr in arrs)

            {

                int s = arr;

            }

  • foreach操作集合

            List<int> list = new List<int>() {1,2,3,4,5 };

            foreach (int l in list) {

                int a = l;

            }

  • foreach操作字典

            Dictionary<string,string> dict = new Dictionary<string, string>()

            {

                {"a","A" },

                {"b","B" },

                {"c","C" },

            };

            foreach(KeyValuePair<string,string> d in dict)

            {

                string key = d.Key;

                string value = d.Value;

            }

  • foreach操作對象集合

           class Person

           {

                  public string name { get; set; }

                  public int age { get; set; }

                  public int height { get; set; }

           }

            List<Person> personList = new List<Person>();

            personList.Add(new Person { name = "A", age = 1, height = 10 });

            personList.Add(new Person { name = "B", age = 2, height = 20 });

            personList.Add(new Person { name = "C", age = 3, height = 30 });

            foreach(Person p in personList)

            {

                string name = p.name;

                int age = p.age;

                int height = p.height;

            }

c#