{"id":94161,"date":"2026-08-13T21:08:50","date_gmt":"2026-08-13T13:08:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94161.html"},"modified":"2026-08-13T21:08:50","modified_gmt":"2026-08-13T13:08:50","slug":"c-%e6%b3%9b%e5%9e%8b%ef%bc%9a%e4%bb%8e%e5%9f%ba%e7%a1%80%e5%88%b0%e9%ab%98%e7%ba%a7%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94161.html","title":{"rendered":"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528"},"content":{"rendered":"<h3>1. \u4ec0\u4e48\u662f\u6cdb\u578b&#xff1f;<\/h3>\n<p>\u6cdb\u578b&#xff08;Generics&#xff09;\u662f C# 2.0 \u5f15\u5165\u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u5b83\u5141\u8bb8\u5728\u5b9a\u4e49\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u65f6\u4f7f\u7528\u7c7b\u578b\u53c2\u6570&#xff0c;\u4ece\u800c\u521b\u5efa\u53ef\u91cd\u7528\u3001\u7c7b\u578b\u5b89\u5168\u4e14\u9ad8\u6027\u80fd\u7684\u4ee3\u7801\u7ec4\u4ef6\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;\u6cdb\u578b\u8ba9\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u80fd\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4ee3\u7801\u6a21\u677f&#xff0c;\u800c\u65e0\u9700\u4e3a\u6bcf\u79cd\u7c7b\u578b\u91cd\u590d\u7f16\u5199\u76f8\u4f3c\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u6ca1\u6709\u6cdb\u578b\u4e4b\u524d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528 object \u7c7b\u578b\u6765\u5b9e\u73b0\u901a\u7528\u5bb9\u5668&#xff08;\u5982 ArrayList&#xff09;&#xff0c;\u4f46\u8fd9\u4f1a\u5bfc\u81f4&#xff1a;<\/p>\n<ul>\n<li>\u7c7b\u578b\u4e0d\u5b89\u5168&#xff1a;\u7f16\u8bd1\u65f6\u65e0\u6cd5\u68c0\u67e5\u7c7b\u578b&#xff0c;\u8fd0\u884c\u65f6\u53ef\u80fd\u629b\u51fa InvalidCastException\u3002<\/li>\n<li>\u6027\u80fd\u635f\u5931&#xff1a;\u503c\u7c7b\u578b\u9700\u8981\u9891\u7e41\u88c5\u7bb1&#xff08;boxing&#xff09;\u548c\u62c6\u7bb1&#xff08;unboxing&#xff09;\u3002<\/li>\n<li>\u4ee3\u7801\u5197\u4f59&#xff1a;\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7f16\u5199\u51e0\u4e4e\u76f8\u540c\u7684\u7c7b\u6216\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u6cdb\u578b\u5b8c\u7f8e\u89e3\u51b3\u4e86\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<h3>2. \u6cdb\u578b\u7684\u57fa\u672c\u8bed\u6cd5<\/h3>\n<h4>2.1 \u6cdb\u578b\u7c7b<\/h4>\n<p>\u5728\u7c7b\u540d\u540e\u4f7f\u7528\u5c16\u62ec\u53f7 &lt;T&gt; \u58f0\u660e\u7c7b\u578b\u53c2\u6570&#xff1a;<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u6cdb\u578b\u5bb9\u5668\u7c7b<br \/>\npublic class GenericList&lt;T&gt;<br \/>\n{<br \/>\n    private T[] items;<br \/>\n    private int count &#061; 0;<\/p>\n<p>    public GenericList(int capacity)<br \/>\n    {<br \/>\n        items &#061; new T[capacity];<br \/>\n    }<\/p>\n<p>    public void Add(T item)<br \/>\n    {<br \/>\n        if (count &lt; items.Length)<br \/>\n        {<br \/>\n            items[count] &#061; item;<br \/>\n            count&#043;&#043;;<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public T Get(int index)<br \/>\n    {<br \/>\n        if (index &gt;&#061; 0 &amp;&amp; index &lt; count)<br \/>\n            return items[index];<br \/>\n        throw new IndexOutOfRangeException();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u4f7f\u7528\u793a\u4f8b<br \/>\nGenericList&lt;int&gt; intList &#061; new GenericList&lt;int&gt;(10);<br \/>\nintList.Add(42);<br \/>\nint value &#061; intList.Get(0); \/\/ \u7c7b\u578b\u5b89\u5168&#xff0c;\u65e0\u9700\u5f3a\u5236\u8f6c\u6362<\/p>\n<p>GenericList&lt;string&gt; stringList &#061; new GenericList&lt;string&gt;(5);<br \/>\nstringList.Add(&#034;Hello&#034;);<br \/>\nstring text &#061; stringList.Get(0);<\/p>\n<h4>2.2 \u6cdb\u578b\u65b9\u6cd5<\/h4>\n<p>\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u72ec\u7acb\u4f7f\u7528\u6cdb\u578b&#xff0c;\u5373\u4f7f\u6240\u5728\u7c7b\u4e0d\u662f\u6cdb\u578b\u7c7b&#xff1a;<\/p>\n<p>public class Utility<br \/>\n{<br \/>\n    \/\/ \u6cdb\u578b\u65b9\u6cd5&#xff1a;\u4ea4\u6362\u4e24\u4e2a\u53d8\u91cf\u7684\u503c<br \/>\n    public static void Swap&lt;T&gt;(ref T a, ref T b)<br \/>\n    {<br \/>\n        T temp &#061; a;<br \/>\n        a &#061; b;<br \/>\n        b &#061; temp;<br \/>\n    }<\/p>\n<p>    \/\/ \u4f7f\u7528\u793a\u4f8b<br \/>\n    public static void Demo()<br \/>\n    {<br \/>\n        int x &#061; 10, y &#061; 20;<br \/>\n        Swap(ref x, ref y); \/\/ \u7f16\u8bd1\u5668\u63a8\u65ad T \u4e3a int<\/p>\n<p>        string s1 &#061; &#034;first&#034;, s2 &#061; &#034;second&#034;;<br \/>\n        Swap(ref s1, ref s2); \/\/ T \u4e3a string<br \/>\n    }<br \/>\n}<\/p>\n<h4>2.3 \u6cdb\u578b\u63a5\u53e3<\/h4>\n<p>\u63a5\u53e3\u540c\u6837\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u578b\u53c2\u6570&#xff1a;<\/p>\n<p>public interface IRepository&lt;T&gt;<br \/>\n{<br \/>\n    T GetById(int id);<br \/>\n    void Add(T entity);<br \/>\n    void Update(T entity);<br \/>\n    void Delete(int id);<br \/>\n}<\/p>\n<p>\/\/ \u5b9e\u73b0\u6cdb\u578b\u63a5\u53e3<br \/>\npublic class UserRepository : IRepository&lt;User&gt;<br \/>\n{<br \/>\n    public User GetById(int id) { \/* \u5b9e\u73b0 *\/ }<br \/>\n    public void Add(User entity) { \/* \u5b9e\u73b0 *\/ }<br \/>\n    public void Update(User entity) { \/* \u5b9e\u73b0 *\/ }<br \/>\n    public void Delete(int id) { \/* \u5b9e\u73b0 *\/ }<br \/>\n}<\/p>\n<h3>3. \u7c7b\u578b\u7ea6\u675f&#xff08;Constraints&#xff09;<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b&#xff0c;\u6cdb\u578b\u7c7b\u578b\u53c2\u6570 T \u53ef\u4ee5\u662f\u4efb\u4f55\u7c7b\u578b\u3002\u4f46\u6709\u65f6\u6211\u4eec\u9700\u8981\u9650\u5236 T \u5fc5\u987b\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6&#xff0c;\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u7ea6\u675f&#xff1a;<\/p>\n<p>\/\/ T \u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b&#xff08;\u7c7b\u3001\u63a5\u53e3\u3001\u59d4\u6258\u3001\u6570\u7ec4&#xff09;<br \/>\npublic class ReferenceTypeClass&lt;T&gt; where T : class<br \/>\n{<br \/>\n    public T Instance { get; set; }<br \/>\n}<\/p>\n<p>\/\/ T \u5fc5\u987b\u662f\u503c\u7c7b\u578b&#xff08;\u7ed3\u6784\u4f53\u3001\u679a\u4e3e&#xff09;&#xff0c;\u4e14\u4e0d\u80fd\u662f\u53ef\u7a7a\u7c7b\u578b<br \/>\npublic struct ValueTypeStruct&lt;T&gt; where T : struct<br \/>\n{<br \/>\n    public T Value { get; set; }<br \/>\n}<\/p>\n<p>\/\/ T \u5fc5\u987b\u5177\u6709\u65e0\u53c2\u6784\u9020\u51fd\u6570<br \/>\npublic class Creatable&lt;T&gt; where T : new()<br \/>\n{<br \/>\n    public T CreateInstance()<br \/>\n    {<br \/>\n        return new T();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ T \u5fc5\u987b\u7ee7\u627f\u81ea\u6307\u5b9a\u57fa\u7c7b\u6216\u5b9e\u73b0\u6307\u5b9a\u63a5\u53e3<br \/>\npublic class Processor&lt;T&gt; where T : IComparable, IFormattable<br \/>\n{<br \/>\n    public int Compare(T a, T b)<br \/>\n    {<br \/>\n        return a.CompareTo(b);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ \u591a\u91cd\u7ea6\u675f<br \/>\npublic class ComplexGeneric&lt;T&gt; where T : class, IComparable&lt;T&gt;, new()<br \/>\n{<br \/>\n    \/\/ T \u5fc5\u987b\u662f\u5f15\u7528\u7c7b\u578b\u3001\u5b9e\u73b0 IComparable&lt;T&gt;\u3001\u4e14\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570<br \/>\n}<\/p>\n<h3>4. \u6cdb\u578b\u96c6\u5408&#xff08;System.Collections.Generic&#xff09;<\/h3>\n<p>.NET Framework \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u6cdb\u578b\u96c6\u5408\u7c7b&#xff0c;\u8fd9\u662f\u6cdb\u578b\u6700\u5e38\u7528\u7684\u573a\u666f&#xff1a;<\/p>\n<p>using System.Collections.Generic;<\/p>\n<p>\/\/ List&lt;T&gt;&#xff1a;\u52a8\u6001\u6570\u7ec4<br \/>\nList&lt;int&gt; numbers &#061; new List&lt;int&gt; { 1, 2, 3, 4, 5 };<br \/>\nnumbers.Add(6);<\/p>\n<p>\/\/ Dictionary&lt;TKey, TValue&gt;&#xff1a;\u952e\u503c\u5bf9\u96c6\u5408<br \/>\nDictionary&lt;string, int&gt; ages &#061; new Dictionary&lt;string, int&gt;<br \/>\n{<br \/>\n    [&#034;Alice&#034;] &#061; 25,<br \/>\n    [&#034;Bob&#034;] &#061; 30<br \/>\n};<br \/>\nint bobAge &#061; ages[&#034;Bob&#034;];<\/p>\n<p>\/\/ Queue&lt;T&gt;&#xff1a;\u5148\u8fdb\u5148\u51fa\u961f\u5217<br \/>\nQueue&lt;string&gt; queue &#061; new Queue&lt;string&gt;();<br \/>\nqueue.Enqueue(&#034;first&#034;);<br \/>\nqueue.Enqueue(&#034;second&#034;);<br \/>\nstring firstItem &#061; queue.Dequeue();<\/p>\n<p>\/\/ Stack&lt;T&gt;&#xff1a;\u540e\u8fdb\u5148\u51fa\u6808<br \/>\nStack&lt;double&gt; stack &#061; new Stack&lt;double&gt;();<br \/>\nstack.Push(3.14);<br \/>\nstack.Push(2.71);<br \/>\ndouble top &#061; stack.Pop();<\/p>\n<p>\/\/ HashSet&lt;T&gt;&#xff1a;\u4e0d\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408<br \/>\nHashSet&lt;string&gt; uniqueNames &#061; new HashSet&lt;string&gt;<br \/>\n{<br \/>\n    &#034;John&#034;, &#034;Jane&#034;, &#034;John&#034; \/\/ \u7b2c\u4e8c\u4e2a &#034;John&#034; \u4e0d\u4f1a\u88ab\u6dfb\u52a0<br \/>\n};<\/p>\n<h3>5. \u534f\u53d8\u4e0e\u9006\u53d8&#xff08;Covariance &amp; Contravariance&#xff09;<\/h3>\n<p>C# 4.0 \u5f15\u5165\u4e86\u6cdb\u578b\u63a5\u53e3\u548c\u59d4\u6258\u7684\u534f\u53d8\u4e0e\u9006\u53d8&#xff0c;\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u7c7b\u578b\u8f6c\u6362&#xff1a;<\/p>\n<h4>5.1 \u534f\u53d8&#xff08;out \u5173\u952e\u5b57&#xff09;<\/h4>\n<p>\u5141\u8bb8\u4f7f\u7528\u6d3e\u751f\u7a0b\u5ea6\u66f4\u5927\u7684\u7c7b\u578b&#xff08;\u66f4\u5177\u4f53\u7684\u7c7b\u578b&#xff09;\u3002\u53ea\u80fd\u7528\u4e8e\u8f93\u51fa\u4f4d\u7f6e&#xff08;\u8fd4\u56de\u503c&#xff09;&#xff1a;<\/p>\n<p>\/\/ IEnumerable&lt;T&gt; \u662f\u534f\u53d8\u7684&#xff1a;IEnumerable&lt;out T&gt;<br \/>\nIEnumerable&lt;string&gt; strings &#061; new List&lt;string&gt; { &#034;a&#034;, &#034;b&#034;, &#034;c&#034; };<br \/>\n\/\/ \u53ef\u4ee5\u5c06 IEnumerable&lt;string&gt; \u8d4b\u503c\u7ed9 IEnumerable&lt;object&gt;<br \/>\nIEnumerable&lt;object&gt; objects &#061; strings; \/\/ \u534f\u53d8&#xff0c;\u56e0\u4e3a string \u6d3e\u751f\u81ea object<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u534f\u53d8\u63a5\u53e3<br \/>\npublic interface IProducer&lt;out T&gt;<br \/>\n{<br \/>\n    T Produce();<br \/>\n}<\/p>\n<p>public class StringProducer : IProducer&lt;string&gt;<br \/>\n{<br \/>\n    public string Produce() &#061;&gt; &#034;Hello&#034;;<br \/>\n}<\/p>\n<p>IProducer&lt;string&gt; stringProducer &#061; new StringProducer();<br \/>\nIProducer&lt;object&gt; objectProducer &#061; stringProducer; \/\/ \u534f\u53d8\u5141\u8bb8<\/p>\n<h4>5.2 \u9006\u53d8&#xff08;in \u5173\u952e\u5b57&#xff09;<\/h4>\n<p>\u5141\u8bb8\u4f7f\u7528\u6d3e\u751f\u7a0b\u5ea6\u66f4\u5c0f\u7684\u7c7b\u578b&#xff08;\u66f4\u901a\u7528\u7684\u7c7b\u578b&#xff09;\u3002\u53ea\u80fd\u7528\u4e8e\u8f93\u5165\u4f4d\u7f6e&#xff08;\u53c2\u6570&#xff09;&#xff1a;<\/p>\n<p>\/\/ IComparer&lt;T&gt; \u662f\u9006\u53d8\u7684&#xff1a;IComparer&lt;in T&gt;<br \/>\nIComparer&lt;object&gt; objectComparer &#061; Comparer&lt;object&gt;.Default;<br \/>\n\/\/ \u53ef\u4ee5\u5c06 IComparer&lt;object&gt; \u8d4b\u503c\u7ed9 IComparer&lt;string&gt;<br \/>\nIComparer&lt;string&gt; stringComparer &#061; objectComparer; \/\/ \u9006\u53d8&#xff0c;\u56e0\u4e3a object \u662f string \u7684\u57fa\u7c7b<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u9006\u53d8\u63a5\u53e3<br \/>\npublic interface IConsumer&lt;in T&gt;<br \/>\n{<br \/>\n    void Consume(T item);<br \/>\n}<\/p>\n<p>public class ObjectConsumer : IConsumer&lt;object&gt;<br \/>\n{<br \/>\n    public void Consume(object item) { }<br \/>\n}<\/p>\n<p>IConsumer&lt;object&gt; objectConsumer &#061; new ObjectConsumer();<br \/>\nIConsumer&lt;string&gt; stringConsumer &#061; objectConsumer; \/\/ \u9006\u53d8\u5141\u8bb8<\/p>\n<h3>6. \u6cdb\u578b\u59d4\u6258<\/h3>\n<p>\u59d4\u6258\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6cdb\u578b&#xff0c;.NET \u5185\u7f6e\u4e86\u5e38\u7528\u7684\u6cdb\u578b\u59d4\u6258&#xff1a;<\/p>\n<p>\/\/ Func&lt;TResult&gt;&#xff1a;\u65e0\u53c2\u6570&#xff0c;\u8fd4\u56de TResult<br \/>\nFunc&lt;int&gt; getRandom &#061; () &#061;&gt; new Random().Next(100);<br \/>\nint randomValue &#061; getRandom();<\/p>\n<p>\/\/ Func&lt;T, TResult&gt;&#xff1a;\u4e00\u4e2a\u53c2\u6570&#xff0c;\u8fd4\u56de TResult<br \/>\nFunc&lt;string, int&gt; stringLength &#061; s &#061;&gt; s.Length;<br \/>\nint length &#061; stringLength(&#034;Hello&#034;);<\/p>\n<p>\/\/ Action&lt;T&gt;&#xff1a;\u4e00\u4e2a\u53c2\u6570&#xff0c;\u65e0\u8fd4\u56de\u503c<br \/>\nAction&lt;string&gt; logger &#061; message &#061;&gt; Console.WriteLine(message);<br \/>\nlogger(&#034;Log message&#034;);<\/p>\n<p>\/\/ Predicate&lt;T&gt;&#xff1a;\u4e00\u4e2a\u53c2\u6570&#xff0c;\u8fd4\u56de bool<br \/>\nPredicate&lt;int&gt; isEven &#061; n &#061;&gt; n % 2 &#061;&#061; 0;<br \/>\nbool result &#061; isEven(4);<\/p>\n<p>\/\/ \u81ea\u5b9a\u4e49\u6cdb\u578b\u59d4\u6258<br \/>\npublic delegate T Transformer&lt;T&gt;(T input);<br \/>\npublic delegate TResult Converter&lt;TInput, TResult&gt;(TInput input);<\/p>\n<p>Transformer&lt;int&gt; square &#061; x &#061;&gt; x * x;<br \/>\nint squared &#061; square(5); \/\/ 25<\/p>\n<h3>7. \u6cdb\u578b\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<ul>\n<li>\u4f18\u5148\u4f7f\u7528\u6cdb\u578b\u96c6\u5408&#xff1a;\u603b\u662f\u4f7f\u7528 List&lt;T&gt;\u3001Dictionary&lt;TKey, TValue&gt; \u7b49\u6cdb\u578b\u96c6\u5408&#xff0c;\u907f\u514d\u4f7f\u7528 ArrayList\u3001Hashtable \u7b49\u975e\u6cdb\u578b\u96c6\u5408\u3002<\/li>\n<li>\u5408\u7406\u4f7f\u7528\u7ea6\u675f&#xff1a;\u53ea\u5728\u5fc5\u8981\u65f6\u6dfb\u52a0\u7ea6\u675f&#xff0c;\u8fc7\u591a\u7684\u7ea6\u675f\u4f1a\u9650\u5236\u6cdb\u578b\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<li>\u8003\u8651\u53ef\u7a7a\u6027&#xff1a;C# 8.0 \u7684\u53ef\u7a7a\u5f15\u7528\u7c7b\u578b\u4e0e\u6cdb\u578b\u7ed3\u5408\u65f6&#xff0c;\u4f7f\u7528 T? \u8bed\u6cd5\u6216 default \u5173\u952e\u5b57\u5904\u7406\u53ef\u80fd\u4e3a null \u7684\u60c5\u51b5\u3002<\/li>\n<li>\u907f\u514d\u8fc7\u5ea6\u6cdb\u5316&#xff1a;\u5982\u679c\u7c7b\u578b\u53c2\u6570\u53ea\u5728\u5c11\u6570\u5730\u65b9\u4f7f\u7528&#xff0c;\u8003\u8651\u4f7f\u7528\u91cd\u8f7d\u65b9\u6cd5\u800c\u975e\u6cdb\u578b\u3002<\/li>\n<li>\u6027\u80fd\u8003\u91cf&#xff1a;\u6cdb\u578b\u5728\u7f16\u8bd1\u65f6\u751f\u6210\u7279\u5b9a\u7c7b\u578b\u7684\u4ee3\u7801&#xff0c;\u907f\u514d\u4e86\u88c5\u7bb1\u62c6\u7bb1&#xff0c;\u6027\u80fd\u63a5\u8fd1\u786c\u7f16\u7801\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<h3>8. \u603b\u7ed3<\/h3>\n<p>C# \u6cdb\u578b\u662f\u73b0\u4ee3 .NET \u5f00\u53d1\u7684\u57fa\u77f3&#xff0c;\u5b83\u63d0\u4f9b\u4e86&#xff1a;<\/p>\n<li>\u7c7b\u578b\u5b89\u5168&#xff1a;\u7f16\u8bd1\u65f6\u7c7b\u578b\u68c0\u67e5&#xff0c;\u51cf\u5c11\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/li>\n<li>\u4ee3\u7801\u91cd\u7528&#xff1a;\u4e00\u5957\u4ee3\u7801\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>\u6027\u80fd\u4f18\u5316&#xff1a;\u907f\u514d\u88c5\u7bb1\u62c6\u7bb1&#xff0c;\u751f\u6210\u9ad8\u6548\u7684\u7279\u5316\u4ee3\u7801\u3002<\/li>\n<li>\u4e30\u5bcc\u7684\u751f\u6001\u7cfb\u7edf&#xff1a;LINQ\u3001\u96c6\u5408\u7c7b\u3001\u5f02\u6b65\u7f16\u7a0b\u7b49\u90fd\u6df1\u5ea6\u4f9d\u8d56\u6cdb\u578b\u3002<\/li>\n<p>\u638c\u63e1\u6cdb\u578b\u4e0d\u4ec5\u80fd\u8ba9\u4f60\u7684\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u548c\u9ad8\u6548&#xff0c;\u4e5f\u662f\u7406\u89e3 .NET \u9ad8\u7ea7\u7279\u6027&#xff08;\u5982 LINQ\u3001\u5f02\u6b65\u6d41\u3001\u4f9d\u8d56\u6ce8\u5165\u5bb9\u5668&#xff09;\u7684\u5fc5\u8981\u524d\u63d0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4ec0\u4e48\u662f\u6cdb\u578b&#xff1f;\u6cdb\u578b&#xff08;Generics&#xff09;\u662f C# 2.0 \u5f15\u5165\u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u5b83\u5141\u8bb8\u5728\u5b9a\u4e49\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u65f6\u4f7f\u7528\u7c7b\u578b\u53c2\u6570&#xff0c;\u4ece\u800c\u521b\u5efa\u53ef\u91cd\u7528\u3001\u7c7b\u578b\u5b89\u5168\u4e14\u9ad8\u6027\u80fd\u7684\u4ee3\u7801\u7ec4\u4ef6\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;\u6cdb\u578b\u8ba9\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u80fd\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4ee3\u7801\u6a21\u677f&#xff0c;\u800c\u65e0\u9700\u4e3a\u6bcf\u79cd\u7c7b\u578b\u91cd\u590d\u7f16\u5199\u76f8\u4f3c\u7684\u4ee3\u7801\u3002\u5728\u6ca1\u6709\u6cdb\u578b\u4e4b\u524d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528 object \u7c7b\u578b\u6765\u5b9e\u73b0\u901a\u7528\u5bb9\u5668&#xff08;\u5982 ArrayList&amp;#xff<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,190],"topic":[],"class_list":["post-94161","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/94161.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"1. \u4ec0\u4e48\u662f\u6cdb\u578b&#xff1f;\u6cdb\u578b&#xff08;Generics&#xff09;\u662f C# 2.0 \u5f15\u5165\u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u5b83\u5141\u8bb8\u5728\u5b9a\u4e49\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u65f6\u4f7f\u7528\u7c7b\u578b\u53c2\u6570&#xff0c;\u4ece\u800c\u521b\u5efa\u53ef\u91cd\u7528\u3001\u7c7b\u578b\u5b89\u5168\u4e14\u9ad8\u6027\u80fd\u7684\u4ee3\u7801\u7ec4\u4ef6\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;\u6cdb\u578b\u8ba9\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u80fd\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4ee3\u7801\u6a21\u677f&#xff0c;\u800c\u65e0\u9700\u4e3a\u6bcf\u79cd\u7c7b\u578b\u91cd\u590d\u7f16\u5199\u76f8\u4f3c\u7684\u4ee3\u7801\u3002\u5728\u6ca1\u6709\u6cdb\u578b\u4e4b\u524d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528 object \u7c7b\u578b\u6765\u5b9e\u73b0\u901a\u7528\u5bb9\u5668&#xff08;\u5982 ArrayList&amp;#xff\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94161.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-13T13:08:50+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94161.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94161.html\",\"name\":\"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-13T13:08:50+00:00\",\"dateModified\":\"2026-08-13T13:08:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94161.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94161.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94161.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/94161.html","og_locale":"zh_CN","og_type":"article","og_title":"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"1. \u4ec0\u4e48\u662f\u6cdb\u578b&#xff1f;\u6cdb\u578b&#xff08;Generics&#xff09;\u662f C# 2.0 \u5f15\u5165\u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u5b83\u5141\u8bb8\u5728\u5b9a\u4e49\u7c7b\u3001\u63a5\u53e3\u3001\u65b9\u6cd5\u65f6\u4f7f\u7528\u7c7b\u578b\u53c2\u6570&#xff0c;\u4ece\u800c\u521b\u5efa\u53ef\u91cd\u7528\u3001\u7c7b\u578b\u5b89\u5168\u4e14\u9ad8\u6027\u80fd\u7684\u4ee3\u7801\u7ec4\u4ef6\u3002\u7b80\u5355\u6765\u8bf4&#xff0c;\u6cdb\u578b\u8ba9\u4f60\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u80fd\u5904\u7406\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u4ee3\u7801\u6a21\u677f&#xff0c;\u800c\u65e0\u9700\u4e3a\u6bcf\u79cd\u7c7b\u578b\u91cd\u590d\u7f16\u5199\u76f8\u4f3c\u7684\u4ee3\u7801\u3002\u5728\u6ca1\u6709\u6cdb\u578b\u4e4b\u524d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f7f\u7528 object \u7c7b\u578b\u6765\u5b9e\u73b0\u901a\u7528\u5bb9\u5668&#xff08;\u5982 ArrayList&amp;#xff","og_url":"https:\/\/www.wsisp.com\/helps\/94161.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-13T13:08:50+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94161.html","url":"https:\/\/www.wsisp.com\/helps\/94161.html","name":"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-13T13:08:50+00:00","dateModified":"2026-08-13T13:08:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94161.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94161.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94161.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C# \u6cdb\u578b\uff1a\u4ece\u57fa\u7840\u5230\u9ad8\u7ea7\u5e94\u7528"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94161","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=94161"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94161\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94161"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}