C#泛型詳解

来源:https://www.cnblogs.com/FoR_Oscar/archive/2019/07/15/11189337.html
-Advertisement-
Play Games

這篇文章主要講解C#中的泛型,泛型在C#中有很重要的地位,尤其是在搭建項目框架的時候。 一、什麼是泛型 泛型是C#2.0推出的新語法,不是語法糖,而是2.0由框架升級提供的功能。 我們在編程程式時,經常會遇到功能非常相似的模塊,只是它們處理的數據不一樣。但我們沒有辦法,只能分別寫多個方法來處理不同的 ...


這篇文章主要講解C#中的泛型,泛型在C#中有很重要的地位,尤其是在搭建項目框架的時候。

一、什麼是泛型

泛型是C#2.0推出的新語法,不是語法糖,而是2.0由框架升級提供的功能。

我們在編程程式時,經常會遇到功能非常相似的模塊,只是它們處理的數據不一樣。但我們沒有辦法,只能分別寫多個方法來處理不同的數據類型。這個時候,那麼問題來了,有沒有一種辦法,用同一個方法來處理傳入不同種類型參數的辦法呢?泛型的出現就是專門來解決這個問題的。

二、為什麼使用泛型

先來看下麵一個例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class CommonMethod
{
/// <summary>
/// 列印個int值
///
/// 因為方法聲明的時候,寫死了參數類型
/// 已婚的男人 Eleven San
/// </summary>
/// <param name="iParameter"></param>
public static void ShowInt(int iParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, iParameter.GetType().Name, iParameter);
}

/// <summary>
/// 列印個string值
/// </summary>
/// <param name="sParameter"></param>
public static void ShowString(string sParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, sParameter.GetType().Name, sParameter);
}

/// <summary>
/// 列印個DateTime值
/// </summary>
/// <param name="oParameter"></param>
public static void ShowDateTime(DateTime dtParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod).Name, dtParameter.GetType().Name, dtParameter);
}
}
}

結果:

從上面的結果中我們可以看出這三個方法,除了傳入的參數不同外,其裡面實現的功能都是一樣的。在1.0版的時候,還沒有泛型這個概念,那麼怎麼辦呢。相信很多人會想到了OOP三大特性之一的繼承,我們知道,C#語言中,object是所有類型的基類,將上面的代碼進行以下優化:

public static void ShowObject(object oParameter)
{
      Console.WriteLine("This is {0},parameter={1},type={2}",
         typeof(CommonMethod), oParameter.GetType().Name, oParameter);
}

 結果:

從上面的結果中我們可以看出,使用Object類型達到了我們的要求,解決了代碼的可復用。可能有人會問定義的是object類型的,為什麼可以傳入int、string等類型呢?原因有二:

1、object類型是一切類型的父類。

2、通過繼承,子類擁有父類的一切屬性和行為,任何父類出現的地方,都可以用子類來代替。

但是上面object類型的方法又會帶來另外一個問題:裝箱和拆箱,會損耗程式的性能。

微軟在C#2.0的時候推出了泛型,可以很好的解決上面的問題。

三、泛型類型參數

在泛型類型或方法定義中,類型參數是在其實例化泛型類型的一個變數時,客戶端指定的特定類型的占位符。 泛型類( GenericList<T>)無法按原樣使用,因為它不是真正的類型;它更像是類型的藍圖。 若要使用 GenericList<T>,客戶端代碼必須通過指定尖括弧內的類型參數來聲明並實例化構造類型。 此特定類的類型參數可以是編譯器可識別的任何類型。 可創建任意數量的構造類型實例,其中每個使用不同的類型參數。

上面例子中的代碼可以修改如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class GenericMethod
{
/// <summary>
/// 泛型方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tParameter"></param>
public static void Show<T>(T tParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(GenericMethod), tParameter.GetType().Name, tParameter.ToString());
}
}
}

調用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
class Program
{
static void Main(string[] args)
{

int iValue = 123;
string sValue = "456";
DateTime dtValue = DateTime.Now;

Console.WriteLine("***********CommonMethod***************");
CommonMethod.ShowInt(iValue);
CommonMethod.ShowString(sValue);
CommonMethod.ShowDateTime(dtValue);
Console.WriteLine("***********Object***************");
CommonMethod.ShowObject(iValue);
CommonMethod.ShowObject(sValue);
CommonMethod.ShowObject(dtValue);
Console.WriteLine("***********Generic***************");
GenericMethod.Show<int>(iValue);
GenericMethod.Show<string>(sValue);
GenericMethod.Show<DateTime>(dtValue);
Console.ReadKey();
}
}
}

