为类创建索引

本文关键字:索引 创建 | 更新日期: 2023-09-27 18:21:12

我有一个班叫本科生,它记录学生的名字和姓氏。我希望每个对象都有一个索引。在我的程序课上,我创建了一个本科生List<>。我该如何设置它,使列表中的每个对象都有一个唯一的标识号,它在程序类中会是什么样子?

public class Undergrad
{
     String fName, lName;
     public Undergrad()
     {
     }
     public Undergrad(string firstName, string lastName)
     {
         this.fName = firstName;
         this.lName = lastName;           
     }
}

为类创建索引

您需要向类添加一个属性来为初学者存储该索引。这可以是整数,也可以是UniqueIdentifer。

如果使用整数,则需要其他地方(如数据库)来存储所有索引,以便应用程序知道在哪里可以获得下一个值。

使用UniqueIdentifer(System.Guid),您不会得到重复的冲突,所以您可以直接创建内联。

选项1

public class Undergrad
{
     String fName, lName;
     public Guid UniqueId {get; set; }
     public Undergrad()
     {
          UniqueId = System.Guid.NewGuid();
     }
     public Undergrad(string firstName, string lastName)
     {   
         UniqueId = System.Guid.NewGuid();
         this.fName = firstName;
         this.lName = lastName;           
     }
}

选项2

public class Undergrad
{
     String fName, lName;
     public int UniqueId {get; set; }
     public Undergrad()
     {
          UniqueId = //LoadFromDatabase();
     }
     public Undergrad(string firstName, string lastName)
     {   
         UniqueId = //LoadFromDatabase();
         this.fName = firstName;
         this.lName = lastName;           
     }
}

你目前将本科生信息存储在哪里?如果它已经在数据库中,我希望您已经在对象上有了一个id字段。

最后,当您将这些放入List<T>时,列表将有自己的索引(用于列表中的位置),这是一个单独的概念。

您可以向类添加static唯一ID。

public class Undergrad
{
     String fName, lName;
     private static int _uniqueID;   
     private int _myUniqueID;
     public Undergrad() : this(string.Empty, string.Empty)
     {
     }
     public Undergrad(string firstName, string lastName) 
     {
         this.fName = firstName;
         this.lName = lastName;          
         _uniqueID += 1; 
         _myUniqueID = _uniqueID;
     }
    public int UniqueID { get { return _myUniqueID; } }
}

不确定这是否是您想要的,但:

您可以创建一个包含Undergrad列表的对象,例如添加一个枚举类EUndergrad,并添加将返回您要查找的实例的索引方法。

public class Undergrads
{
    private List<Undergrad> myList;
    public Undergrad this[EUndergrad index]
    {
        return this.myList[index];
    }
}
public enum EUndergrad
{
    Undergrad_0 = 0,
    Undergrad_1 = 1,
    ...
}

你可以通过调用来检索你的本科生对象

instanceOfUndergrads[EUndergrad.Undergrad_0]

维护列表的查找,并为该查找分配一个唯一的id/guid作为关键字

    public class Undergrad
    {
        String fName, lName;
        public Undergrad()
        {
        }
        public Undergrad(string firstName, string lastName)
        {
            this.fName = firstName;
            this.lName = lastName;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Undergrad> lstGraduates = new List<Undergrad>() { new Undergrad("f1", "l1"), new Undergrad("f2", "l2"), new Undergrad("f3", "l3") };
            int i = 0;
            ILookup<int, Undergrad> lookup = lstGraduates.ToLookup(p => i++);
            foreach (IGrouping<int, Undergrad> packageGroup in lookup)
            {
                Console.WriteLine(packageGroup.Key);
                Undergrad obj = (Undergrad)packageGroup;                
            }
            Console.Read();
        }
    }

感谢在.Net 中生成唯一密钥

可以包括提供唯一关键帧的KeyGenerator。

public class Undergrad
{
    public string Id { get; private set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Undergrad()
    {
        Id = KeyGenerator.GetUniqueKey();
    }
    public Undergrad(string firstName, string lastName)
        : this()
    {
        FirstName = firstName;
        LastName = lastName;
    }
}
public static class KeyGenerator
{
    public static string GetUniqueKey()
    {
        int maxSize = 8;
        int minSize = 5;
        var chars = new char[62];
        string a;
        a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        chars = a.ToCharArray();
        int size = maxSize;
        var data = new byte[1];
        var crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);
        size = maxSize;
        data = new byte[size];
        crypto.GetNonZeroBytes(data);
        var result = new StringBuilder(size);
        foreach (byte b in data)
        {
            result.Append(chars[b%(chars.Length - 1)]);
        }
        return result.ToString();
    }

测试:

internal class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine((new Undergrad()).Id);
        Console.WriteLine((new Undergrad()).Id);
        Console.WriteLine((new Undergrad()).Id);
    }
}

产品:

    QdfQcGuV
    5MtEBtev
    AXwseCmJ