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#:

csharp
int[] numbers = new int[5];


csharp
List<int> numbers = new List<int>();


csharp
Dictionary<string, int> ageDictionary = new Dictionary<string, int>();


csharp
Queue<string> queue = new Queue<string>();


csharp
Stack<string> stack = new Stack<string>();


csharp
HashSet<int> hashSet = new HashSet<int>();


csharp
LinkedList<int> linkedList = new LinkedList<int>();


csharp
SortedList<string, int> sortedList = new SortedList<string, int>();


csharp
SortedDictionary<string, int> sortedDict = new SortedDictionary<string, int>();



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

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:

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

    }