顯示結果:

為什麼泛型可以解決上面的問題呢?

泛型是延遲聲明的:即定義的時候沒有指定具體的參數類型,把參數類型的聲明推遲到了調用的時候才指定參數類型。 延遲思想在程式架構設計的時候很受歡迎。例如:分散式緩存隊列、EF的延遲載入等等。

泛型究竟是如何工作的呢?

控制台程式最終會編譯成一個exe程式,exe被點擊的時候,會經過JIT(即時編譯器)的編譯,最終生成二進位代碼,才能被電腦執行。泛型加入到語法以後,VS自帶的編譯器又做了升級,升級之後編譯時遇到泛型,會做特殊的處理:生成占位符。再次經過JIT編譯的時候,會把上面編譯生成的占位符替換成具體的數據類型。請看下麵一個例子:

1 Console.WriteLine(typeof(List<>));
2 Console.WriteLine(typeof(Dictionary<,>));

 結果:

從上面的截圖中可以看出:泛型在編譯之後會生成占位符。

註意:占位符需要在英文輸入法狀態下才能輸入,只需要按一次波浪線(數字1左邊的鍵位)的鍵位即可,不需要按Shift鍵。

1、泛型性能問題

請看一下的一個例子,比較普通方法、Object參數類型的方法、泛型方法的性能。

添加一個Monitor類,讓三種方法執行同樣的操作,比較用時長短:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class Monitor
{
public static void Show()
{
Console.WriteLine("****************Monitor******************");
{
int iValue = 12345;
long commonSecond = 0;
long objectSecond = 0;
long genericSecond = 0;

{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000000; i++)
{
ShowInt(iValue);
}
watch.Stop();
commonSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000000; i++)
{
ShowObject(iValue);
}
watch.Stop();
objectSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000000; i++)
{
Show<int>(iValue);
}
watch.Stop();
genericSecond = watch.ElapsedMilliseconds;
}
Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}"
, commonSecond, objectSecond, genericSecond);
}
}

#region PrivateMethod
private static void ShowInt(int iParameter)
{
//do nothing
}
private static void ShowObject(object oParameter)
{
//do nothing
}
private static void Show<T>(T tParameter)
{
//do nothing
}
#endregion

}
}

Main()方法調用:

1 Monitor.Show();

 結果:

從結果中可以看出:泛型方法的性能最高,其次是普通方法,object方法的性能最低。

四、泛型類

除了方法可以是泛型以外,類也可以是泛型的,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
/// <summary>
/// 泛型類
/// </summary>
/// <typeparam name="T"></typeparam>
public class GenericClass<T>
{
public T _T;
}
}

Main()方法中調用:

// T是int類型
GenericClass<int> genericInt = new GenericClass<int>();
genericInt._T = 123;
// T是string類型
GenericClass<string> genericString = new GenericClass<string>();
genericString._T = "123";

除了可以有泛型類,也可以有泛型介面,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
/// <summary>
/// 泛型介面
/// </summary>
public interface IGenericInterface<T>
{
//泛型類型的返回值
T GetT(T t);
}
}

也可以有泛型委托:

1 public delegate void SayHi<T>(T t);//泛型委托

 註意:

1、泛型在聲明的時候可以不指定具體的類型,但是在使用的時候必須指定具體類型,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
/// <summary>
/// 使用泛型的時候必須指定具體類型,
/// 這裡的具體類型是int
/// </summary>
public class CommonClass :GenericClass<int>
{
}
}

如果子類也是泛型的,那麼繼承的時候可以不指定具體類型,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
/// <summary>
/// 使用泛型的時候必須指定具體類型,
/// 這裡的具體類型是int
/// </summary>
public class CommonClass :GenericClass<int>
{
}

/// <summary>
/// 子類也是泛型的,繼承的時候可以不指定具體類型
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommonClassChild<T>:GenericClass<T>
{

}
}

2、類實現泛型介面也是這種情況,例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
/// <summary>
/// 必須指定具體類型
/// </summary>
public class Common : IGenericInterface<string>
{
public string GetT(string t)
{
throw new NotImplementedException();
}
}

