C#
Datastructures in C#
C# supports a variety of data structures to help you manage and organize your data efficiently. Here are some of the most commonly used data structures in C#:
Arrays:
Fixed-size collection of elements of the same type.
csharp
int[] numbers = new int[5];
Lists:
Dynamic array that grows as needed. Part of the System.Collections.Generic namespace.
csharp
List<int> numbers = new List<int>();
Dictionaries:
Collection of key-value pairs. Also part of System.Collections.Generic.
csharp
Dictionary<string, int> ageDictionary = new Dictionary<string, int>();
Queues:
First-in, first-out (FIFO) collection.
csharp
Queue<string> queue = new Queue<string>();
Stacks:
Last-in, first-out (LIFO) collection.
csharp
Stack<string> stack = new Stack<string>();
HashSets:
Collection of unique elements. Part of System.Collections.Generic.
csharp
HashSet<int> hashSet = new HashSet<int>();
LinkedLists:
Doubly-linked list providing fast insertions and deletions.
csharp
LinkedList<int> linkedList = new LinkedList<int>();
SortedLists:
Collection of key-value pairs sorted by key.
csharp
SortedList<string, int> sortedList = new SortedList<string, int>();
SortedDictionaries:
Similar to SortedList, but more efficient for large datasets.
csharp
SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>();
Graphs:
Custom data structures for representing nodes and edges, often implemented using lists or dictionaries.
A tuple in C# is a data structure that allows you to store a fixed number of items, each of which can be of different types. It's a great way to group related data without having to create a separate class.
Here's a basic example:
csharp
var tuple = (1, "Hello", 3.5);
Console.WriteLine(tuple.Item1); // Outputs: 1
Console.WriteLine(tuple.Item2); // Outputs: Hello
Console.WriteLine(tuple.Item3); // Outputs: 3.5
In this example, tuple is a tuple containing an integer, a string, and a double. The items in the tuple can be accessed using Item1, Item2, Item3, and so on.
Named Tuples
You can also use named tuples to make your code more readable:
csharp
var person = (Name: "John", Age: 30);
Console.WriteLine(person.Name); // Outputs: John
Console.WriteLine(person.Age); // Outputs: 30
Named tuples allow you to access items by their names rather than Item1, Item2, etc.
Advantages of Tuples
Convenient: Quickly group related data without creating a separate class.
Readability: Named tuples enhance code readability.
Immutability: Tuples are immutable, meaning their values cannot be changed once created.
Tuples are incredibly useful for returning multiple values from a method,
In C#, a record is a special type of class introduced in C# 9.0 that is designed to model immutable data. Unlike regular classes, records are intended to be more concise and automatically provide value-based equality, meaning that two record instances with the same data are considered equal.
Here are some key features of record:
Key Features:
Concise Syntax: Records use a more concise syntax to define data models.
Immutability: Properties in records are immutable by default.
Value-Based Equality: Records automatically provide value-based equality and comparison, unlike classes that use reference-based equality.
Deconstruction: Records support deconstruction to easily extract property values.
Example:
Here's a basic example of a record in C#:
csharp
public record Person(string FirstName, string LastName);
This defines a Person record with two properties, FirstName and LastName. You can create and use records like this:
csharp
var person1 = new Person("John", "Doe");
var person2 = new Person("John", "Doe");
Console.WriteLine(person1 == person2); // Outputs: True (value-based equality)
Console.WriteLine(person1.FirstName); // Outputs: John
// Deconstruction
var (firstName, lastName) = person1;
Console.WriteLine(firstName); // Outputs: John
Console.WriteLine(lastName); // Outputs: Doe
Access modifiers in C#
Access modifiers in C# control the visibility and accessibility of classes and members, such as methods, properties, and fields. Here’s a breakdown:
public: The member is accessible from any other code.
csharp
public class Example
{
public int PublicProperty { get; set; }
}
private: The member is accessible only within the same class.
csharp
public class Example
{
private int PrivateProperty { get; set; }
}
protected: The member is accessible within its class and by derived class instances.
csharp
public class BaseExample
{
protected int ProtectedProperty { get; set; }
}
public class DerivedExample : BaseExample
{
public void ExampleMethod()
{
Console.WriteLine(ProtectedProperty); // Accessible
}
}
internal: The member is accessible only within its own assembly but not from another assembly.
csharp
internal class Example
{
internal int InternalProperty { get; set; }
}
protected internal: The member is accessible within its assembly and also in derived classes located in other assemblies.
csharp
public class Example
{
protected internal int ProtectedInternalProperty { get; set; }
}
private protected: The member is accessible within its declaring class and derived classes within the same assembly.
csharp
public class BaseExample
{
private protected int PrivateProtectedProperty { get; set; }
}
public class DerivedExample : BaseExample
{
public void ExampleMethod()
{
Console.WriteLine(PrivateProtectedProperty); // Accessible
}