Collections in C # over VB.NET

Hi, I am new to C # and have studied it. Actually I think that I am using it, there are some things that I'm not sure about, but I will try to research them before asking. However, I can not find the collection. I came from the VB.NET database, and earlier I used collections of another class to store my properties instead of wading through datatable. My previous code was something like the following

Public class UserDetails Public Property Username() As String Get Return sUsername End Get Set(ByVal value As String) sUsername = value End Set End Property Public Property Forename() As String Get Return sForename End Get Set(ByVal value As String) sForename = value End Set End Property Public Property Surname() As String Get Return sSurname End Get Set(ByVal value As String) sSurname = value End Set End Property End Class 

you get an idea, a class with properties for some reason (this is easy in C # and somthing, which I managed to do).

 public string Username { get { return sUsername; } set { sUsername = value; } } public string Forename { get { return sForename; } set { sForename = value; } } public string Surname { get { return sSurname; } set { sSurname = value; } } 

Then I have another class and create an object for this class and save each object in the collection as shown below:

 clUserDetails = New Collection Dim objUserDetails as new UserDetails objUserDetails.Username = "SomeonesUsername" objUserDetails.Forename = "SomeonesForename" objUserDetails.Surname = "SomeonesSurname" clUserDetails.Add(objUserDetails) 

and the class also has a function that returns a collection

 Public Function Items() As Collection Return clUserDetails End Function 

Now I get this by scrolling through each object and assigning the object to get properties or doing the following

 objUsers.Items(1) 

Now in C #, I tried using List <>, since I cannot find the collection object. I assume this is a VB thing, so I need help on how to do this in C #.

I tried the following

 private List <UserDetails> clUserDetails; UserDetails objUserDetails = new UserDetails(); objUserDetails.Username = "Username"; objUserDetails.Forename = "Forename"; objUserDetails.Surname = "Surname"; clUserDetails.Add(objUserDetails); 

but I was fixated on how to get the only object of this list / collection.

Can someone help

+5
source share
3 answers

Is that what you want to do?

Your model:

 public class UserDetails { public string Username { get; set; } public string Forename { get; set; } public string Surname { get; set; } } 

And create / add user to list <>

 var userDetails = new UserDetails { Username = "Username", Forename = "Forename", Surname = "Surname" }; // object initialiser var listOfUsers = new List<UserDetails>(); listOfUsers.Add(userDetails); 

And then fetch using the index (assuming it exists), for example:

 listOfUsers[0].Forename 
+5
source

Well, you are there. List in System.Collections.Generic is what you need.

I simplified your class a bit:

 class UserDetails { public string Username { get; set; } public string Forename { get; set; } public string Surname { get; set; } } 

Declare a new UserDetails list:

 List<UserDetails> clUserDetails = new List<UserDetails>(); 

You can add additional UserDetails to the list:

 clUserDetails.Add(new UserDetails { Username = "UserName1", Forename = "Forename1", Surname = "Surname1" }); clUserDetails.Add(new UserDetails { Username = "UserName2", Forename = "Forename2", Surname = "Surname2" }); clUserDetails.Add(new UserDetails { Username = "UserName3", Forename = "Forename3", Surname = "Surname3" }); 

And then you can execute the loop with foreach

 foreach(UserDetails userDet in clUserDetails) { Console.WriteLine($"Current user details --> Username : {userDet.Username} Forename : {userDet.Forename} Surname : {userDet.Surname} "); } 

The output will be:

Current username → Username: UserName1 Forename: Forename1 Last name: Surname1 Current user information → Username: UserName2 File name: Forename2 Last name: Surname2 Current username → Username: UserName3 Forename: Forename3 Last name: Last name3

+3
source

Working with collections, as well as with arrays in C #, is similar to VB.NET, except that in C # we use the characters [and] instead of (and) as an indexer.

An example of using a list (or any other collection):

 // Create a list and add some items to it. var myList = new List<string>(); myList.Add("Item A"); myList.Add("Item B") // Now, use the list. string itemA = myList[0]; string itemB = myList[1]; 

Dictionary usage example:

 // Create a dictionary and add some items to it. var myDic = new Dictionary<string, string>(); myDic.Add("google.com", "Search engine"); myDic.Add("stackoverflow.com", "An awesome site"); // Now, use the dictionary (get its keys value). string google = myDic["google.com"]; string stackOverflow = myDic["stackoverflow.com"]; 

An example of using arrays:

 // Declare an array and add some items to it. string[] myFirstArray = { "One", "Two", "Three" } // Declare a second array similar to the first one. string[3] mySecondArray; mySecondArray[0] = "One"; mySecondArray[1] = "Two"; mySecondArray[2] = "Three"; // Now, use values of the arrays. string one = myFirstArray[0]; string two = mySecondArray[1]; string three = myFirstArray[2]; 
+2
source

All Articles