/// <summary>
/// 可以不知道具體類型,但是子類也必須是泛型的
/// </summary>
/// <typeparam name="T"></typeparam>
public class CommonChild<T> : IGenericInterface<T>
{
public T GetT(T t)
{
throw new NotImplementedException();
}
}
}

五、泛型約束

先來看看下麵的一個例子:

定義一個People類,裡面有屬性和方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public interface ISports
{
void Pingpang();
}

public interface IWork
{
void Work();
}


public class People
{
public int Id { get; set; }
public string Name { get; set; }

public void Hi()
{
Console.WriteLine("Hi");
}
}

public class Chinese : People, ISports, IWork
{
public void Tradition()
{
Console.WriteLine("仁義禮智信,溫良恭儉讓");
}
public void SayHi()
{
Console.WriteLine("吃了麽?");
}

public void Pingpang()
{
Console.WriteLine("打乒乓球...");
}

public void Work()
{
throw new NotImplementedException();
}
}

public class Hubei : Chinese
{
public Hubei(int version)
{ }

public string Changjiang { get; set; }
public void Majiang()
{
Console.WriteLine("打麻將啦。。");
}
}


public class Japanese : ISports
{
public int Id { get; set; }
public string Name { get; set; }
public void Hi()
{
Console.WriteLine("Hi");
}
public void Pingpang()
{
Console.WriteLine("打乒乓球...");
}
}
}

在Main()方法裡面實例化:

People people = new People()
{
Id = 123,
Name = "走自己的路"
};
Chinese chinese = new Chinese()
{
Id = 234,
Name = "晴天"
};
Hubei hubei = new Hubei(123)
{
Id = 345,
Name = "流年"
};
Japanese japanese = new Japanese()
{
Id = 7654,
Name = "werwer"
};

這時有一個需求:需要列印出Id和Name屬性的值,將ShowObject()方法修改如下:

但是這樣修改報錯了:object類裡面沒有Id和Name屬性,可能會有人說,強制類型轉換一下就行了啊:

public static void ShowObject(object oParameter)
{
Console.WriteLine("This is {0},parameter={1},type={2}",
typeof(CommonMethod), oParameter.GetType().Name, oParameter);

Console.WriteLine($"{((People)oParameter).Id}_{((People)oParameter).Name}");
}

這樣修改以後,代碼不會報錯了,這時我們在Main()方法裡面調用:

1 CommonMethod.ShowObject(people);
2 CommonMethod.ShowObject(chinese);
3 CommonMethod.ShowObject(hubei);
4 CommonMethod.ShowObject(japanese);

 結果:

可以看出程式報錯了,因為Japanese沒有繼承自People,這裡類型轉換的時候失敗了。這樣會造成類型不安全的問題。那麼怎麼解決類型不安全的問題呢?那就是使用泛型約束。

所謂的泛型約束,實際上就是約束的類型T。使T必須遵循一定的規則。比如T必須繼承自某個類,或者T必須實現某個介面等等。那麼怎麼給泛型指定約束?其實也很簡單,只需要where關鍵字,加上約束的條件。

泛型約束總共有五種。

約束 s說明
T:結構 類型參數必須是值類型
T:類 類型參數必須是引用類型;這一點也適用於任何類、介面、委托或數組類型。
T:new() 類型參數必須具有無參數的公共構造函數。 當與其他約束一起使用時,new() 約束必須最後指定。
T:<基類名> 類型參數必須是指定的基類或派生自指定的基類。
T:<介面名稱> 類型參數必須是指定的介面或實現指定的介面。 可以指定多個介面約束。 約束介面也可以是泛型的。

1、基類約束

上面列印的方法約束T類型必須是People類型。

/// <summary>
/// 基類約束:約束T必須是People類型或者是People的子類
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="tParameter"></param>
public static void Show<T>(T tParameter) where T : People
{
Console.WriteLine($"{tParameter.Id}_{tParameter.Name}");
tParameter.Hi();
}

註意:

基類約束時,基類不能是密封類,即不能是sealed類。sealed類表示該類不能被繼承,在這裡用作約束就無任何意義,因為sealed類沒有子類。

2、介面約束

/// <summary>
/// 介面約束
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T Get<T>(T t) where T : ISports
{
t.Pingpang();
return t;
}

3、引用類型約束 class

引用類型約束保證T一定是引用類型的。

