Skip to content

Commit

Permalink
Add .NET 8 notes (#39)
Browse files Browse the repository at this point in the history
* feat(dotnet): dotnet 8 any type alias

* feat(dotnet): lamda default parameters

* feat(dotnet): rework class declaration notes

* fix(dotnet): remove duplicate notes

* feat(csharp): add improved collection initializers notes
  • Loading branch information
m-lamonaca authored Oct 24, 2023
1 parent 4d3f97c commit 957f45a
Showing 1 changed file with 89 additions and 42 deletions.
131 changes: 89 additions & 42 deletions docs/languages/dotnet/csharp/csharp.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,26 @@ to native code later.
| Event, Enum, Enum Value, | PascalCase |
| Variables, Parameters | camelCase |

### Namespaces & Imports
### Namespaces, Imports & Aliases

Hierarchic organization of programs an libraries.

```cs linenums="1"
using System; // import the System Namespace
using Alias = Namespace; // set an alias for a specific namespace
using static System.Console; // statically import a class to use its static methods w/o qualification
// type aliases
using Alias = Namespace.SubNamespace.Type;
using LookupTable = Dictionary<string, string>;
using Points = (int, int, int)[];

// global using [C# 10], should be in dedicated file
global using <namespace>;

namespace Namespace; // [C# 10]
//or
namespace Namespace // namespace declaration
namespace Namespace
{
// class here
}
```

Expand Down Expand Up @@ -788,7 +791,7 @@ Both of these are designed primarily for code generation scenarios, although the
| --------- | ---------------------- |
| x `+=` y | x = x + y |
| x `-=` y | x = x - y |
| x `*=` y | x = x \* y |
| x `*=` y | x = x * y |
| x `/=` y | x = x / y |
| x `%=` y | x = x % y |
| x `<<=` y | x = x << y |
Expand Down Expand Up @@ -1419,63 +1422,66 @@ If any part is declared abstract, then the whole type is considered abstract.
If any part is declared sealed, then the whole type is considered sealed.
If any part declares a base type, then the whole type inherits that class.

### Class Declaration

A _field_ is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
A _property_ is a member that provides a flexible mechanism to read, write, or compute the value of a private field.

The `static` keyword declares that a member is not associated with any particular instance of the class.
### Methods

```cs linenums="1"
class Class
{
// if static contractor exists the initializer is runned immediately before the static constructor,
// otherwise is will run no later than the first access to one on the type's fields or methods
public static type Field = value; // static (non-instance) public field w/ initializer
static Type Method(Type Argument, ...) {} // static method, can operate ONLY on STATIC VARIABLES
// runs before instance's constructor
private type _field = value; // private instance field w/ initializer
Type Method(Type Argument, ...) {} // instance method, can operate on INSTANCE VARIABLES
private type _field; // private instance field, initialized by constructor
// override inherited method
public override Type Method()
{
}

// static constructor, not called explicitly, has no arguments
// triggered by one of two events, whichever occurs first: creating an instance, or accessing any static member of the class.
// since it's static and takes no arguments there can be at most one for each class
static Class() {
// place to init static fields
// visrual methods CAN be overridden
public virtual Type Method()
{
}
}```

public Class() { /* ... */} // parameterless constructor
### Constructors

// class constructor
public Class(type parameter) {
_field = parameter;
}
```cs
class Class(Type Parameter, ...) // primary constructor (like records)
{

// extends an existing constructor adding parameters to the existing ones
public Class(type anotherParameter, type parameter) : this(parameter)
public Class() { } // parameterless constructor
// standard constructor
public Class(type parameter)
{
this._anotherField = anotherParameter;
_field = field;
}

// called by Console.WriteLine(obj)
public override string ToString(){
return <string>
// extends an existing constructor adding parameters to the existing ones
public Class(type parameter, type other) : this(parameter)
{
_other = other;
}

static type Method(arguments) {} // static method, can operate on STATIC VARIABLES
type Method(arguments) {} // instance method, can ONLY operate on INSTANCE VARIABLES
}
```

### Properties
> **Note**: if a _primary constructor_ is used all other constructors must use `this(...)` to invoke it
### Properties & Fields

[Properties Docs](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties)

A _field_ is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.
A _property_ is a member that provides a flexible mechanism to read, write, or compute the value of a private field.

```cs linenums="1"
class Class
{

public static Type Field = value; // static (non-instance) public field w/ initializer
private Type _field = value; // private instance field w/ initializer
private Type _field; // private instance field, initialized by constructor
private type _backingField;

// PROPERTY
Expand Down Expand Up @@ -1587,18 +1593,59 @@ var identity = new Matrix
};

List<int> digits = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
List<int> digits = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

int[] digits = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] digits = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

Dictionary<int, string> digits = new Dictionary<int, string> {
{ 0, "zero" },
{ 1, "one" },
{ 2, "two" },
{ 3, "three" },
{ 4, "four" },
{ 5, "five" },
{ 6, "six" },
{ 7, "seven" },
{ 8, "eight" },
{ 9, "nine" }
};

Dictionary<int, string> digits = [
0: "zero",
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine"
];
```

### `Static` Class

The `static` keyword declares that a member is not associated with any particular instance of the class.
Static classes **can't** instantiate objects and all their methods **must** be static.

```cs linenums="1"
static class Class
{
static type variable = value;
// static constructor, not called explicitly, has no arguments
// triggered by one of two events, whichever occurs first: creating an instance, or accessing any static member of the class.
// since it's static and takes no arguments there can be at most one for each class
static Class()
{
}

static Type _field = value;

static Type Property { get; set; } = value;

static type method (type parameter, ...)
static Type Method (Type parameter, ...)
{
}
}
Expand Down Expand Up @@ -2212,13 +2259,14 @@ Delegate<Type> lambda = (Type input) => <expr>;
Delegate<Type> lambda = input => { return <expr>; };
Delegate<Type> lambda = (input) => { return <expr>; };
Delegate<Type> lambda = (Type input) => { return <expr>; };
Delegate<Type> lambda = (Type input = default) => { return <expr>; }; // lambda default parameter
// static modifier prevents unintentional capture of local variables or instance state by the lambda
Delegate<Type> lambda = (static Type input) => <expr>;

Type variable = delegate { <expression>; }; // ignore arguments of the method passed to the delegate
// lambda type inference [C# 10]
// lambda type inference
var f = Console.WriteLine;
var f = x => x; // inferring the return type
var f = (string x) => x; // inferring the signature
Expand Down Expand Up @@ -2972,7 +3020,6 @@ unsafe
[ptr_math]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code#pointer-arithmetic
[ptr_comparison]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code#pointer-comparison
[stack_alloc]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code#stack-allocation
[fixed_buffers]: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/unsafe-code#fixed-size-buffers

### Native Memory

Expand Down

0 comments on commit 957f45a

Please sign in to comment.