简体   繁体   中英

What data structure should I use?

Each of my object has these values: 2 short strings, 2 doubles and 5 TextBlocks (for a Windows app). I have about 30 of these objects.

I will need to iterate through these objects regularly. I have some ideas but want your independent suggestions for ease of iteration and selection of values from these objects.

One consideration is that the size of the collection is static, it doesn't need to grow.

The properties in your object should not affect the type of collection you choose. The key is the fact that the number of items in your collection remains static, so a list is overkill. I would use a typed array like MyClass[]. You can still use LINQ to query the array and filter by any public property.

Here's a link to information about LINQ - LINQ . LINQ is what you're looking for. Consider LINQ statements like the following example. Here is the syntax of LINQ:

var scores = new List<int>();
var Example =
        from score in scores
        where score > 80
        select score;

foreach (var example in Example) { doSomething(); }

The actual sorting only goes on when called at a later time. This is called deferred execution, one property of the example above. IE, Example's statement is only actually executed during:

 foreach (var example in Example) { doSomething(); }

There is also this to consider with LINQ:

Query syntax vs Method Syntax

I hope you have found this helpful.

Using Array of Objects........

enter code here
MyClass
{
   public string str;

   public SetStr(string st)
   {
     str=st;
   }

   public GetStr()
   {
      return str;
   }

}

int main() 
{ 
     MyClass obj[30]; 
     int i; 
     for(i=0; i < 30; i++) 
       obj[i].str; //Access as required
     return 0; 
}

Also for get and Set properies can be used

List with 3 different classes or strings. Generic class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM