Collections are data structures that hold objects in memory. In C#, collections are incredibly versatile and can be used to store, manipulate, and manage data efficiently. Unlike arrays, which have a fixed size, collections can dynamically resize, offering more flexibility. This chapter introduces some of the most commonly used collection types in the .NET Framework, including their use cases, benefits, and key operations.
Collections are data structures that hold objects in memory. In C#, collections are incredibly versatile and can be used to store, manipulate, and manage data efficiently. Unlike arrays, which have a fixed size, collections can dynamically resize, offering more flexibility. This chapter introduces some of the most commonly used collection types in the .NET Framework, including their use cases, benefits, and key operations.
Arrays vs. Collections
While arrays are useful for storing fixed-size, homogeneous data sets, collections offer more functionality, such as dynamic resizing and the ability to store heterogeneous objects. Collections also provide various methods for item manipulation, making them more flexible and powerful for data management.
System.Collections.Generic
The System.Collections.Generic namespace includes several collection classes that are type-safe and can store any data types. Using generic collections helps to avoid boxing and unboxing and improves performance.
List<T>
The List<T> class represents a strongly typed list of objects that can be accessed by index. It provides methods to search, sort, and manipulate lists.
List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");
Dictionary<TKey, TValue>
A Dictionary<TKey, TValue> is a collection of key/value pairs that are organized based on the key. It is useful for fast lookups by key.
A Queue<T> represents a first-in, first-out (FIFO) collection of objects. It is ideal for storing objects in the order they were added, simulating a queue.
Queue<string> tasks = new Queue<string>();
tasks.Enqueue("Task1");
tasks.Enqueue("Task2");
Stack<T>
A Stack<T> represents a last-in, first-out (LIFO) collection of objects. It is used to store objects in reverse order from which they were added, simulating a stack.
Stack<string> books = new Stack<string>();
books.Push("Book1");
books.Push("Book2");
System.Collections Namespace
This namespace contains non-generic collection types. However, it's generally recommended to use the generic collections for type safety and better performance.
ArrayList
An ArrayList is similar to a List<T>, but it can hold items of any data type. Due to its non-generic nature, it's less efficient than List<T>.
A Hashtable stores key/value pairs, similar to Dictionary<TKey, TValue>, but without type safety.
Hashtable myHashtable = new Hashtable();
myHashtable.Add("ID1", "Alice");
myHashtable.Add("ID2", "Bob");
LINQ and Collections
Language Integrated Query (LINQ) can be used with collections to perform powerful data queries and manipulations more succinctly and readably.
var filteredNames = names.Where(name => name.StartsWith("A"));
Summary
Collections in C# provide a flexible way to work with groups of objects. The .NET Framework offers a variety of collection classes for different purposes, including generic collections for type safety and performance. Understanding how to use these collections effectively is essential for managing data in your C# applications, enabling efficient storage, retrieval, and manipulation of objects.
In C#, delegates and events are foundational concepts that enable a flexible and extensible way to handle method callbacks and notifications. They play a crucial role in designing and implementing event-driven programming patterns, which are central to developing interactive applications such as graphical user interfaces, game development, and service-oriented applications. This chapter introduces delegates and events, explaining their uses, syntax, and how they enable managed event handling in C#.
Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It also restricts direct access to some of the object's components, which is a way of preventing accidental manipulation of data and ensuring internal data integrity. This principle of hiding the internal state and requiring all interaction to occur through an object's methods is central to C#. This chapter explains the concept of encapsulation and demonstrates how to implement it in C# through access modifiers and properties.
Functions are a cornerstone of programming, allowing developers to encapsulate code that performs a specific task into a reusable and maintainable block. In C#, functions are defined within classes or structs, and they are referred to as methods. This chapter will dive into the basics of defining and using functions in C#, covering their syntax, types, parameters, and return values. Understanding functions is crucial for both beginners and intermediate developers, as they provide the building blocks for structuring and organizing code in any application.