Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,

How to store all the fields in the List<> form? any help would be appreciated. thanks.
And what/how does a list help to do?

What I have tried:

i tried googling but i found it is same like creating an object
Posted
Updated 25-May-16 19:35pm
Comments
Dave Kreskowiak 25-May-16 22:58pm    
What are you talking about? Fields? Fields for what? And what's a "List<> form"?
Sergey Alexandrovich Kryukov 26-May-16 0:07am    
...also, this is a logically wrong question about "a list help to do". Help to do what? Without answering this question, you seem to ask "how a list can help to create a list". A list itself, without the purpose understood separately, cannot be helpful. The is no a list for the sake of list. You really need to understand what you want.

—SA

1 solution

A List is just the same as creating any other object, the difference is that it's a Generic type, which means that you specify what kind of objects the List holds between the '<' and '>' characters. So the general syntax for creating a list is:
C#
List<TypeOfObjectsItHolds> nameOfTheListVariable = new List<TypeOfObjectsItHolds>();
For example, if you want a List of integers:
C#
List<int> list = new List<int>();
for (int i = 0; i < 10; i++)
   {
   list.Add(i * 2);
   }
Or a list of strings:
C#
List<string> list = new List<string>();
for (int i = 0; i < 10; i++)
   {
   list.Add(string.Format("The value is {0}", i * 2));
   }
Or your own class:
C#
List<MyClass> list = new List<MyClass>();
for (int i = 0; i < 10; i++)
   {
   list.Add(new MyClass(i));
   }

After that, you can think of a list as a "stretchy array" - you can do anything with it that you can do with an array but you can also add and remove items at runtime:
C#
if (list.Count > 5)
   {
   Console.WriteLine(list[4]);
   list.RemoveAt(3);
   Console.WriteLine(list[4]);
   }
They are really handy when you don't know in advance how many items you want to hold - for example when you want to get a load of numbers from the user and then sort them into descending order. Rather than asking the user how many items he is going to enter, you just add them to a list until he says "that's your lot!".

There are some caveats regarding performance however, which I'll give you a link to - but "remember" it for later when you are more familiar with C# and collections generally: List<T> - Is it really as efficient as you probably think?[^] rather than read it now!
 
Share this answer
 
Comments
Member 8010354 26-May-16 6:01am    
Hi,
Thanks for your explanation and it really helped me a lot. With that i am having an another doubt. That is, I have created a List

List<string, int=""> ArtifactIDList = new List<string,int>();

And i have created another object for field collection: FieldCollection fc=(FieldCollection)this.ActveArtifact.Fields;

Now how i have to compare the current field to list? Sorry if my question is incomplete.
OriginalGriff 26-May-16 6:10am    
Sorry - I can't read that, the Generic parts have been swallowed by the HTML as they use the same characters "less than" and "greater than" and a quick look at the "raw" code doesn't look like it would compile!
You'll have to open a new question and use code blocks to preserve the generics and formatting when you paste it in.
Member 8010354 26-May-16 7:26am    
Actually i have to comapre the GUID inside the list is equal to field collection
if it is matching, then getting the value
OriginalGriff 26-May-16 8:32am    
Yes, but I can't read your code! :laugh:
Look at the code remaining in your comment above, and you'll see what I see - which doesn't work. Open a new question, and paste your code in: then we can see the actually compiling stuff and have a chance of answering it!
Member 8010354 26-May-16 6:02am    
continuation.........
foreach(var item in fc)
{
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900