Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Get And Set An Item In A C# List
WhatsApp
Mahesh Chand
6y
348.7k
0
7
100
Article
C# List<T> class provides methods and properties to create a list of objects (classes).
List is a generic class. You must import the following namespace before using the List<T> class.
using
System.Collections.Generic;
List.Item
The Item property gets and sets the value associated with the specified index.
The following code snippet gets and sets the first item in a list.
using
System;
using
System.Collections.Generic;
namespace
ConsoleApp1
{
class
Program
{
static
void
Main(
string
[] args)
{
// Create a list of strings
List<
string
> AuthorList =
new
List<
string
>();
AuthorList.Add(
"Mahesh Chand"
);
AuthorList.Add(
"Praveen Kumar"
);
AuthorList.Add(
"Raj Kumar"
);
AuthorList.Add(
"Nipun Tomar"
);
AuthorList.Add(
"Dinesh Beniwal"
);
// Get first item of a List
string
auth = AuthorList[0];
Console.WriteLine(auth);
Console.WriteLine(
"-------------"
);
// Set first item of a List
AuthorList[0] =
"New Author"
;
foreach
(var author
in
AuthorList)
{
Console.WriteLine(author);
}
}
}
}
The output from above code listing is shown in below figure.
Next >>
C# List Tutorial
C# List item
find item
get list item
get List Item in C#
List.Item
List.Item property
set List item
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.4k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found