├── favicon.ico ├── .github ├── CODEOWNERS ├── PULL_REQUEST_TEMPLATE.md ├── workflows │ └── main.yml └── ISSUE_TEMPLATE.md ├── src ├── Finished │ ├── ValRef │ │ ├── ValRef.csproj │ │ └── Program.cs │ ├── Classes │ │ ├── Defining │ │ │ ├── Defining.csproj │ │ │ └── Program.cs │ │ ├── Equality │ │ │ ├── Equality.csproj │ │ │ └── Program.cs │ │ └── Inheritance │ │ │ ├── Inheritance.csproj │ │ │ └── Program.cs │ ├── Records │ │ ├── Defining │ │ │ ├── Defining.csproj │ │ │ └── Program.cs │ │ ├── AutoFeatures │ │ │ ├── AutoFeatures.csproj │ │ │ └── Program.cs │ │ ├── Immutability │ │ │ ├── Immutability.csproj │ │ │ └── Program.cs │ │ ├── Inheritance │ │ │ ├── Inheritance.csproj │ │ │ └── Program.cs │ │ └── RecordStructs │ │ │ ├── RecordStructs.csproj │ │ │ └── Program.cs │ └── Structs │ │ ├── Defining │ │ ├── Defining.csproj │ │ └── Program.cs │ │ ├── Immutable │ │ ├── Immutable.csproj │ │ └── Program.cs │ │ ├── StructMethods │ │ ├── StructMethods.csproj │ │ └── Program.cs │ │ └── ValueSemantics │ │ ├── ValueSemantics.csproj │ │ └── Program.cs └── Start │ ├── Records │ ├── Defining │ │ ├── Program.cs │ │ └── Defining.csproj │ ├── Inheritance │ │ ├── Inheritance.csproj │ │ └── Program.cs │ ├── AutoFeatures │ │ ├── AutoFeatures.csproj │ │ └── Program.cs │ ├── Immutability │ │ ├── Immutability.csproj │ │ └── Program.cs │ └── RecordStructs │ │ ├── RecordStructs.csproj │ │ └── Program.cs │ ├── Classes │ ├── Defining │ │ ├── Defining.csproj │ │ └── Program.cs │ ├── Equality │ │ ├── Equality.csproj │ │ └── Program.cs │ └── Inheritance │ │ ├── Inheritance.csproj │ │ └── Program.cs │ └── Structs │ ├── Defining │ ├── Defining.csproj │ └── Program.cs │ ├── Immutable │ ├── Immutable.csproj │ └── Program.cs │ ├── StructMethods │ ├── StructMethods.csproj │ └── Program.cs │ └── ValueSemantics │ ├── ValueSemantics.csproj │ └── Program.cs ├── CONTRIBUTING.md ├── NOTICE ├── .devcontainer ├── Dockerfile └── devcontainer.json ├── .vscode └── settings.json ├── README.md ├── .gitignore └── LICENSE /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkedInLearning/advanced-c-sharp-classes-records-and-structs-2712014/main/favicon.ico -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/Finished/ValRef/ValRef.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Defining Records Example 3 | 4 | 5 | // Create a new instance of a record 6 | 7 | 8 | 9 | // Records can be defined as if they are regular classes 10 | 11 | 12 | // Defining a record in one line 13 | -------------------------------------------------------------------------------- /src/Start/Classes/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Classes/Equality/Equality.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Structs/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Classes/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Classes/Equality/Equality.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Records/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Structs/Defining/Defining.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Structs/Immutable/Immutable.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Classes/Inheritance/Inheritance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/Inheritance/Inheritance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Structs/Immutable/Immutable.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Classes/Inheritance/Inheritance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Records/AutoFeatures/AutoFeatures.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Records/Immutability/Immutability.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Records/Inheritance/Inheritance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/AutoFeatures/AutoFeatures.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/Immutability/Immutability.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Records/RecordStructs/RecordStructs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Structs/StructMethods/StructMethods.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Records/RecordStructs/RecordStructs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Structs/StructMethods/StructMethods.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Finished/Structs/ValueSemantics/ValueSemantics.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Start/Structs/ValueSemantics/ValueSemantics.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /src/Start/Structs/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Defining and initializing structs 3 | 4 | // Instantiate a new instance of the Rectangle - note that you have to initialize 5 | // all of the instance variables before you can use the struct 6 | 7 | 8 | // Change some values 9 | 10 | 11 | // define a struct to represent a Rectangle 12 | 13 | -------------------------------------------------------------------------------- /src/Start/Records/Inheritance/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Records Inheritance Example 3 | 4 | var r1 = new Rectangle(5,0,0,100,200); 5 | Console.WriteLine($"{r1}"); 6 | 7 | var c1 = new Circle(10, 20); 8 | Console.WriteLine($"{c1}"); 9 | 10 | // Equality depends upon the run-time type 11 | 12 | 13 | // Define a base record for a common property 14 | 15 | 16 | // Define two records that inherit from Shape 17 | public record Rectangle(int Border, int Top, int Left, int Right, int Bottom); 18 | public record Circle(int Border, int Radius); 19 | -------------------------------------------------------------------------------- /src/Finished/Records/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Defining Records Example 3 | 4 | 5 | // Create a new instance of a record 6 | var r1 = new Rectangle(0,0,100,200); 7 | Console.WriteLine($"{r1}"); 8 | 9 | var c1 = new Circle() {Radius = 20}; 10 | Console.WriteLine($"{c1}"); 11 | 12 | 13 | // Records can be defined as if they are regular classes 14 | public record Circle() { 15 | public int Radius { get; init; } 16 | } 17 | 18 | // Defining a record in one line 19 | public record Rectangle(int Top, int Left, int Right, int Bottom); 20 | -------------------------------------------------------------------------------- /src/Start/Records/AutoFeatures/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Automatic Records Features Example 3 | using System.Text; 4 | 5 | 6 | // Create a few records 7 | var r1 = new Rectangle(0, 0, 10, 20); 8 | var r2 = new Rectangle(0, 0, 10, 20); 9 | var r3 = new Rectangle(0, 0, 30, 30); 10 | 11 | 12 | // Value-based Equality Checking 13 | 14 | 15 | // Synthesized GetHashCode() 16 | 17 | 18 | // Built-in Formatting (ToString and PrintMembers) 19 | 20 | 21 | // Define a Record type 22 | public record Rectangle(int Top, int Left, int Right, int Bottom); 23 | 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2024 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /src/Start/Records/RecordStructs/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Defining Record Structs Example 3 | using System.Text; 4 | 5 | 6 | var r1 = new Rectangle(0, 0, 10, 20); 7 | var r2 = new Rectangle(0, 0, 10, 20); 8 | var r3 = new Rectangle(0, 0, 30, 30); 9 | 10 | // Automatic Formatting 11 | Console.WriteLine($"{r1}"); 12 | 13 | // Value-based Equality Checking 14 | Console.WriteLine($"r1 == r2: {r1 == r2}"); 15 | Console.WriteLine($"r2 == r3: {r1 == r3}"); 16 | Console.WriteLine($"r1 != r3: {r2 != r3}"); 17 | 18 | // Simple Declaration and custom output formatting 19 | public record Rectangle(int Top, int Left, int Right, int Bottom); 20 | -------------------------------------------------------------------------------- /src/Start/Structs/StructMethods/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Defining members within structs 3 | 4 | 5 | // Create the rectangle 6 | 7 | 8 | // Access the property 9 | 10 | 11 | // Invoke the ToString() method 12 | 13 | 14 | // define a struct to represent a Rectangle 15 | // struct members can be public, internal, or private 16 | struct Rectangle { 17 | public int Top; 18 | public int Left; 19 | public int Bottom; 20 | public int Right; 21 | 22 | // Provide a constructor to support object initializer 23 | 24 | 25 | // Define a property 26 | 27 | 28 | // Override the ToString method from the base object 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/Start/Structs/ValueSemantics/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Understanding struct value semantics 3 | 4 | Rectangle r1, r2; 5 | 6 | r1.Top = 0; 7 | r1.Left = 0; 8 | r1.Right = 100; 9 | r1.Bottom = 200; 10 | 11 | r2.Top = 0; 12 | r2.Left = 0; 13 | r2.Right = 100; 14 | r2.Bottom = 200; 15 | 16 | // Equality comparison is performed on struct values 17 | 18 | 19 | // Objects do not refer to the same memory block 20 | 21 | 22 | // structs cannot be declared as constants 23 | 24 | 25 | // define a struct to represent a Rectangle 26 | struct Rectangle { 27 | public int Top; 28 | public int Left; 29 | public int Bottom; 30 | public int Right; 31 | } 32 | -------------------------------------------------------------------------------- /src/Finished/Records/Inheritance/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Records Inheritance Example 3 | 4 | var r1 = new Rectangle(5,0,0,100,200); 5 | Console.WriteLine($"{r1}"); 6 | 7 | var c1 = new Circle(10, 20); 8 | Console.WriteLine($"{c1}"); 9 | 10 | // Equality depends upon the run-time type 11 | Shape r2 = new Rectangle(1, 5, 5, 10, 10); 12 | Shape c2 = new Circle(1, 5); 13 | Console.WriteLine($"{r2 == c2}"); 14 | 15 | // Define a base record for a common property 16 | public abstract record Shape(int Border); 17 | 18 | // Define two records that inherit from Shape 19 | public record Rectangle(int Border, int Top, int Left, int Right, int Bottom) 20 | : Shape(Border); 21 | public record Circle(int Border, int Radius) 22 | : Shape(Border); 23 | -------------------------------------------------------------------------------- /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | # [Choice] .NET version: 6.0, 3.1, 6.0-bullseye, 3.1-bullseye, 6.0-focal, 3.1-focal 2 | ARG VARIANT=6.0-bullseye 3 | FROM mcr.microsoft.com/vscode/devcontainers/dotnet:0-${VARIANT} 4 | 5 | # [Choice] Node.js version: none, lts/*, 18, 16, 14 6 | ARG NODE_VERSION="none" 7 | RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi 8 | 9 | # [Optional] Uncomment this section to install additional OS packages. 10 | # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 11 | # && apt-get -y install --no-install-recommends 12 | 13 | # [Optional] Uncomment this line to install global node packages. 14 | # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 -------------------------------------------------------------------------------- /src/Finished/Structs/ValueSemantics/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Understanding struct value semantics 3 | 4 | Rectangle r1, r2; 5 | 6 | r1.Top = 0; 7 | r1.Left = 0; 8 | r1.Right = 100; 9 | r1.Bottom = 200; 10 | 11 | r2.Top = 0; 12 | r2.Left = 0; 13 | r2.Right = 100; 14 | r2.Bottom = 200; 15 | 16 | // Equality comparison is performed on struct values 17 | Console.WriteLine($"{r1.Equals(r2)}"); 18 | 19 | // Objects do not refer to the same memory block 20 | Rectangle r3 = r2; 21 | r3.Bottom = 20; 22 | Console.WriteLine($"{r2.Bottom}, {r3.Bottom}"); 23 | 24 | // structs cannot be declared as constants 25 | // const Rectangle r3 = new(); 26 | 27 | // define a struct to represent a Rectangle 28 | struct Rectangle { 29 | public int Top; 30 | public int Left; 31 | public int Bottom; 32 | public int Right; 33 | } 34 | -------------------------------------------------------------------------------- /src/Finished/Records/RecordStructs/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Defining Record Structs Example 3 | using System.Text; 4 | 5 | 6 | var r1 = new Rectangle(0, 0, 10, 20); 7 | var r2 = new Rectangle(0, 0, 10, 20); 8 | var r3 = new Rectangle(0, 0, 30, 30); 9 | 10 | // Automatic Formatting 11 | Console.WriteLine($"{r1}"); 12 | 13 | // Value-based Equality Checking 14 | Console.WriteLine($"r1 == r2: {r1 == r2}"); 15 | Console.WriteLine($"r2 == r3: {r1 == r3}"); 16 | Console.WriteLine($"r1 != r3: {r2 != r3}"); 17 | 18 | // Simple Declaration and custom output formatting 19 | readonly record struct Rectangle (int Top, int Left, int Right, int Bottom) { 20 | private bool PrintMembers(StringBuilder stringBuilder) { 21 | stringBuilder.Append($"Top, Left: {Top},{Left} Bottom, Right: {Bottom},{Right}"); 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Finished/ValRef/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Simple demonstration of value and reference types 3 | 4 | 5 | int x = 10; 6 | 7 | void ModifyTheValType(int theVal) { 8 | theVal += 10; 9 | Console.WriteLine($"Value: {theVal}"); 10 | } 11 | 12 | Console.WriteLine($"x: {x}"); 13 | ModifyTheValType(x); 14 | Console.WriteLine($"x: {x}"); 15 | 16 | // --------------------------------------------------- 17 | 18 | Person p1 = new(); 19 | p1.Name = "John Doe"; 20 | Person p2 = p1; 21 | 22 | void ModifyTheRefType(Person person) { 23 | person.Name = "Jane Doh"; 24 | } 25 | 26 | Console.WriteLine($"p1: {p1.Name}"); 27 | Console.WriteLine($"P2: {p2.Name}"); 28 | ModifyTheRefType(p2); 29 | Console.WriteLine($"p1: {p1.Name}"); 30 | Console.WriteLine($"p2: {p2.Name}"); 31 | 32 | public class Person { 33 | public string Name; 34 | } 35 | -------------------------------------------------------------------------------- /src/Finished/Structs/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Defining and initializing structs 3 | 4 | // Instantiate a new instance of the Rectangle - note that you have to initialize 5 | // all of the instance variables before you can use the struct 6 | Rectangle r1; 7 | r1.Top = 0; 8 | r1.Left = 0; 9 | r1.Right = 100; 10 | r1.Bottom = 200; 11 | Console.WriteLine($"Area: {(r1.Bottom - r1.Top) * (r1.Right - r1.Left)}"); 12 | 13 | // Change some values 14 | r1.Top = 50; 15 | r1.Left = 50; 16 | Console.WriteLine($"Area: {(r1.Bottom - r1.Top) * (r1.Right - r1.Left)}"); 17 | 18 | Rectangle r2 = new(); 19 | Console.WriteLine($"Area: {(r2.Bottom - r2.Top) * (r2.Right - r2.Left)}"); 20 | 21 | // define a struct to represent a Rectangle 22 | struct Rectangle { 23 | public int Top; 24 | public int Left; 25 | public int Bottom; 26 | public int Right; 27 | } 28 | -------------------------------------------------------------------------------- /src/Start/Records/Immutability/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Immutability of Records Example 3 | 4 | 5 | // Create a new instance of a record 6 | var r1 = new Rectangle(0,0,100,200); 7 | Console.WriteLine($"{r1}"); 8 | 9 | var c1 = new Circle() {Radius = 20}; 10 | Console.WriteLine($"{c1}"); 11 | 12 | // Try to set a property of a record 13 | // c1.Radius = 10; 14 | // Console.WriteLine($"{c1}"); 15 | 16 | // Use the "with" expression to create a new record based on another 17 | 18 | 19 | // Create a non-mutated copy of an existing Rectangle 20 | 21 | 22 | // Define a record for Rectangle 23 | public record Rectangle(int Top, int Left, int Right, int Bottom); 24 | 25 | // Records can be defined as if they are regular classes 26 | // You can specify a "setter" property to make them mutable, but not advisable 27 | public record Circle() { 28 | public int Radius { get; init; } 29 | } 30 | -------------------------------------------------------------------------------- /src/Finished/Records/AutoFeatures/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Automatic Records Features Example 3 | using System.Text; 4 | 5 | 6 | // Create a few records 7 | var r1 = new Rectangle(0, 0, 10, 20); 8 | var r2 = new Rectangle(0, 0, 10, 20); 9 | var r3 = new Rectangle(0, 0, 30, 30); 10 | 11 | 12 | // Value-based Equality Checking 13 | Console.WriteLine($"r1 == r2: {r1 == r2}"); 14 | Console.WriteLine($"r2 == r3: {r1 == r3}"); 15 | Console.WriteLine($"r1 != r3: {r2 != r3}"); 16 | 17 | // Synthesized GetHashCode() 18 | Console.WriteLine($"Hash code: {r1.GetHashCode()}"); 19 | 20 | // Built-in Formatting (ToString and PrintMembers) 21 | Console.WriteLine($"{r1}"); 22 | 23 | // Define a Record type 24 | public record Rectangle(int Top, int Left, int Right, int Bottom) { 25 | protected virtual bool PrintMembers(StringBuilder stringBuilder) 26 | { 27 | stringBuilder.Append($"Top, Left: {Top},{Left} Bottom, Right: {Bottom},{Right}"); 28 | return true; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Finished/Records/Immutability/Program.cs: -------------------------------------------------------------------------------- 1 | // Advanced C#: Classes, Records, and Structs by Joe Marini 2 | // Immutability of Records Example 3 | 4 | 5 | // Create a new instance of a record 6 | var r1 = new Rectangle(0,0,100,200); 7 | Console.WriteLine($"{r1}"); 8 | 9 | var c1 = new Circle() {Radius = 20}; 10 | Console.WriteLine($"{c1}"); 11 | 12 | // Try to set a property of a record 13 | c1.Radius = 10; 14 | Console.WriteLine($"{c1}"); 15 | 16 | // Use the "with" expression to create a new record based on another 17 | var r2 = r1 with { 18 | Top = r1.Top + 5, 19 | Bottom = r1.Bottom + 20 20 | }; 21 | Console.WriteLine($"{r2}"); 22 | 23 | // Create a non-mutated copy of an existing Rectangle 24 | var r3 = r1 with {}; 25 | Console.WriteLine($"{r3}"); 26 | 27 | // Define a record for Rectangle 28 | public record Rectangle(int Top, int Left, int Right, int Bottom); 29 | 30 | // Records can be defined as if they are regular classes 31 | // You can specify a "setter" property to make them mutable, but not advisable 32 | public record Circle() { 33 | public int Radius { get; set; } 34 | } 35 | -------------------------------------------------------------------------------- /src/Finished/Structs/StructMethods/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Defining members within structs 3 | 4 | 5 | // Create the rectangle 6 | Rectangle r = new(0, 0, 50, 60); 7 | 8 | // Access the property 9 | Console.WriteLine($"{r.Area}"); 10 | 11 | // Invoke the ToString() method 12 | Console.WriteLine($"{r}"); 13 | 14 | // define a struct to represent a Rectangle 15 | // struct members can be public, internal, or private 16 | struct Rectangle { 17 | int Top; 18 | int Left; 19 | int Bottom; 20 | int Right; 21 | 22 | // Provide a constructor to support object initializer 23 | public Rectangle(int t, int l, int r, int b) { 24 | Top = t; 25 | Left = l; 26 | Right = r; 27 | Bottom = b; 28 | } 29 | 30 | // Define a property 31 | public int Area { 32 | get => (Bottom - Top) * (Right - Left); 33 | } 34 | 35 | // Override the ToString method from the base object 36 | public override string ToString() => $"Rectangle Area:{Area},Top:{Top},Left:{Left},Right:{Right},Bottom:{Bottom}"; 37 | } 38 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.bracketPairColorization.enabled": true, 3 | "editor.cursorBlinking": "solid", 4 | "editor.fontFamily": "ui-monospace, Menlo, Monaco, 'Cascadia Mono', 'Segoe UI Mono', 'Roboto Mono', 'Oxygen Mono', 'Ubuntu Monospace', 'Source Code Pro', 'Fira Mono', 'Droid Sans Mono', 'Courier New', monospace", 5 | "editor.fontLigatures": false, 6 | "editor.fontSize": 22, 7 | "editor.formatOnPaste": true, 8 | "editor.formatOnSave": true, 9 | "editor.lineNumbers": "on", 10 | "editor.matchBrackets": "always", 11 | "editor.minimap.enabled": false, 12 | "editor.smoothScrolling": true, 13 | "editor.tabSize": 2, 14 | "editor.useTabStops": true, 15 | "emmet.triggerExpansionOnTab": true, 16 | "explorer.openEditors.visible": 0, 17 | "files.autoSave": "afterDelay", 18 | "screencastMode.onlyKeyboardShortcuts": true, 19 | "terminal.integrated.fontSize": 18, 20 | "workbench.colorTheme": "Visual Studio Light", 21 | "workbench.fontAliasing": "antialiased", 22 | "workbench.statusBar.visible": true, 23 | "terminal.integrated.defaultProfile.linux": "pwsh" 24 | } 25 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ASPNET", 3 | // Configure tool-specific properties. 4 | "build": { 5 | "dockerfile": "Dockerfile", 6 | "args": { 7 | // Update 'VARIANT' to pick a .NET Core version: 3.1, 6.0 8 | // Append -bullseye or -focal to pin to an OS version. 9 | "VARIANT": "6.0-bullseye", 10 | // Options 11 | "NODE_VERSION": "lts/*" 12 | } 13 | }, 14 | "customizations": { 15 | // Configure properties specific to VS Code. 16 | "vscode": { 17 | // Add the IDs of extensions you want installed when the container is created. 18 | "extensions": [ 19 | "GitHub.github-vscode-theme", 20 | "ms-dotnettools.csharp" 21 | ] 22 | } 23 | }, 24 | // Use 'forwardPorts' to make a list of ports inside the container available locally. 25 | // "forwardPorts": [], 26 | // Use 'postCreateCommand' to run commands after the container is created. 27 | // "postCreateCommand": "gcc -v", 28 | "onCreateCommand": "echo PS1='\"$ \"' >> ~/.bashrc && dotnet dev-certs https", //Set Terminal Prompt to $ 29 | // Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root. 30 | "remoteUser": "vscode", 31 | "features": { 32 | "powershell": "latest" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Start/Structs/Immutable/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Creating immutable structs 3 | 4 | // Instantiate a new instance of the Rectangle 5 | Rectangle r1 = new(){Top=0,Right=20,Left=0,Bottom=30}; 6 | Console.WriteLine($"{r1}"); 7 | 8 | // Create another rectangle but with only two parameters 9 | Rectangle r2 = new(){Right=10, Bottom=10}; 10 | Console.WriteLine($"{r2}"); 11 | 12 | // Try to change a property (causes an error on readonly struct) 13 | r2.Top = 5; 14 | Console.WriteLine($"{r2}"); 15 | 16 | // Use non-destructive mutation to modify specific values: 17 | 18 | 19 | // The .NET DateTime is actually a struct 20 | 21 | 22 | // define a struct to represent a Rectangle 23 | // - Use the "readonly" modifier to indicate to the compiler that it is immutable 24 | // - Ensure that all property accessors are "get" or "init" 25 | // - Other members are private 26 | struct Rectangle { 27 | public int Top {get; set;} 28 | public int Left {get; set;} 29 | public int Bottom {get; set;} 30 | public int Right {get; set;} 31 | 32 | public Rectangle(int t, int l, int r, int b) { 33 | Top = t; 34 | Left = l; 35 | Right = r; 36 | Bottom = b; 37 | } 38 | 39 | public int Area { 40 | get => (Bottom - Top) * (Right - Left); 41 | } 42 | 43 | public override string ToString() => $"Rectangle Area:{Area},Top:{Top},Left:{Left},Right:{Right},Bottom:{Bottom}"; 44 | } 45 | -------------------------------------------------------------------------------- /src/Start/Classes/Equality/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Equality comparison in class objects 3 | 4 | 5 | Circle c1 = new Circle(5); 6 | Circle c2 = new Circle(5); 7 | Circle c3 = null; 8 | Circle c4 = new Circle(10); 9 | Circle c5 = null; 10 | 11 | // Default equality checking compares references, not values 12 | // Implementing the Equals operator overrides can resolve this 13 | Console.WriteLine($"{c1 == c2}"); 14 | Console.WriteLine($"{c3 == c5}"); 15 | Console.WriteLine($"{c4 == c2}"); 16 | Console.WriteLine($"{c4 != c2}"); 17 | 18 | // Test using Equals methods 19 | Console.WriteLine($"{object.Equals(c1, c2)}"); 20 | Console.WriteLine($"{c1.Equals(c2)}"); 21 | 22 | 23 | // 1. Define a class that implements the IEquatable interface 24 | public class Circle { 25 | public Circle(int radius) { 26 | Radius = radius; 27 | } 28 | 29 | public int Radius { 30 | get; set; 31 | } 32 | 33 | public float Area { 34 | get => (float)(Math.PI * (Radius * Radius)); 35 | } 36 | 37 | public override string ToString() => $"Circle Radius:{Radius}, Area:{Area}"; 38 | 39 | public void Resize(int Size) { 40 | Radius += Size; 41 | } 42 | 43 | // Checking value equality requires overriding Equals and GetHashCode, along with == and != 44 | // 2. Override the Equals method on object 45 | 46 | 47 | // 3. Implement an Equals function for this class 48 | 49 | 50 | // 4. Override GetHashCode 51 | 52 | 53 | // 5. Implement the == operator 54 | 55 | 56 | // 6. Implement the != operator 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/Finished/Structs/Immutable/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Creating immutable structs 3 | 4 | // Instantiate a new instance of the Rectangle 5 | Rectangle r1 = new(){Top=0,Right=20,Left=0,Bottom=30}; 6 | Console.WriteLine($"{r1}"); 7 | 8 | // Create another rectangle but with only two parameters 9 | Rectangle r2 = new(){Right=10, Bottom=10}; 10 | Console.WriteLine($"{r2}"); 11 | 12 | // Try to change a property (causes an error on readonly struct) 13 | // r2.Top = 5; 14 | // Console.WriteLine($"{r2}"); 15 | 16 | // Use non-destructive mutation to modify specific values: 17 | Rectangle r3 = r2 with {Top = 5, Left = 2}; 18 | Console.WriteLine($"{r3}"); 19 | 20 | // The .NET DateTime is actually a struct 21 | var dt = new DateTime(2030,4,1); 22 | Console.WriteLine($"{dt}"); 23 | var newdt = dt.AddDays(15); 24 | Console.WriteLine($"{newdt}"); 25 | 26 | // define a struct to represent a Rectangle 27 | // - Use the "readonly" modifier to indicate to the compiler that it is immutable 28 | // - Ensure that all property accessors are "get" or "init" 29 | // - Other members are private 30 | readonly struct Rectangle { 31 | public int Top {get; init;} 32 | public int Left {get; init;} 33 | public int Bottom {get; init;} 34 | public int Right {get; init;} 35 | 36 | public Rectangle(int t, int l, int r, int b) { 37 | Top = t; 38 | Left = l; 39 | Right = r; 40 | Bottom = b; 41 | } 42 | 43 | public int Area { 44 | get => (Bottom - Top) * (Right - Left); 45 | } 46 | 47 | public override string ToString() => $"Rectangle Area:{Area},Top:{Top},Left:{Left},Right:{Right},Bottom:{Bottom}"; 48 | } 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Advanced C#: Classes, Records, and Structs 2 | This is the repository for the LinkedIn Learning course `Advanced C#: Classes, Records, and Structs`. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![lil-thumbnail-url] 5 | 6 | The .NET Framework provides several ways of managing data within your application. Choosing the right tool for the job can help your app manage memory more efficiently and provide users with better performance. In this advanced course, learn the differences between structs, classes, and records in C#. Each data type has its own semantics for common scenarios such as equality testing, implementing behavior, and value/reference typing. All three data types provide great ways of working with structured data, but each one has its strengths and weaknesses. Tune in to better understand how these features can help you improve your code. 7 | 8 | This course includes Code Challenges powered by CoderPad. Code Challenges are interactive coding exercises with real-time feedback, so you can get hands-on coding practice alongside the course content to advance your programming skills. 9 | 10 | 11 | ### Instructor 12 | 13 | Joe Marini 14 | 15 | Technology Industry Veteran 16 | 17 | 18 | 19 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/joe-marini?u=104). 20 | 21 | 22 | [0]: # (Replace these placeholder URLs with actual course URLs) 23 | 24 | [lil-course-url]: https://www.linkedin.com/learning/advanced-c-sharp-classes-records-and-structs 25 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/D560DAQGYJAhPnqAJfg/learning-public-crop_675_1200/0/1717614797351?e=2147483647&v=beta&t=HFzwTakX7ZiUE7NGPnCDuYKJzpRgnkMmZzLzvTOODGo 26 | 27 | -------------------------------------------------------------------------------- /src/Start/Classes/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Class definition 3 | 4 | var r1 = new Rectangle(0, 0, 100, 200); 5 | Console.WriteLine($"{r1}"); 6 | 7 | var c1 = new Circle(10); 8 | Console.WriteLine($"{c1}"); 9 | 10 | 11 | public class Rectangle { 12 | // internal class member to hold the ID of an instance 13 | private string _id; 14 | 15 | // Constructor to create the class 16 | public Rectangle(int T, int R, int L, int B) { 17 | Top = T; 18 | Right = R; 19 | Left = L; 20 | Bottom = B; 21 | 22 | _id = Guid.NewGuid().ToString(); 23 | } 24 | 25 | // Classes can expose Properties that can be get, set, or init 26 | // Immutability requires init-only properties 27 | public int Top { 28 | get; set; 29 | } 30 | 31 | public int Right { 32 | get; set; 33 | } 34 | 35 | public int Left { 36 | get; set; 37 | } 38 | 39 | public int Bottom { 40 | get; set; 41 | } 42 | 43 | public string ID => _id; 44 | 45 | // Classes can override superclass methods 46 | 47 | 48 | // Classes typically contain logic and behavior 49 | 50 | } 51 | 52 | public class Circle { 53 | // internal class member to hold the ID of an instance 54 | private string _id; 55 | 56 | public Circle(int radius) { 57 | Radius = radius; 58 | _id = Guid.NewGuid().ToString(); 59 | } 60 | 61 | public int Radius { 62 | get; set; 63 | } 64 | 65 | public string ID => _id; 66 | 67 | public float Area { 68 | get => (float)(Math.PI * (Radius * Radius)); 69 | } 70 | 71 | // Classes can override superclass methods 72 | 73 | 74 | // Classes typically contain logic and behavior 75 | 76 | } 77 | -------------------------------------------------------------------------------- /src/Start/Classes/Inheritance/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Class-based inheritance 3 | 4 | 5 | var r1 = new Rectangle(5, 0, 100, 0, 200); 6 | Console.WriteLine($"{r1}"); 7 | 8 | var c1 = new Circle(3, 10); 9 | Console.WriteLine($"{c1}"); 10 | 11 | var s1 = new Shape(5); 12 | 13 | // Base classes let us centralize behavior in one place 14 | // Making them abstract prevents instantiation 15 | public class Shape { 16 | protected string _id; 17 | 18 | public Shape (int BorderSize) { 19 | BorderSize = BorderSize; 20 | _id = Guid.NewGuid().ToString(); 21 | } 22 | 23 | public string ID => _id; 24 | 25 | public virtual float Area { get => 0; } 26 | 27 | public int BorderSize { 28 | get; set; 29 | } 30 | } 31 | 32 | public class Rectangle : Shape { 33 | public Rectangle(int BS, int T, int R, int L, int B) : base(BS) { 34 | Top = T; 35 | Right = R; 36 | Left = L; 37 | Bottom = B; 38 | } 39 | public int Top { 40 | get; set; 41 | } 42 | 43 | public int Right { 44 | get; set; 45 | } 46 | 47 | public int Left { 48 | get; set; 49 | } 50 | 51 | public int Bottom { 52 | get; set; 53 | } 54 | 55 | public override string ToString() => $"Rectangle Top:{Top}, Left:{Left}, Right:{Right}, Bottom:{Bottom}, Area: {Area}, ID:{ID}"; 56 | } 57 | 58 | // Sealing a class prevents the class from being derived from 59 | public class Circle : Shape { 60 | public Circle(int BS, int radius) : base(BS) { 61 | Radius = radius; 62 | } 63 | 64 | public int Radius { 65 | get; set; 66 | } 67 | 68 | public override float Area { 69 | get => (float)(Math.PI * (Radius * Radius)); 70 | } 71 | 72 | public override string ToString() => $"Circle Radius:{Radius}, Area:{Area}, ID:{ID}"; 73 | } 74 | 75 | public class BetterCircle : Circle { 76 | public BetterCircle(int b, int r):base(b, r) {} 77 | } 78 | -------------------------------------------------------------------------------- /src/Finished/Classes/Inheritance/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Class-based inheritance 3 | 4 | 5 | var r1 = new Rectangle(5, 0, 100, 0, 200); 6 | Console.WriteLine($"{r1}"); 7 | 8 | var c1 = new Circle(3, 10); 9 | Console.WriteLine($"{c1}"); 10 | 11 | // var s1 = new Shape(5); 12 | 13 | // Base classes let us centralize behavior in one place 14 | // Making them abstract prevents instantiation 15 | public abstract class Shape { 16 | protected string _id; 17 | 18 | public Shape (int BorderSize) { 19 | BorderSize = BorderSize; 20 | _id = Guid.NewGuid().ToString(); 21 | } 22 | 23 | public string ID => _id; 24 | 25 | public abstract float Area { get; } 26 | 27 | public int BorderSize { 28 | get; set; 29 | } 30 | } 31 | 32 | public class Rectangle : Shape { 33 | public Rectangle(int BS, int T, int R, int L, int B) : base(BS) { 34 | Top = T; 35 | Right = R; 36 | Left = L; 37 | Bottom = B; 38 | } 39 | public int Top { 40 | get; set; 41 | } 42 | 43 | public int Right { 44 | get; set; 45 | } 46 | 47 | public int Left { 48 | get; set; 49 | } 50 | 51 | public int Bottom { 52 | get; set; 53 | } 54 | 55 | public override float Area { 56 | get => (float)((Right - Left) * (Bottom - Top)); 57 | } 58 | 59 | public override string ToString() => $"Rectangle Top:{Top}, Left:{Left}, Right:{Right}, Bottom:{Bottom}, Area: {Area}, ID:{ID}"; 60 | } 61 | 62 | // Sealing a class prevents the class from being derived from 63 | public sealed class Circle : Shape { 64 | public Circle(int BS, int radius) : base(BS) { 65 | Radius = radius; 66 | } 67 | 68 | public int Radius { 69 | get; set; 70 | } 71 | 72 | public override float Area { 73 | get => (float)(Math.PI * (Radius * Radius)); 74 | } 75 | 76 | public override string ToString() => $"Circle Radius:{Radius}, Area:{Area}, ID:{ID}"; 77 | } 78 | 79 | // public class BetterCircle : Circle { 80 | // public BetterCircle(int b, int r):base(b, r) {} 81 | // } 82 | -------------------------------------------------------------------------------- /src/Finished/Classes/Defining/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Class definition 3 | 4 | var r1 = new Rectangle(0, 0, 100, 200); 5 | Console.WriteLine($"{r1}"); 6 | r1.Resize(50,50); 7 | Console.WriteLine($"{r1}"); 8 | 9 | var c1 = new Circle(10); 10 | Console.WriteLine($"{c1}"); 11 | c1.Resize(10); 12 | Console.WriteLine($"{c1}"); 13 | 14 | 15 | public class Rectangle { 16 | // internal class member to hold the ID of an instance 17 | private string _id; 18 | 19 | // Constructor to create the class 20 | public Rectangle(int T, int R, int L, int B) { 21 | Top = T; 22 | Right = R; 23 | Left = L; 24 | Bottom = B; 25 | 26 | _id = Guid.NewGuid().ToString(); 27 | } 28 | 29 | // Classes can expose Properties that can be get, set, or init 30 | // Immutability requires init-only properties 31 | public int Top { 32 | get; set; 33 | } 34 | 35 | public int Right { 36 | get; set; 37 | } 38 | 39 | public int Left { 40 | get; set; 41 | } 42 | 43 | public int Bottom { 44 | get; set; 45 | } 46 | 47 | public string ID => _id; 48 | 49 | // Classes can override superclass methods 50 | public override string ToString() => $"Rectangle Top:{Top}, Left:{Left}, Right:{Right}, Bottom:{Bottom}, ID:{ID}"; 51 | 52 | // Classes typically contain logic and behavior 53 | public void Resize(int Width, int Height) { 54 | Right += Width; 55 | Bottom += Height; 56 | } 57 | } 58 | 59 | public class Circle { 60 | // internal class member to hold the ID of an instance 61 | private string _id; 62 | 63 | public Circle(int radius) { 64 | Radius = radius; 65 | _id = Guid.NewGuid().ToString(); 66 | } 67 | 68 | public int Radius { 69 | get; set; 70 | } 71 | 72 | public string ID => _id; 73 | 74 | public float Area { 75 | get => (float)(Math.PI * (Radius * Radius)); 76 | } 77 | 78 | public override string ToString() => $"Circle Radius:{Radius}, Area:{Area}, ID:{ID}"; 79 | 80 | public void Resize(int Size) { 81 | Radius += Size; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/Finished/Classes/Equality/Program.cs: -------------------------------------------------------------------------------- 1 | // Example file for C#: Records, Classes, and Structs by Joe Marini 2 | // Equality comparison in class objects 3 | 4 | 5 | Circle c1 = new Circle(5); 6 | Circle c2 = new Circle(5); 7 | Circle c3 = null; 8 | Circle c4 = new Circle(10); 9 | Circle c5 = null; 10 | 11 | // Default equality checking compares references, not values 12 | // Implementing the Equals operator overrides can resolve this 13 | Console.WriteLine($"{c1 == c2}"); 14 | Console.WriteLine($"{c3 == c5}"); 15 | Console.WriteLine($"{c4 == c2}"); 16 | Console.WriteLine($"{c4 != c2}"); 17 | 18 | // Test using Equals methods 19 | Console.WriteLine($"{object.Equals(c1, c2)}"); 20 | Console.WriteLine($"{c1.Equals(c2)}"); 21 | 22 | 23 | // 1. Define a class that implements the IEquatable interface 24 | public class Circle : IEquatable { 25 | public Circle(int radius) { 26 | Radius = radius; 27 | } 28 | 29 | public int Radius { 30 | get; set; 31 | } 32 | 33 | public float Area { 34 | get => (float)(Math.PI * (Radius * Radius)); 35 | } 36 | 37 | public override string ToString() => $"Circle Radius:{Radius}, Area:{Area}"; 38 | 39 | public void Resize(int Size) { 40 | Radius += Size; 41 | } 42 | 43 | // Checking value equality requires overriding Equals and GetHashCode, along with == and != 44 | // 2. Override the Equals method on object 45 | public override bool Equals(object obj) => this.Equals(obj as Circle); 46 | 47 | // 3. Implement an Equals function for this class 48 | public bool Equals(Circle c) { 49 | // if the object being compared to is null, return false 50 | if (c is null) { return false; } 51 | 52 | // if the run-time types aren't the same, return false 53 | if (this.GetType() != c.GetType()) { 54 | return false; 55 | } 56 | 57 | return (this.Radius == c.Radius); 58 | } 59 | 60 | // 4. Override GetHashCode 61 | public override int GetHashCode() => Radius.GetHashCode(); 62 | 63 | // 5. Implement the == operator 64 | public static bool operator == (Circle lhs, Circle rhs) 65 | { 66 | if (lhs is null) { 67 | if (rhs is null) { 68 | return true; 69 | } 70 | 71 | // Only the left side is null. 72 | return false; 73 | } 74 | // The instance Equals handles case of null on right-hand side 75 | return lhs.Equals(rhs); 76 | } 77 | 78 | // 6. Implement the != operator 79 | public static bool operator != (Circle lhs, Circle rhs) => !(lhs == rhs); 80 | } 81 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Apple 2 | *.DS_Store 3 | 4 | # Built application files 5 | *.apk 6 | *.ap_ 7 | 8 | # Files for the Dalvik VM 9 | *.dex 10 | 11 | # Java class files 12 | *.class 13 | 14 | # Generated files 15 | bin/ 16 | gen/ 17 | 18 | # Gradle files 19 | .gradle/ 20 | build/ 21 | 22 | # Local configuration file (sdk path, etc) 23 | local.properties 24 | 25 | # Proguard folder generated by Eclipse 26 | proguard/ 27 | 28 | # Log Files 29 | *.log 30 | 31 | # Android Studio Navigation editor temp files 32 | .navigation/ 33 | 34 | # Android Studio captures folder 35 | captures/ 36 | 37 | # Google Doc files 38 | *.gsheet 39 | *.gslides 40 | *.gdoc 41 | 42 | # MS Office files 43 | *.xls_ 44 | *.doc_ 45 | *.ppt_ 46 | 47 | # PDF files 48 | *.pdf 49 | 50 | # ZIP files 51 | *.zip 52 | 53 | # VISUAL STUDIO FILES 54 | 55 | # User-specific files 56 | *.suo 57 | *.user 58 | *.userosscache 59 | *.sln.docstates 60 | 61 | # User-specific files (MonoDevelop/Xamarin Studio) 62 | *.userprefs 63 | 64 | # Build results 65 | [Dd]ebug/ 66 | [Dd]ebugPublic/ 67 | [Rr]elease/ 68 | [Rr]eleases/ 69 | x64/ 70 | x86/ 71 | bld/ 72 | [Bb]in/ 73 | [Oo]bj/ 74 | [Ll]og/ 75 | __pycache__/ 76 | 77 | # Visual Studio 2015 cache/options directory 78 | .vs/ 79 | # Uncomment if you have tasks that create the project's static files in wwwroot 80 | #wwwroot/ 81 | 82 | # MSTest test Results 83 | [Tt]est[Rr]esult*/ 84 | [Bb]uild[Ll]og.* 85 | 86 | # NUNIT 87 | *.VisualState.xml 88 | TestResult.xml 89 | 90 | # Build Results of an ATL Project 91 | [Dd]ebugPS/ 92 | [Rr]eleasePS/ 93 | dlldata.c 94 | 95 | # DNX 96 | project.lock.json 97 | artifacts/ 98 | 99 | *_i.c 100 | *_p.c 101 | *_i.h 102 | *.ilk 103 | *.meta 104 | *.obj 105 | *.pch 106 | *.pdb 107 | *.pgc 108 | *.pgd 109 | *.rsp 110 | *.sbr 111 | *.tlb 112 | *.tli 113 | *.tlh 114 | *.tmp 115 | *.tmp_proj 116 | *.log 117 | *.vspscc 118 | *.vssscc 119 | .builds 120 | *.pidb 121 | *.svclog 122 | *.scc 123 | 124 | # Chutzpah Test files 125 | _Chutzpah* 126 | 127 | # Visual C++ cache files 128 | ipch/ 129 | *.aps 130 | *.ncb 131 | *.opendb 132 | *.opensdf 133 | *.sdf 134 | *.cachefile 135 | *.VC.db 136 | *.VC.VC.opendb 137 | 138 | # Visual Studio profiler 139 | *.psess 140 | *.vsp 141 | *.vspx 142 | *.sap 143 | 144 | # TFS 2012 Local Workspace 145 | $tf/ 146 | 147 | # Guidance Automation Toolkit 148 | *.gpState 149 | 150 | # ReSharper is a .NET coding add-in 151 | _ReSharper*/ 152 | *.[Rr]e[Ss]harper 153 | *.DotSettings.user 154 | 155 | # JustCode is a .NET coding add-in 156 | .JustCode 157 | 158 | # TeamCity is a build add-in 159 | _TeamCity* 160 | 161 | # DotCover is a Code Coverage Tool 162 | *.dotCover 163 | 164 | # NCrunch 165 | _NCrunch_* 166 | .*crunch*.local.xml 167 | nCrunchTemp_* 168 | 169 | # MightyMoose 170 | *.mm.* 171 | AutoTest.Net/ 172 | 173 | # Web workbench (sass) 174 | .sass-cache/ 175 | 176 | # Installshield output folder 177 | [Ee]xpress/ 178 | 179 | # DocProject is a documentation generator add-in 180 | DocProject/buildhelp/ 181 | DocProject/Help/*.HxT 182 | DocProject/Help/*.HxC 183 | DocProject/Help/*.hhc 184 | DocProject/Help/*.hhk 185 | DocProject/Help/*.hhp 186 | DocProject/Help/Html2 187 | DocProject/Help/html 188 | 189 | # Click-Once directory 190 | publish/ 191 | 192 | # Publish Web Output 193 | *.[Pp]ublish.xml 194 | *.azurePubxml 195 | # TODO: Comment the next line if you want to checkin your web deploy settings 196 | # but database connection strings (with potential passwords) will be unencrypted 197 | *.pubxml 198 | *.publishproj 199 | 200 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 201 | # checkin your Azure Web App publish settings, but sensitive information contained 202 | # in these scripts will be unencrypted 203 | PublishScripts/ 204 | 205 | # NuGet Packages 206 | *.nupkg 207 | # The packages folder can be ignored because of Package Restore 208 | **/packages/* 209 | # except build/, which is used as an MSBuild target. 210 | !**/packages/build/ 211 | # Uncomment if necessary however generally it will be regenerated when needed 212 | #!**/packages/repositories.config 213 | # NuGet v3's project.json files produces more ignoreable files 214 | *.nuget.props 215 | *.nuget.targets 216 | 217 | # Microsoft Azure Build Output 218 | csx/ 219 | *.build.csdef 220 | 221 | # Microsoft Azure Emulator 222 | ecf/ 223 | rcf/ 224 | 225 | # Windows Store app package directories and files 226 | AppPackages/ 227 | BundleArtifacts/ 228 | Package.StoreAssociation.xml 229 | _pkginfo.txt 230 | 231 | # Visual Studio cache files 232 | # files ending in .cache can be ignored 233 | *.[Cc]ache 234 | # but keep track of directories ending in .cache 235 | !*.[Cc]ache/ 236 | 237 | # Others 238 | ClientBin/ 239 | ~$* 240 | *~ 241 | *.dbmdl 242 | *.dbproj.schemaview 243 | *.pfx 244 | *.publishsettings 245 | node_modules/ 246 | orleans.codegen.cs 247 | 248 | # Since there are multiple workflows, uncomment next line to ignore bower_components 249 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 250 | #bower_components/ 251 | 252 | # RIA/Silverlight projects 253 | Generated_Code/ 254 | 255 | # Backup & report files from converting an old project file 256 | # to a newer Visual Studio version. Backup files are not needed, 257 | # because we have git ;-) 258 | _UpgradeReport_Files/ 259 | Backup*/ 260 | UpgradeLog*.XML 261 | UpgradeLog*.htm 262 | 263 | # SQL Server files 264 | *.mdf 265 | *.ldf 266 | 267 | # Business Intelligence projects 268 | *.rdl.data 269 | *.bim.layout 270 | *.bim_*.settings 271 | 272 | # Microsoft Fakes 273 | FakesAssemblies/ 274 | 275 | # GhostDoc plugin setting file 276 | *.GhostDoc.xml 277 | 278 | # Node.js Tools for Visual Studio 279 | .ntvs_analysis.dat 280 | 281 | # Visual Studio 6 build log 282 | *.plg 283 | 284 | # Visual Studio 6 workspace options file 285 | *.opt 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # JetBrains Rider 303 | .idea/ 304 | *.sln.iml 305 | 306 | # VS Code folder 307 | .vscode/ 308 | 309 | # Databricks file 310 | *.pyi 311 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | --------------------------------------------------------------------------------