为类创建索引

时间:2013-07-05 07:47:16

标签: c# .net

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

public class Undergrad
{
     String fName, lName;

     public Undergrad()
     {
     }

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

5 个答案:

答案 0 :(得分:4)

您需要向类中添加一个属性,以便为初学者存储该索引。这可以是整数或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>时,列表将拥有它自己的索引(对于列表中的位置),这是一个单独的概念。

答案 1 :(得分:1)

您可以在课程中添加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; } }
}

答案 2 :(得分:1)

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

您可以创建一个包含Undergrad列表的对象,例如添加一个enum类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,
    ...
}

您可以通过调用

来检索您的Undergrad对象
instanceOfUndergrads[EUndergrad.Undergrad_0]

答案 3 :(得分:1)

维护查找列表并指定唯一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();
        }
    }

答案 4 :(得分:1)

礼貌Generating Unique Keys in .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
相关问题