Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How To Get And Set Size Of A C# List
WhatsApp
Mahesh Chand
6y
188.8k
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.Capacity
The Capacity property gets and sets the number of items a list can hold without resizing. Capacity is always greater than or equal to the Count value.
The following code snippet creates a List of authors and displays the original capacity. After that, code removes the excess capacity by calling TrimExcess method. After that, capacity is set to 20.
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"
);
// Original Capacity
Console.WriteLine($
"Original Capacity: {AuthorList.Capacity}"
);
// Trim excess
AuthorList.TrimExcess();
Console.WriteLine($
"Trimmed Capacity: {AuthorList.Capacity}"
);
// Update Capacity
AuthorList.Capacity = 20;
Console.WriteLine(AuthorList.Capacity);
Console.WriteLine($
"Updated Capacity: {AuthorList.Capacity}"
);
}
}
}
The output from above code listing is shown in below figure.
Next >>
C# List Tutorial
C# List
C# List size
get size of a List
List
List capacity C#
List class
List.Capacity
set size of a List
size of List with C#
T class
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k 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