C#中的通用编程教程

150 阅读4分钟

C#中的通用编程

在C#中,集合是指将对象群放在一起的类。.NET库提供了有用的系统定义类,提供了各种集合和功能。

目标

在这篇文章中,我们将讨论通用集合,其中包括列表、字典和哈希集。

绪论

在任何现代系统中,数据处理是主要目标之一。开发人员以不同的方式处理数据。

一些系统采用通用方法,包括使用列表、字典和哈希集。其他语言更喜欢非通用集合,即数组、数组列表和比特数组。

非泛型编程表示一个对象的有序集合,可以单独进行索引。

请注意,泛型编程是强类型的,而非泛型编程则不是。这意味着,泛型编程在处理不同的数据类型上执行严格的规则。

今天,软件系统处理大量的数据。这比存储单个条目更有用。然而,处理所有这些数据要困难得多。C#引入了通用集合来帮助解决这个问题。

Generic classes 给用户提供了定义 和 与占位符的能力。classes methods

了解集合

在C#中,collections ,是将对象组放在一起的类。

.NET 库提供了有用的系统定义的类,如集合命名空间,提供不同的功能。

下面将讨论C#命名空间中三种常见的通用集合。

1.列表

列表是最常用的通用集合。它是数组的一个很好的替代品。

列表是一个通用的数据结构,每个条目都占据着一个特定的位置,称为索引。列表很灵活,因为它们可以在每次添加数据时自动调整大小。

列表的另一个优点是它们很容易操作。它们包含许多强大的方法,从用户那里抽象出复杂的工作。

列表按顺序存储数据,但也可以接受重复的条目。

在C#中声明了一个通用的列表类,如下图所示。

public class genericList<T>

{
   // Code goes here 
    void add(T input){}
}

列表可以存储不同的数据类型,包括整数和字符串,如下图所示。

using System;
using System.collection.generic; // A system namespace for implementing generic lists.

private class genericListExample static void Main () {
   
    GenericList<int> list1 = new GenericList<int>(); // This declares a list of type int.
    
    GenericList<string> list2 = new GenericList<string>(); //This declares a list of type string.
     
}

下面是一个c#中通用列表的简单实现。

using System;
using System.Collections.Generic; // A system namespace for implementing generic lists.

namespace GenericListExample
{
    public class Generic<T>  // Declare Generics.
    {
        public void GenFunction(T printvalue)
        {
            Console.WriteLine(printvalue);
        }
    }
    public class program
    {
        public static void Main(string [] args)
        {
         // Code goes here 
            Console.WriteLine("Printing integer value");
            Generic<int> gen = new Generic<int>();
            gen.GenFunction(10);
            
            Console.WriteLine("Printing string value");
            Generic<string> genString = new Generic<string>();
            genString.GenFunction("hallo there");
            
        }
    }
}

请注意,Generic<T> 是一个占位符,在编译时被一个特定的数据类型所取代。

2.哈希集

与列表不同,集合不以特定的顺序存储数据或条目。

哈希集是基于离散数学中 "集合 "的概念,它是包含一个、两个或许多不同项目的条目的分组。

我们在处理大量未经排序的数据时使用哈希集。在哈希集中进行排序是不可能的,因为条目没有特定的索引。

这里有一个程序可以帮助你理解哈希集。

using System;
using System.Collections.Generic;
  
public class MyHashSet {

    static public void Main(){
        // code goes here
        // Creating HashSet
     
        HashSet<string> myhash1 = new HashSet<string>();
  
        // Add the elements in HashSet
  
        myhash1.Add("Chemistry");
        myhash1.Add("Maths");

        // Creating another HashSet
     
        HashSet<string> myhash2 = new HashSet<string>();
        myhash2.Add("Section");
        myhash2.Add("Git");
   
        // Using ExceptWith method
        myhash1.ExceptWith(myhash2);
        foreach(var ele in myhash1)
        {
            Console.WriteLine(ele);
        }
    }
}

哈希集不支持以下操作。

  • 在一个特定的位置添加一个对象。
  • 在一个特定的索引处替换一个项目。
  • 检索所有项目是可能的,但顺序是不确定的。

3.字典

字典与列表和集合略有不同,因为它们不是单独的对象,而是成对地存储数据;一个key 和一个value

key 包含了对的地址,而value 则存储了所需的信息。

字典有广泛的应用。例如,一个地址簿可以使用keys 作为人名,而values 作为地址、电话、电子邮件或其他联系细节。

字典中不允许有重复的键。字典的一个巨大优势是,它们存储无序的项目,可以使用其独特的键进行排序。

这里有一个简单的程序来帮助你理解字典。

using System;
using System.Collections.Generic;  
  
public class MyDictinary {
    static public void Main () {
          // code goes here.
      
        //  Creating a dictionary using Dictionary <TKey,TValue> class
        Dictionary<int, string> My_dict1 = new Dictionary<int, string>(); 
            
          // Adding <key/value> pair in the Dictionary using Add() method
          My_dict1.Add(1123, "Welcome");
          My_dict1.Add(1124, "Hello");
          My_dict1.Add(1125, "World");
            
          foreach(KeyValuePair<int, string> ele1 in My_dict1){
              Console.WriteLine("{0} and {1}", ele1.Key, ele1.Value);
          }
          Console.WriteLine();
            
          // Creating another dictionary
 
      Dictionary<string, string> My_dict2 = new Dictionary<string, string>(){
                                  {"a.1", "CSharp"},
                                  {"a.2", "Java"},
                                {"a.3", "Python"} }; 
           
          foreach(KeyValuePair<string, string> ele2 in My_dict2){
              Console.WriteLine("{0} and {1}", ele2.Key, ele2.Value);
          }
    }
}

结论

在本教程中,我们已经学习了C#中的通用集合,以及它们的优点。讨论的一些集合是列表、字典和哈希集。