1 of 34

Lesson 13: More beautiful C#

2 of 34

Agenda

  • Tuples
  • Deconstruction
  • Discard operator
  • Expression trees
  • GC
  • Unsafe code
  • Preprocessor
  • Calling private method from outside a class
  • Creating objects without knowing their type

3 of 34

Tuples

4 of 34

Tuple

  • Tuples are data structures to carry a known set of items
  • They can be useful when returning multiple values
  • They are generic and can have many values (not just 2)
  • Reference type

5 of 34

ValueTuple

  • Value type tuple
  • Works exactly like Tuple
  • Is a default target for storing multiple values

https://stackoverflow.com/questions/41084411/whats-the-difference-between-system-valuetuple-and-system-tuple

6 of 34

Deconstructing tuples

  • You don’t have to create a tuple in order to return multiple values with it
  • Under the hood ValueTuple is returned in DemoTwoValuesReturn

7 of 34

Deconstructing other types

  • void Deconstruct method
  • out every parameter you want to deconstruct

8 of 34

Discard operator “_”

  • Sometimes we don’t want to out all the data
  • Maybe we don’t need any of it
  • For example in TryParse we might only want if the parse was success

9 of 34

Expression trees

10 of 34

Expression trees

  • Is a way to translate code into data (expressions)
  • For every code element there is a kind of expression

https://docs.microsoft.com/en-us/archive/blogs/charlie/expression-tree-basics

11 of 34

Why do we need to translate code to data?

  • Because we want to do the opposite- to translate data back in code
  • If we want to execute code in multiple languages, maybe our domain-specific language- that’s the way to do it!

12 of 34

Practical examples: dynamic LINQ

  • Another application- making combos of LINQ expressions

13 of 34

Practical examples: LINQ to SQL

  • Is a way to translate code into data (expressions)
  • For every code element there is a kind of expression

https://docs.microsoft.com/en-us/archive/blogs/charlie/expression-tree-basics

14 of 34

GC

15 of 34

Memory allocation

16 of 34

GC

  • GC is Garbage Collector
  • It exists in managed languages like C# or Java
  • This means that we don’t care about freeing up memory of objects that we no longer use
  • It also defragments the memory gaps

17 of 34

When does GC get activated

  • The system has low physical memory
  • The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs
  • The GC.Collect method is called

18 of 34

3 simple rules

  • Avoid calling GC manually
  • Understand, that GC frees memory allocated on the heap
  • Object is ready to be GC’ed only when it is no longer referenced by anything (out of scope, not referenced by other object)

19 of 34

Unsafe code

20 of 34

Unsafe code

  • There is a way to call code outside of CLR (common language runtime)
  • This means we can call unmanaged code in C#
  • It’s disabled by default
  • In order to enable it, inside: .csproj add this line

21 of 34

Pandora box of unsafe code

  • There are no guards and many risks for permanently corrupting memory
  • Enables two features:
    • Pointers
    • Fixed buffer
  • All unsafe code requires either unsafe type or unsafe code block

22 of 34

Pointer

  • Pointer is similar to a reference type
  • However, the fundamental difference is that a pointer points to a point in memory
  • Reference points to an object
  • Pointer is static

23 of 34

Pointer example

24 of 34

Fixed

  • Fixed is used for a fixed size array
  • Such array is declared with a size
  • Cannot be initialized

25 of 34

Fixed: benefits

  • Stays on the stack
  • Does not get called by GC

26 of 34

Fixed: problems

  • Does not get defragmented until application is closed
  • Should not be abused

27 of 34

Preprocessor directives

28 of 34

Preprocessor

  • Used for conditional compiling

29 of 34

#if..#endif

  • Will compile only in a certain environment
  • Environments can be:
    • Debugging
    • Some version of .NET
    • Debug symbol

30 of 34

#pragma

  • Can turn off warnings

31 of 34

Doing the impossible with reflection!

32 of 34

Calling private method from outside a class

33 of 34

Activator

34 of 34

Compiled Lambda