C# is a [[DotNet|.NET]] programming language by [[Microsoft]] running on the [[Common Language Runtime]] (CLR).

# Notability
It is one of the more common languages used on Windows, and with the introduction of dotNET core, it is now portable(-ish) to [[Linux]] and other operating systems.
Mostly interesting to me as it is usable as a scripting language for many game engines like [[Godot]], [[Unity Engine]], and [[CryEngine]].
## History
C# was mostly just Microsoft's take on [[Java]]. In some ways it has exceeded Java, but being tightly connected to the Microsoft platform for so long has limited its reach.
A lot of simple Java code will run with little to no changes in C#.
See also: [[DotNet]]
# Tips
## Implementing Optional Types
[[aarthificial]] has a [video](https://www.youtube.com/watch?v=uZmWgQ7cLNI) demonstrating creating optional types in [[Unity Engine]] that consists mostly of just a parametric struct with a constructor.
```Csharp
public struct Optional<T> {
private bool enabled;
private T value;
public Optional(T initialValue) {
enabled = true;
value = initialValue;
}
public bool Enabled => enabled;
public T Value => value;
}
```
Like Java, C# has the concept of value types vs boxed types. Value types are non-nullable. An Optional wrapper as above allows one to mark a value type as invalid, and also wrap nullable types so that a comparison to null isn't needed - which is apparently problematic in Unity (and possibly C# in general).
There is some [discussion](https://stackoverflow.com/questions/16199227/optional-return-in-c-net) on [[Stack Overflow]] about optional types with many people not understanding the value/boxed problem. However, there are still some good ideas there which expands upon the concept by implementing an additional interface so that the Optional type can be used by C#'s LINQ syntax.
```Csharp
public class Option<T> : IEnumerable<T> {
private readonly T[] data;
private Option(T[] data) {
this.data = data;
}
public static Option<T> Create(T value) {
return new Option<T>(new T[] { value });
}
public static Option<T> CreateEmpty() {
return new Option<T>(new T[0]);
}
public IEnumerator<T> GetEnumerator() {
return ((IEnumerable<T>)this.data).GetEnumerator();
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator() {
return this.data.GetEnumerator();
}
}
```
There is also an actively developed set of [[Functional - C Sharp Functional Language Extensions]] to C# which include various [monadic types](https://github.com/louthy/language-ext#optional-and-alternative-value-monads) include multiple Option types.
## Nullability
Some types can be `null` in C# and others cannot.
Classes can be `null` while structs cannot. This is... "useful" in that it can be abused for a variety of [interesting effects](https://github.com/louthy/language-ext#ad-hoc-polymorphism), but clearly isn't cohesive.