Skip to content

Commit be29071

Browse files
committed
Add documentation for C# Generics and Linux Software Installation; remove outdated files; improve code-behind
1 parent 9fdb8f4 commit be29071

13 files changed

Lines changed: 246 additions & 0 deletions

docs/CSharpDotNet/Generics/Async-Generics.md

Whitespace-only changes.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
title: Basic Syntax
3+
sidebar_position: 1
4+
---
5+
6+
# Basic Syntax
7+
## Declaration
8+
### Generic Class
9+
```CSharp
10+
public class Box<T>
11+
{
12+
public T Value { get; set; }
13+
}
14+
```
15+
### Generic Method
16+
```CSharp
17+
public T Method<T>(T value)
18+
{
19+
return value;
20+
}
21+
```
22+
### Generic Interface
23+
```CSharp
24+
public interface IRepository<T>
25+
{
26+
T Get(int id);
27+
}
28+
```
29+
### Generic Constraints
30+
```CSharp
31+
public class Factory<T> where T : new()
32+
{
33+
public T Create() => new T();
34+
}
35+
```
36+
37+
:::tip
38+
- T is a type parameter (a placeholder)
39+
- `where T : new()` is a constraint, which restricts what kinds of types can be used
40+
:::
41+
42+
## Constraint
43+
Generic constraints specify the requirements that a type parameter must satisfy. They allow the compiler to ensure that `T` supports certain operations, such as having a parameterless constructor, inheriting from a specific base class, or implementing an interface.
44+
45+
Without constraints, the compiler cannot assume anything about `T`.
46+
47+
## Basic Syntax
48+
49+
```CSharp
50+
class MyClass<T> where T : ConstraintType
51+
{
52+
}
53+
```
54+
55+
Multiple constraints can be combined:
56+
57+
```CSharp
58+
class MyClass<T> where T : BaseClass, IInterface, new()
59+
{
60+
}
61+
```
62+
63+
Constraint order must follow this pattern:
64+
65+
1. class or struct
66+
2. Base class
67+
3. Interfaces
68+
4. new()
69+
70+
## Common Constraint Types
71+
72+
### Reference type constraint
73+
74+
```CSharp
75+
where T : class
76+
```
77+
78+
### Value type constraint
79+
80+
```CSharp
81+
where T : struct
82+
```
83+
84+
### Parameterless constructor
85+
86+
```CSharp
87+
where T : new()
88+
```
89+
90+
### Inherit from a base class
91+
92+
```CSharp
93+
where T : BaseClass
94+
```
95+
96+
### Implement an interface
97+
98+
```CSharp
99+
where T : IDisposable
100+
```
101+
102+
## Why Constraints Are Needed
103+
104+
### Calling members that may not exist
105+
106+
```CSharp
107+
void CloseItem<T>(T item)
108+
{
109+
item.Dispose(); // Cannot compile
110+
}
111+
```
112+
113+
```CSharp
114+
void CloseItem<T>(T item) where T : IDisposable
115+
{
116+
item.Dispose(); // ✔ Safe
117+
}
118+
```
119+
120+
### Creating instances of T
121+
122+
```CSharp
123+
class Factory<T>
124+
{
125+
T Create() => new T(); // Not allowed
126+
}
127+
```
128+
129+
```CSharp
130+
class Factory<T> where T : new()
131+
{
132+
T Create() => new T(); // ✔ Allowed
133+
}
134+
```
135+
136+
## Method-Level Constraints
137+
138+
Constraints can also be applied to individual methods:
139+
140+
```CSharp
141+
public void Save<T>(T item) where T : IEntity
142+
{
143+
item.Store();
144+
}
145+
```
146+
147+
## Summary
148+
149+
- Constraints define what a type parameter must support.
150+
- They ensure type safety for operations involving `T`.
151+
- Common constraints include: class, struct, new(), base classes, and interfaces.

docs/CSharpDotNet/Generics/ConcurrentCollections.md

Whitespace-only changes.

docs/CSharpDotNet/Generics/Delegates.md

Whitespace-only changes.

docs/CSharpDotNet/Generics/Interfaces.md

Whitespace-only changes.

docs/CSharpDotNet/Generics/LINQ-Generics.md

Whitespace-only changes.

docs/CSharpDotNet/Generics/Memory-Span.md

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Miscellaneous
2+
3+
## System
4+
### `Nullable<T>` : (struct) enables value types to be null → `int?`, `bool?`
5+
6+
## System.Threading.Tasks
7+
### `Task<T>` : (class) async operation returning `T`
8+
### `ValueTask<T>` : (struct) lightweight async return → reduces allocation
9+
### `TaskCompletionSource<T>` : (class) manually completes `Task<T>`
10+
11+
## System.Lazy
12+
### `Lazy<T>` : (class) lazy-initialized value container
13+
14+
## System.Tuple
15+
### `Tuple<T1, T2...>` : (class) legacy tuple type
16+
### `ValueTuple<T1, T2...>` : (struct) modern tuple type behind `(a, b, c)`
17+
18+
## System.Memory
19+
### `Memory<T>` : (struct) sliceable, safe memory buffer
20+
### `ReadOnlyMemory<T>` : (struct) read-only memory slice
21+
### `Span<T>` : (ref struct) high-performance stack-only span
22+
### `ReadOnlySpan<T>` : (ref struct) read-only span
23+
24+
## System.Buffers
25+
### `ArrayPool<T>` : (class) shared array pool → high-performance allocations
26+
### `MemoryPool<T>` : (class) pooled `Memory<T>` blocks
27+
### `IMemoryOwner<T>` : (interface) owner of pooled `Memory<T>`
28+
29+
## System.IO / Array utilities
30+
### `ArraySegment<T>` : (struct) slice of an array without copying

docs/CSharpDotNet/Generics/ObservableCollection.md

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: System.Collections.Generic
3+
sidebar_position: 2
4+
---
5+
6+
# `System.Collections.Generic`
7+
Core Generic Collections
8+
9+
## `List<T>`
10+
Resizable dynamic array.
11+
Fast random access (O(1)), append is amortized O(1).
12+
13+
## `Dictionary<TKey, TValue>`
14+
Hash map (hash table).
15+
Fast lookup/insert/delete average O(1).
16+
17+
## `HashSet<T>`
18+
Unique-value collection (mathematical set).
19+
Insert & lookup average O(1).
20+
21+
## `Queue<T>`
22+
FIFO (First-In-First-Out) collection.
23+
24+
## `Stack<T>`
25+
LIFO (Last-In-First-Out) collection.
26+
27+
## `LinkedList<T>`
28+
Doubly linked list.
29+
Efficient insertion/removal in the middle of the list.
30+
31+
## `SortedList<TKey, TValue>`
32+
Sorted Key–Value pairs backed by *array*.
33+
Good for smaller datasets.
34+
35+
## `SortedDictionary<TKey, TValue>`
36+
Sorted Key–Value pairs backed by *red-black tree*.
37+
Good for larger datasets.
38+
39+
## `SortedSet<T>`
40+
Sorted unique elements.
41+
Uses balanced tree (RB-tree).
42+
43+
## `KeyValuePair<TKey, TValue>`
44+
Struct used when iterating `Dictionary<K, V>`.

0 commit comments

Comments
 (0)