C# General Notes - Habilya/LearningCourseNotes GitHub Wiki
Types
Value types (int, bool, string) Reference types (objects mostely)
value types can be passed as reference using keyword 'ref'
structs are considered value types (a copy is passed all the time) useful in games geometry shapes
For Classes : public parameterless constructor will go away once one parametered constructor is defined. Not for structs
value types stored in the stack objects (reference types) stored in the heap (garbage collected)
Record type
c# 9 Record type - looks like a class (it is a reference type) good for DTO - data transfer objects
// short hand syntax
public record MyRecord(
int NumericValue,
string StringValue);
//automatically gives a record with 2 publically accessible properties
// properties are immutables INIT only not settable outside of creation(constructor)
//record equality can be compared using == or .Equal out of the box
// construct record from another record with minimal changes
MyRecord recordC = recordA with { NumericValue = 456 };
// deconstruction of records (look at shorthand syntax constructor up top)
var (numericValue, stringValue) = recordA;
// they will already be of types as in constructor
// You can also mix in and add mutable properties
public record MyRecord(
int NumericValue,
string StringValue)
{
public string ExtraProperty { get; set; }
}
Generics
// You can specify some conditions to methods using generics usinc keyword where
double CalculateWeight<T>(IEnumerable<T> animals)
where T : IAnimal
{
var total = animals.Sum(a => a.Weight);
return total;
}
Tuples
// Note the named tupple Min, Max
(int Min, int Max) GetMinAndMax(int[] numbers)
{
...
return (min, max);
}
var minAndMaxNamedTuple = GetMinAndMax(numbers);
Console.WriteLine($"Min: {minAndMaxNamedTuple.Min}, Max: {minAndMaxNamedTuple.Max}");
// you can use deconstruction too with tuples
(int firstThing, string secondThing) = (1, "this is a string");
var (firstThing, secondThing) = (1, "this is a string");
(int minVal, int maxVal) = GetMinAndMax(numbers);
// you can even discard unused values
(int minVal, _) = GetMinAndMax(numbers);
Delegates
Delegate - take a method and put it into a parameter (variable)
Action - void method (variable of type Action)
Action action = SomeMethod;
void SomeMethod()
{
Comsole.WriteLine("Hello World");
}
// to invoke the delegate method
action();
// or
action.Invoke();
Func - delegate type with a return type
// The very last type parameter is a return type. In this instance, it is returning a string for both
Func<int, int, string> addFunction = AddFunction;
Func<int, int, string> substractFunction = SubstractFunction;
string AddFunction(int a, int b)
{
return $"{a + b}";
}
string SubstractFunction(int a, int b)
{
return $"{a - b}";
}
Callbacks
Callback - ability to run a code at some point later.
void SomeMethod()
{
Comsole.WriteLine("Hello World");
}
void DoSomethingAfterUserPressEnter(Action callback)
{
Console.WriteLine("Press enter for a surprise!");
Console.ReadLine();
callback(); // note the () here because the call of this method is actually here
}
// Note there is no () in the passed method, as we don't want to run it BEFORE entering the DoSomethingAfterUserPressEnter method
// we are just passing the method ass signature to be invoked at some point in DoSomethingAfterUserPressEnter method
DoSomethingAfterUserPressEnter(SomeMethod);
Declare a custom delegate
delegate int CalculateDelegate(
int firstNumber,
int secondNumber);
int AddFunction(int a, int b)
{
return a + b;
}
int SubstractFunction(int a, int b)
{
return a - b;
}
CalculateDelegate addDelegate = AddFunction;
CalculateDelegate substractDelegate = SubstractFunction;
void Calculate(CalculateDelegate calculateCallback)
{
Console.WriteLine("Enter the first integer: ");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the second integer: ");
int b = int.Parse(Console.ReadLine());
int result = calculateCallback(a, b);
Console.WriteLine($"The result is: {result}");
}
Calculate(addDelegate);
Calculate(substractDelegate);
internal access modifier
internal access modifier restricts access and visibility to the classes within the same assembly, internal methods and classes are non visible from other assemblies.