/// <summary>
/// 引用類型約束
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T Get<T>(T t) where T : class
{
return t;
}

4、值類型約束  struct

值類型約束保證T一定是值類型的。

/// <summary>
/// 值類型類型約束
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T Get<T>(T t) where T : struct
{
return t;
}

5、無參數構造函數約束  new()

/// <summary>
/// new()約束
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static T Get<T>(T t) where T : new()
{
return t;
}

泛型約束也可以同時約束多個,例如:

public static void Show<T>(T tParameter)
where T : People, ISports, IWork, new()
{
Console.WriteLine($"{tParameter.Id}_{tParameter.Name}");
tParameter.Hi();
tParameter.Pingpang();
tParameter.Work();
}

註意:有多個泛型約束時,new()約束一定是在最後。

六、泛型的協變和逆變

協變和逆變是在.NET 4.0的時候出現的,只能放在介面或者委托的泛型參數前面,out 協變covariant,用來修飾返回值;in:逆變contravariant,用來修飾傳入參數。

先看下麵的一個例子:

定義一個Animal類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class Animal
{
public int Id { get; set; }
}
}

然後在定義一個Cat類繼承自Animal類:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class Cat :Animal
{
public string Name { get; set; }
}
}

在Main()方法可以這樣調用:

// 直接聲明Animal類
Animal animal = new Animal();
// 直接聲明Cat類
Cat cat = new Cat();
// 聲明子類對象指向父類
Animal animal2 = new Cat();
// 聲明Animal類的集合
List<Animal> listAnimal = new List<Animal>();
// 聲明Cat類的集合
List<Cat> listCat = new List<Cat>();

那麼問題來了:下麵的一句代碼是不是正確的呢?

1 List<Animal> list = new List<Cat>();

 可能有人會認為是正確的:因為一隻Cat屬於Animal,那麼一群Cat也應該屬於Animal啊。但是實際上這樣聲明是錯誤的:因為List<Cat>和List<Animal>之間沒有父子關係。

這時就可以用到協變和逆變了。

1 // 協變
2 IEnumerable<Animal> List1 = new List<Animal>();
3 IEnumerable<Animal> List2 = new List<Cat>();

 F12查看定義:

可以看到,在泛型介面的T前面有一個out關鍵字修飾,而且T只能是返回值類型,不能作為參數類型,這就是協變。使用了協變以後,左邊聲明的是基類,右邊可以聲明基類或者基類的子類。

協變除了可以用在介面上面,也可以用在委托上面:

1 Func<Animal> func = new Func<Cat>(() => null);

 除了使用.NET框架定義好的以為,我們還可以自定義協變,例如:

/// <summary>
/// out 協變 只能是返回結果
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICustomerListOut<out T>
{
T Get();
}

public class CustomerListOut<T> : ICustomerListOut<T>
{
public T Get()
{
return default(T);
}
}

使用自定義的協變:

1 // 使用自定義協變
2 ICustomerListOut<Animal> customerList1 = new CustomerListOut<Animal>();
3 ICustomerListOut<Animal> customerList2 = new CustomerListOut<Cat>();

 在來看看逆變。

在泛型介面的T前面有一個In關鍵字修飾,而且T只能方法參數,不能作為返回值類型,這就是逆變。請看下麵的自定義逆變:

/// <summary>
/// 逆變 只能是方法參數
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICustomerListIn<in T>
{
void Show(T t);
}

public class CustomerListIn<T> : ICustomerListIn<T>
{
public void Show(T t)
{
}
}

使用自定義逆變:

1 // 使用自定義逆變
2 ICustomerListIn<Cat> customerListCat1 = new CustomerListIn<Cat>();
3 ICustomerListIn<Cat> customerListCat2 = new CustomerListIn<Animal>();

協變和逆變也可以同時使用,看看下麵的例子:

/// <summary>
/// inT 逆變
/// outT 協變
/// </summary>
/// <typeparam name="inT"></typeparam>
/// <typeparam name="outT"></typeparam>
public interface IMyList<in inT, out outT>
{
void Show(inT t);
outT Get();
outT Do(inT t);
}

public class MyList<T1, T2> : IMyList<T1, T2>
{

public void Show(T1 t)
{
Console.WriteLine(t.GetType().Name);
}

public T2 Get()
{
Console.WriteLine(typeof(T2).Name);
return default(T2);
}

public T2 Do(T1 t)
{
Console.WriteLine(t.GetType().Name);
Console.WriteLine(typeof(T2).Name);
return default(T2);
}
}

使用:

1 IMyList<Cat, Animal> myList1 = new MyList<Cat, Animal>();
2 IMyList<Cat, Animal> myList2 = new MyList<Cat, Cat>();//協變
3 IMyList<Cat, Animal> myList3 = new MyList<Animal, Animal>();//逆變
4 IMyList<Cat, Animal> myList4 = new MyList<Animal, Cat>();//逆變+協變

 七、泛型緩存

在前面我們學習過,類中的靜態類型無論實例化多少次,在記憶體中只會有一個。靜態構造函數只會執行一次。在泛型類中,T類型不同,每個不同的T類型,都會產生一個不同的副本,所以會產生不同的靜態屬性、不同的靜態構造函數,請看下麵的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyGeneric
{
public class GenericCache<T>
{
static GenericCache()
{
Console.WriteLine("This is GenericCache 靜態構造函數");
_TypeTime = string.Format("{0}_{1}", typeof(T).FullName, DateTime.Now.ToString("yyyyMMddHHmmss.fff"));
}

private static string _TypeTime = "";

public static string GetCache()
{
return _TypeTime;
}
}
}

然後新建一個測試類,用來測試GenericCache類的執行順序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MyGeneric
{
public class GenericCacheTest
{
public static void Show()
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine(GenericCache<int>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<long>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<DateTime>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<string>.GetCache());
Thread.Sleep(10);
Console.WriteLine(GenericCache<GenericCacheTest>.GetCache());
Thread.Sleep(10);
}
}
}
}

Main()方法裡面調用:

1 GenericCacheTest.Show();

結果:

從上面的截圖中可以看出,泛型會為不同的類型都創建一個副本,所以靜態構造函數會執行5次。 而且每次靜態屬性的值都是一樣的。利用泛型的這一特性,可以實現緩存。

註意:只能為不同的類型緩存一次。泛型緩存比字典緩存效率高。泛型緩存不能主動釋放


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 數組元素查找(查找指定元素第一次在數組中出現的索引): 結果: ...
  • 8.4 粘包問題 粘包問題發生的原因: 1.發送端需要等緩衝區滿才發送出去,造成粘包(發送數據時間間隔很短,數據了很小,會合到一起,產生粘包),這樣接收端,就難於分辨出來了,必須提供科學的拆包機制。 即面向流的通信是無消息保護邊界的。 2.接收方不及時接收緩衝區的包,造成多個包接收(客戶端發送了一段 ...
  • 1.python介紹 Python(英國發音:/ˈpaɪθən/ 美國發音:/ˈpaɪθɑːn/)是一種廣泛使用的解釋型、高級編程、通用型編程語言,由吉多、範羅蘇姆創造,第一版發佈於1991年。可以視之為一種改良(加入一些其他編程語言的優點,如面向對象)的lisp。Python的設計哲學強調代碼的可 ...
  • 題目 "String painter " 給出兩個字元串s1,s2。對於每次操作可以將 s1 串中的任意一個子段變成另一個字元。問最少需要多少步操作能將s1串變為s2串。 解析 太妙了這個題,mark一下。 這個題先考慮怎麼由空串轉化s2, $f[i][j]$表示從空串到s2最少的次數, 則有$f[ ...
  • 3.9各類型數據方法補充,轉換,分類,編碼,坑中菜 3.9.1數據類型方法補充 1.str:不可變 補充方法 1. s1.capitalize():首字母大寫 2. s1.title(): 每個單詞首字母大寫 3. s1.swapcase():大小寫反轉 4. s1.center():居中 填充 5 ...
  • 1.各種斷言方法 常用斷言方法: 方法 用途 assertEqual(a, b) 核實a == b assertNotEqual(a, b) 核實a != b assertTrue(x) 核實x為True assertFalse(x) 核實x為False asseertIn(item, list) ...
  • Github有一個經過重寫的微信小程式SignalR的js類庫 https://github.com/liangshiw/SignalRMiniProgram-Client 於是我把他改成支付寶小程式的版本,上面這個項目的核心代碼基本沒有變,只是小程式開放介面改了一下,在支付寶小程式就能跑起來了 把 ...
  • App_Code 下創建的.cs文件僅僅是“內容”不是代碼。設置文件為“編譯”就可正常引用。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...