├── .gitignore ├── AccessModifiers ├── AccessModifiers.csproj └── Program.cs ├── AccessModifiersOtherAssembly ├── AccessModifiersOtherAssembly.csproj └── Program.cs ├── Adapter ├── Adapter.csproj └── Program.cs ├── BoxingAndUnboxing ├── BoxingAndUnboxing.csproj └── Program.cs ├── Bridge ├── Bridge.csproj └── Program.cs ├── Builder ├── Builder.csproj ├── Car.cs ├── Pet.cs └── Program.cs ├── ClassVsStruct ├── ClassVsStruct.csproj └── Program.cs ├── CommonIntermediateLanguage ├── CommonIntermediateLanguage.csproj └── Program.cs ├── ConstAndReadonly ├── ConstAndReadonly.csproj └── Program.cs ├── DeepCopyVsShallowCopy ├── DeepCopyVsShallowCopy.csproj └── Program.cs ├── DefaultAccessModifiers ├── DefaultAccessModifiers.csproj └── Program.cs ├── DependencyInversionPrinciple ├── DependencyInversionPrinciple.csproj └── Program.cs ├── DontRepeatYourself ├── DontRepeatYourself.csproj └── Program.cs ├── DotNet50JuniorInterviewQuestions.sln ├── Encapsulation ├── Encapsulation.csproj └── Program.cs ├── EqualityOperatorVsEquals ├── EqualityOperatorVsEquals.csproj └── Program.cs ├── ExceptionsHandling ├── ExceptionsHandling.csproj └── Program.cs ├── ExtensionMethods ├── ExtensionMethods.csproj └── Program.cs ├── FactoryMethod ├── FactoryMethod.csproj └── Program.cs ├── GarbageCollectorMemoryLeak ├── GarbageCollectorMemoryLeak.csproj └── Program.cs ├── Generics ├── Generics.csproj └── Program.cs ├── Goto ├── DatabaseMock.cs ├── Goto.csproj ├── IDatabase.cs ├── ILogger.cs ├── Person.cs ├── PersonalDataFormatter.cs ├── Pet.cs ├── Program.cs └── VeryUglyPersonalDataFormatter.cs ├── IEnumerable ├── IEnumerable.csproj └── Program.cs ├── InterfaceSegregationPrinciple ├── InterfaceSegregationPrinciple.csproj └── Program.cs ├── InterfaceVsAbstractClass ├── InterfaceVsAbstractClass.csproj └── Program.cs ├── LINQ ├── LINQ.csproj ├── Person.cs └── Program.cs ├── LiskovsSubstitutionPrinciple ├── AbuseOfInterfaceImplementationBySubclass.cs ├── LiskovSubstitutionPrinciple.csproj ├── PreconditionStrengthenedBySubtype.cs ├── Program.cs └── RuntimeTypeSwitching.cs ├── MagicNumber ├── MagicNumber.csproj └── Program.cs ├── MethodOverloading ├── MethodOverloading.csproj └── Program.cs ├── MethodOverridingVsMethodHiding ├── MethodOverridingVsMethodHiding.csproj ├── Program.cs └── Shapes.cs ├── MultipleInheritance ├── MultipleInheritance.csproj └── Program.cs ├── NewKeyword ├── NewKeyword.csproj ├── NewOperator.cs └── Program.cs ├── NullOperators ├── NullOperators.csproj └── Program.cs ├── Nullable ├── Nullable.csproj └── Program.cs ├── OpenClosedPrinciple ├── OpenClosedPrinciple.csproj ├── Program.cs └── Shapes.cs ├── Params ├── Params.csproj └── Program.cs ├── PartialClasses ├── DuckPartOne.cs ├── DuckPartTwo.cs ├── PartialClasses.csproj └── Program.cs ├── Polymorphism ├── Polymorphism.csproj └── Program.cs ├── Properties ├── Program.cs └── Properties.csproj ├── README.md ├── RefAndOut ├── Program.cs └── RefAndOut.csproj ├── SealedModifier ├── Program.cs └── SealedModifier.csproj ├── SingleResponsibilityPrinciple ├── PeopleInformationPrinter.cs ├── PeopleInformationPrinterBreakingSRP.cs ├── Person.cs ├── Program.cs └── SingleResponsibilityPrinciple.csproj ├── Singleton ├── Database.cs ├── ILogger.cs ├── InterfaceHandler.cs ├── Logger.cs ├── NetworkConnector.cs ├── Program.cs └── Singleton.csproj ├── SpaghettiCode ├── ConsoleReader.cs ├── IQuadraticFunctionRootsCalculator.cs ├── MathUtilities.cs ├── Program.cs ├── QuadraticFunctionRoots.cs ├── QuadraticFunctionRootsCalculator.cs ├── SpaghettiCode.csproj └── SpaghettiQuadraticFunctionRootsCalculator.cs ├── StaticClass ├── Program.cs └── StaticClass.csproj ├── StaticKeyword ├── Geometry.cs ├── Program.cs └── StaticKeyword.csproj ├── TernaryOperator ├── Program.cs └── TernaryOperator.csproj ├── TypesOfErrors ├── Program.cs └── TypesOfErrors.csproj ├── ValueTypesAndReferenceTypes ├── Program.cs └── ValueTypesAndReferenceTypes.csproj └── VirtualAndAbstractMethods ├── Program.cs └── VirtualAndAbstractMethods.csproj /.gitignore: -------------------------------------------------------------------------------- 1 | [Oo]bj 2 | [Bb]in 3 | *.user 4 | *.suo 5 | *.[Cc]ache 6 | *.bak 7 | *.ncb 8 | *.log 9 | *.v2 10 | *.vs 11 | *.DS_Store 12 | [Tt]humbs.db 13 | _ReSharper.* 14 | *.resharper 15 | Ankh.NoLoad -------------------------------------------------------------------------------- /AccessModifiers/AccessModifiers.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AccessModifiers/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AccessModifiers 4 | { 5 | public class TestClass 6 | { 7 | public string PublicField = "public"; //accessible in any class in any assembly 8 | internal string InternalField = "internal"; //accessible in any class in this assembly 9 | protected string ProtectedField = "protected"; // accessible only in classes derived from this class 10 | protected internal string ProtectedInternalField = "protected internal"; // accessible from any class in this assembly, OR from derived classes in other assemblies 11 | private protected string PrivateProtectedField = "private protected"; // in this assembly can only be accessed from classes derived from this class. Not accessible in another assemblies at all. 12 | private string PrivateField = "private"; // not accessible in any other class 13 | 14 | } 15 | 16 | class ChildOfTestClassInTheSameAssembly : TestClass 17 | { 18 | public ChildOfTestClassInTheSameAssembly() 19 | { 20 | Console.WriteLine(base.PublicField); 21 | Console.WriteLine(base.InternalField); 22 | Console.WriteLine(base.ProtectedField); 23 | Console.WriteLine(base.ProtectedInternalField); 24 | Console.WriteLine(base.PrivateProtectedField); 25 | //Console.WriteLine(base.PrivateField); //only accessible in the TestClass 26 | } 27 | } 28 | 29 | class Program 30 | { 31 | static void Main(string[] args) 32 | { 33 | var testClassInstance = new TestClass(); 34 | 35 | Console.WriteLine(testClassInstance.PublicField); 36 | Console.WriteLine(testClassInstance.InternalField); 37 | //Console.WriteLine(testClassInstance.ProtectedField); //not accessible here because this class is not derived from the TestClass 38 | Console.WriteLine(testClassInstance.ProtectedInternalField); 39 | //Console.WriteLine(testClassInstance.PrivateProtectedField); //not accessible here because this class is not derived from the TestClass 40 | //Console.WriteLine(testClassInstance.PrivateField); //only accessible in the TestClass 41 | 42 | Console.ReadKey(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /AccessModifiersOtherAssembly/AccessModifiersOtherAssembly.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /AccessModifiersOtherAssembly/Program.cs: -------------------------------------------------------------------------------- 1 | using AccessModifiers; 2 | using System; 3 | 4 | namespace AccessModifiersOtherAssembly 5 | { 6 | class ChildOfTestClassInOtherAssembly : TestClass 7 | { 8 | public ChildOfTestClassInOtherAssembly() 9 | { 10 | Console.WriteLine(base.PublicField); 11 | //Console.WriteLine(base.InternalField); //not accessible in this assembly 12 | Console.WriteLine(base.ProtectedField); 13 | Console.WriteLine(base.ProtectedInternalField); 14 | //Console.WriteLine(base.PrivateProtectedField); //only accessible in classes derived from the TestClass and in the same assembly as the TestClass 15 | //Console.WriteLine(base.PrivateField); //only accessible in the TestClass 16 | } 17 | } 18 | 19 | class Program 20 | { 21 | static void Main(string[] args) 22 | { 23 | var testClassInstance = new TestClass(); 24 | Console.WriteLine(testClassInstance.PublicField); 25 | //Console.WriteLine(testClassInstance.InternalField); //not accessible in this assembly 26 | //Console.WriteLine(testClassInstance.ProtectedField); //not accessible here because this class is not derived from the TestClass 27 | //Console.WriteLine(testClassInstance.ProtectedInternalField); //not accessible here because this class is not derived from the TestClass and it is in another assembly 28 | //Console.WriteLine(testClassInstance.PrivateProtectedField); //not accessible here because this class is not derived from the TestClass 29 | //Console.WriteLine(testClassInstance.PrivateField); //only accessible in the TestClass 30 | 31 | 32 | Console.ReadKey(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Adapter/Adapter.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Adapter/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Adapter 6 | { 7 | public class Hotel 8 | { 9 | public string Name { get; } 10 | 11 | public Hotel(string name) 12 | { 13 | Name = name; 14 | } 15 | } 16 | 17 | public interface IHotelsByCityFinder 18 | { 19 | IEnumerable FindByCity(string city); 20 | } 21 | 22 | public interface IHotelsByZipCodeFinder 23 | { 24 | IEnumerable FindByZipCode(string zipCode); 25 | } 26 | 27 | public class HotelsByZipCodeFinder : IHotelsByZipCodeFinder 28 | { 29 | public IEnumerable FindByZipCode(string zipCode) 30 | { 31 | switch(zipCode) 32 | { 33 | case "E1 6AN": 34 | return new[] { new Hotel("Imperial Hotel"), new Hotel("Golden Duck Hotel") }; 35 | case "E1 7AA": 36 | return new[] { new Hotel("Ambassador Hotel") }; 37 | default: 38 | return Enumerable.Empty(); 39 | } 40 | } 41 | } 42 | 43 | public class HotelsByCityFinderAdapter : IHotelsByCityFinder 44 | { 45 | private readonly IHotelsByZipCodeFinder _hotelsByZipCodeFinder; 46 | 47 | public HotelsByCityFinderAdapter(IHotelsByZipCodeFinder hotelsByZipCodeFinder) 48 | { 49 | _hotelsByZipCodeFinder = hotelsByZipCodeFinder; 50 | } 51 | 52 | public IEnumerable FindByCity(string city) 53 | { 54 | var zipCodes = GetZipCodesForCity(city); 55 | return zipCodes.SelectMany(zipCode => _hotelsByZipCodeFinder.FindByZipCode(zipCode)); 56 | } 57 | 58 | private IEnumerable GetZipCodesForCity(string city) 59 | { 60 | if(city == "London") 61 | { 62 | return new[] { "E1 6AN", "E1 7AA" }; 63 | } 64 | throw new Exception("Unknown city"); 65 | } 66 | } 67 | 68 | class Program 69 | { 70 | static void Main(string[] args) 71 | { 72 | IHotelsByCityFinder hotelsFinder = new HotelsByCityFinderAdapter(new HotelsByZipCodeFinder()); 73 | var hotelsInLondon = hotelsFinder.FindByCity("London"); 74 | Console.WriteLine($"Hotels in London: {string.Join(", ", hotelsInLondon.Select(h => h.Name))}"); 75 | 76 | Console.ReadKey(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /BoxingAndUnboxing/BoxingAndUnboxing.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /BoxingAndUnboxing/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BoxingAndUnboxing 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | //no boxing here, because string is not a value type 10 | string word = "abc"; 11 | object obj = word; 12 | 13 | //boxing and unboxing 14 | int number = 5; 15 | object boxedNumber = number; 16 | int unboxedNumber = (int)boxedNumber; 17 | 18 | //this will throw because unboxing requires exact type match 19 | short shortNumber = 3; 20 | object boxedShortNumber = shortNumber; 21 | //int unboxedShortNumber = (int)boxedShortNumber; 22 | 23 | //this will work fine - no boxing or unboxing here, 24 | //so short is converted to int without problem 25 | short otherShortNumber = 3; 26 | int otherShortNumberCastToInt = (int)otherShortNumber; 27 | 28 | Console.ReadKey(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Bridge/Bridge.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Bridge/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Bridge 4 | { 5 | class Car 6 | { 7 | public Motor Motor { get; } 8 | public Gear Gear { get; } 9 | 10 | public Car(Motor motor, Gear gear) 11 | { 12 | Motor = motor; 13 | Gear = gear; 14 | } 15 | } 16 | 17 | class Pickup : Car 18 | { 19 | public Pickup(Motor motor, Gear gear) : base(motor, gear) 20 | { 21 | } 22 | } 23 | 24 | class Sedan : Car 25 | { 26 | public Sedan(Motor motor, Gear gear) : base(motor, gear) 27 | { 28 | } 29 | } 30 | 31 | class Motor { } 32 | class ElectricMotor : Motor { } 33 | class PetrolMotor : Motor { } 34 | 35 | class Gear { } 36 | class ManualGear : Gear { } 37 | class AutomaticGear : Gear { } 38 | 39 | 40 | class Program 41 | { 42 | static void Main(string[] args) 43 | { 44 | var electricPickupWithManualGear = new Pickup(new ElectricMotor(), new ManualGear()); 45 | var petrolSedanWithAutomaticGear = new Sedan(new PetrolMotor(), new AutomaticGear()); 46 | 47 | Console.WriteLine("Hello!"); 48 | Console.ReadKey(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Builder/Builder.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Builder/Car.cs: -------------------------------------------------------------------------------- 1 | namespace Builder 2 | { 3 | record Car 4 | { 5 | public string Brand { get; set; } 6 | public string Color { get; set; } 7 | } 8 | } -------------------------------------------------------------------------------- /Builder/Pet.cs: -------------------------------------------------------------------------------- 1 | namespace Builder 2 | { 3 | class Pet 4 | { 5 | //public Pet() //only for object initializer 6 | //{ 7 | 8 | //} 9 | 10 | public Pet(string type, string officialName, string nickname, string motherName, string fatherName, string breedingCompanyName) 11 | { 12 | Type = type; 13 | OfficialName = officialName; 14 | Nickname = nickname; 15 | MotherName = motherName; 16 | FatherName = fatherName; 17 | BreedingCompanyName = breedingCompanyName; 18 | } 19 | 20 | public string Type { get; } 21 | public string OfficialName { get; } 22 | public string Nickname { get; } 23 | public string MotherName { get; } 24 | public string FatherName { get; } 25 | public string BreedingCompanyName { get; } 26 | 27 | public class Builder 28 | { 29 | private string _type; 30 | private string _officialName; 31 | private string _nickname; 32 | private string _motherName; 33 | private string _fatherName; 34 | private string _breedingCompanyName; 35 | 36 | public Builder WithType(string type) 37 | { 38 | _type = type; 39 | return this; 40 | } 41 | 42 | public Builder WithOfficialName(string officialName) 43 | { 44 | _officialName = officialName; 45 | return this; 46 | } 47 | 48 | public Builder WithNickname(string nickname) 49 | { 50 | _nickname = nickname; 51 | return this; 52 | } 53 | 54 | public Builder WithMotherName(string motherName) 55 | { 56 | _motherName = motherName; 57 | return this; 58 | } 59 | 60 | public Builder WithFatherName(string fatherName) 61 | { 62 | _fatherName = fatherName; 63 | return this; 64 | } 65 | 66 | public Builder WithBreedingCompanyName(string breedingCompanyName) 67 | { 68 | _breedingCompanyName = breedingCompanyName; 69 | return this; 70 | } 71 | 72 | public Pet Build() 73 | { 74 | return new Pet(_type, _officialName, _nickname, _motherName, _fatherName, _breedingCompanyName); 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /Builder/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Builder 4 | { 5 | public class Person 6 | { 7 | private string Name { get; } 8 | private string LastName { get; } 9 | private int YearOfBirth { get; } 10 | private int Age { get; } 11 | 12 | private Person(string name, int yearOfBirth, int age) 13 | { 14 | Name = name; 15 | YearOfBirth = yearOfBirth; 16 | Age = age; 17 | } 18 | 19 | public class Builder 20 | { 21 | private string _name; 22 | private int _yearOfBirth; 23 | private int _age; 24 | 25 | public Builder WithName(string name) 26 | { 27 | _name = name; 28 | return this; 29 | } 30 | 31 | public Builder WithYearOfBirth(int yearOfBirth) 32 | { 33 | if (yearOfBirth > DateTime.Now.Year || yearOfBirth < 1900) 34 | { 35 | throw new Exception("Invalid year of birth"); 36 | } 37 | _age = DateTime.Now.Year - yearOfBirth; 38 | _yearOfBirth = yearOfBirth; 39 | return this; 40 | } 41 | 42 | public Person Build() 43 | { 44 | return new Person(_name, _yearOfBirth, _age); 45 | } 46 | } 47 | } 48 | 49 | class Program 50 | { 51 | static void Main(string[] args) 52 | { 53 | var builder = new Person.Builder(); 54 | var name = ReadName(); 55 | builder = builder.WithName(name); 56 | //some other operations 57 | var year = ReadYear(); 58 | builder = builder.WithYearOfBirth(year); 59 | //some other operations 60 | var person = builder.Build(); 61 | 62 | 63 | var dog1 = new Pet("Dog", "Louis Charles Bryan the Third", "Rex", "Tina", "Lucky", "Happy Paws"); 64 | var dog2 = new Pet.Builder() 65 | .WithType("Dog") 66 | .WithNickname("Rex") 67 | .WithOfficialName("Louis Charles Bryan the Third") 68 | .WithFatherName("Lucky") 69 | .WithMotherName("Tina") 70 | .WithBreedingCompanyName("Happy Paws"); 71 | 72 | //var dog3 = new Pet //that requies adding public setters 73 | //{ 74 | // Type = "Dog", 75 | // OfficialName = "Louis Charles Bryan the Third", 76 | // Nickname = "Rex", 77 | // MotherName = "Tina", 78 | // FatherName = "Lucky", 79 | // BreedingCompanyName = "Happy Paws" 80 | //}; 81 | 82 | var invalidDog = new Pet.Builder().Build(); //no properties are set 83 | 84 | var catBuilder = new Pet.Builder() 85 | .WithType("Cat") 86 | .WithNickname("Leon"); 87 | 88 | //some logic here... 89 | catBuilder = catBuilder.WithType("Dog"); //we override the original Type 90 | var cat = catBuilder.Build(); 91 | 92 | var car = new Car() { Brand = "Mazda", Color = "Red" }; 93 | var carAfterPainting = car with { Color = "Blue" }; //please note that Car is a record, not a class 94 | 95 | Console.ReadKey(); 96 | } 97 | 98 | private static string ReadName() 99 | { 100 | //imagine this is some complicated process, like reading from a remote database 101 | return "Alex"; 102 | } 103 | 104 | private static int ReadYear() 105 | { 106 | //imagine this is some complicated process, like reading from a remote database 107 | return 1980; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /ClassVsStruct/ClassVsStruct.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ClassVsStruct/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | var point = new Point(); //parameterless constructor is always present for structs 4 | 5 | Console.ReadKey(); 6 | 7 | public struct Point 8 | { 9 | public int X; 10 | public int Y; 11 | 12 | public Point(int x, int y) 13 | { 14 | X = x; 15 | Y = y; 16 | } 17 | 18 | //this will not work - structs can't have finalizers 19 | //~Point() 20 | //{ 21 | 22 | //} 23 | 24 | // //before C# 10 this would not compile - struct couldn't have 25 | // //explicit parameterless constuctor 26 | // public Point() 27 | // { 28 | 29 | // } 30 | 31 | // //before C# 10 this would not compile - all fields must be 32 | // //assigned in the constructor 33 | // public Point(int x) 34 | // { 35 | 36 | // } 37 | } 38 | 39 | //This will not work - structs do not support inheritance 40 | //public struct SpecialPoint : Point 41 | //{ 42 | 43 | //} 44 | 45 | //this works fine - structs can implement interfaces 46 | 47 | public struct SpecialPoint : IComparable 48 | { 49 | public int CompareTo(SpecialPoint other) 50 | { 51 | throw new NotImplementedException(); 52 | } 53 | } 54 | 55 | -------------------------------------------------------------------------------- /CommonIntermediateLanguage/CommonIntermediateLanguage.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /CommonIntermediateLanguage/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CommonIntermediateLanguage 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine("Hello! What's your name?"); 10 | var name = Console.ReadLine(); 11 | 12 | Console.WriteLine($"Nice to meet you, {name}. How old are you?"); 13 | 14 | var ageAsText = Console.ReadLine(); 15 | 16 | if(int.TryParse(ageAsText, out int age)) 17 | { 18 | Console.WriteLine($"That young, only {age}?"); 19 | } 20 | else 21 | { 22 | Console.WriteLine("Sorry, I didn't get that."); 23 | } 24 | Console.WriteLine("Well, it was nice to meet you! Bye, bye!"); 25 | Console.ReadKey(); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /ConstAndReadonly/ConstAndReadonly.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ConstAndReadonly/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ConstAndReadonly 4 | { 5 | class Program 6 | { 7 | //must be assigned at declaration 8 | // private const int ConstNumber; 9 | 10 | private const int OtherConstNumber = 4; 11 | 12 | //must be compile-time constant 13 | // private const int ConstCurrentYear = DateTime.Now.Year; 14 | 15 | //must be compile-time constant 16 | //private const Person ConstPerson = new Person("John", "Smith", 1980); 17 | 18 | // private static const int StaticConst = 6; //const can't be declared static - it is implicitely static 19 | 20 | private readonly int ReadonlyCurrentYear = DateTime.Now.Year; //readonly does not need to be compile-time constant 21 | private readonly Person ReadonlyPerson = new Person("John", "Smith", 1980); 22 | private static readonly int StaticReadonly = 6; 23 | 24 | private readonly int ReadonlyNumber; 25 | 26 | //it is fine to assign readonly number at declaration 27 | private readonly int OtherReadonlyNumber = 4; 28 | 29 | public Program() //constructor 30 | { 31 | //this will not compile because we can only assign consts at declaration 32 | //ConstNumber = 5; 33 | 34 | //it is fine to assign readonly value in constructor 35 | ReadonlyNumber = 10; 36 | 37 | //we can also assign a value even if we already did it at declaration 38 | OtherReadonlyNumber = 12; 39 | } 40 | 41 | static void Main(string[] args) 42 | { 43 | const float PI = 3.14f; 44 | const int DaysInWeek = 7; 45 | const int MaxSizeOfAnArray = 20; //assuming this is the designed limitation 46 | const int BitsInByte = 8; 47 | 48 | // ReadonlyNumber = 20; //this will not compile as we can only assign readonly number 49 | //at declaration or in constructor 50 | } 51 | } 52 | 53 | class Person 54 | { 55 | public string Name; 56 | public string LastName; 57 | public int YearOfBirth; 58 | 59 | public Person(string name, string lastName, int yearOfBirth) 60 | { 61 | Name = name; 62 | LastName = lastName; 63 | YearOfBirth = yearOfBirth; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /DeepCopyVsShallowCopy/DeepCopyVsShallowCopy.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DeepCopyVsShallowCopy/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DeepCopyVsShallowCopy 4 | { 5 | class Pet 6 | { 7 | public string Name; 8 | public int Age; 9 | 10 | public Pet(string name, int age) 11 | { 12 | Name = name; 13 | Age = age; 14 | } 15 | } 16 | 17 | class Person 18 | { 19 | public string Name; 20 | public int Height; 21 | public Pet Pet; 22 | 23 | public Person(string name, int height, Pet pet) 24 | { 25 | Name = name; 26 | Height = height; 27 | Pet = pet; 28 | } 29 | 30 | public Person ShallowCopy() 31 | { 32 | return (Person)MemberwiseClone(); 33 | } 34 | 35 | public Person DeepCopy() 36 | { 37 | return new Person(Name, Height, new Pet(Pet.Name, Pet.Age)); 38 | } 39 | 40 | //MemberwiseClone does something like this 41 | //public object MemberwiseClone() 42 | //{ 43 | // return new Person 44 | // { 45 | // Name = this.Name, 46 | // Height = this.Height, 47 | // Pet = this.Pet, 48 | // }; 49 | //} 50 | } 51 | 52 | class Program 53 | { 54 | static void Main(string[] args) 55 | { 56 | var john = new Person("John", 175, new Pet("Lucky", 5)); 57 | var johnShallowCopy = john.ShallowCopy(); 58 | johnShallowCopy.Pet.Age = 10; 59 | Console.WriteLine($"John's pet age: {john.Pet.Age}"); 60 | Console.WriteLine($"John's shallow copy's pet age: {johnShallowCopy.Pet.Age}\n"); 61 | 62 | johnShallowCopy.Height = 150; 63 | Console.WriteLine($"John's height: {john.Height}"); 64 | Console.WriteLine($"John's shallow copy's height: {johnShallowCopy.Height}\n"); 65 | 66 | var mary = new Person("Mary", 165, new Pet("Tiger", 7)); 67 | var maryDeepCopy = mary.DeepCopy(); 68 | maryDeepCopy.Pet.Age = 11; 69 | Console.WriteLine($"Mary's pet age: {mary.Pet.Age}"); 70 | Console.WriteLine($"Mary's shallow copy's pet age: {maryDeepCopy.Pet.Age}\n"); 71 | 72 | var person = new Person("Alex", 183, null); 73 | var person2 = person; 74 | person2.Height = 160; 75 | Console.WriteLine($"person's height: {person.Height}"); 76 | Console.WriteLine($"person2's height: {person2.Height}\n"); 77 | 78 | Console.ReadKey(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /DefaultAccessModifiers/DefaultAccessModifiers.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DefaultAccessModifiers/Program.cs: -------------------------------------------------------------------------------- 1 | namespace DefaultAccessModifiers 2 | { 3 | //at namespace level we can only use internal or public 4 | //private class PrivateClassAtNamespaceLevel 5 | //{ 6 | //} 7 | 8 | //this class is internal by default 9 | class ClassAtNamespaceLevel 10 | { 11 | //this field is private by default 12 | int number; 13 | 14 | //all access modifiers other than private are non-default 15 | public int publicNumber; 16 | 17 | 18 | //this class is private by default, 19 | //because it is declared at class level 20 | class InnerClass 21 | { 22 | 23 | } 24 | } 25 | 26 | public class PublicClassAtNamespaceLevel //public is non-default access modifier 27 | { 28 | } 29 | 30 | class Program 31 | { 32 | static void Main(string[] args) 33 | { 34 | System.Console.WriteLine("Hello!"); 35 | System.Console.ReadKey(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DependencyInversionPrinciple/DependencyInversionPrinciple.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /DependencyInversionPrinciple/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DependencyInversionPrinciple 4 | { 5 | class YourStore 6 | { 7 | private readonly IDelivery _delivery; 8 | 9 | public YourStore(IDelivery delivery) { _delivery = delivery; } 10 | 11 | public void SellItem(string item, string address) 12 | { 13 | var package = PerparePackage(item, address); 14 | _delivery.DeliverPackage(package); 15 | } 16 | 17 | private string PerparePackage(string item, string address) 18 | { 19 | return "package ready to be shipped"; 20 | } 21 | } 22 | 23 | public interface IDelivery 24 | { 25 | void DeliverPackage(string package); 26 | } 27 | 28 | internal class FastWheelsDelivery : IDelivery 29 | { 30 | public void DeliverPackage(string package) 31 | { 32 | //delivering with a bike 33 | } 34 | } 35 | 36 | internal class HeavyCargoDelivery : IDelivery 37 | { 38 | public void DeliverPackage(string package) 39 | { 40 | //delivering accross the country even the heaviest washing machines! 41 | } 42 | } 43 | 44 | public class Program 45 | { 46 | static void Main(string[] args) 47 | { 48 | Console.WriteLine("Hello!"); 49 | Console.ReadKey(); 50 | } 51 | } 52 | } 53 | 54 | 55 | -------------------------------------------------------------------------------- /DontRepeatYourself/DontRepeatYourself.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DontRepeatYourself/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DontRepeatYourself 4 | { 5 | class Order 6 | { 7 | public string CustomerId { get; } 8 | public string ProductId { get; } 9 | 10 | public Order(string customerId, string productId) 11 | { 12 | CustomerId = customerId; 13 | ProductId = productId; 14 | } 15 | } 16 | 17 | class OnlineStore 18 | { 19 | //below code breaks the DRY principle as 30-days policy is defined in two places 20 | //but it doesn't contain repeated code 21 | //public DateTime ReturnDateDeadline(DateTime purchaseDate) 22 | //{ 23 | // return purchaseDate.AddDays(30); 24 | //} 25 | 26 | //public bool IsAfterPossibleReturnDate(DateTime purchaseDate) 27 | //{ 28 | // return (DateTime.Now - purchaseDate).TotalDays > 30; 29 | //} 30 | 31 | 32 | //the below are the same by coincidence - we should NOT have one constant for it 33 | public const int DaysForReturn = 30; 34 | public const int DaysForRefund = 30; 35 | 36 | public DateTime ReturnDateDeadline(DateTime purchaseDate) 37 | { 38 | return purchaseDate.AddDays(DaysForReturn); 39 | } 40 | 41 | public bool IsAfterPossibleReturnDate(DateTime purchaseDate) 42 | { 43 | return IsBeforeNow(ReturnDateDeadline(purchaseDate)); 44 | } 45 | 46 | public DateTime RefundDateDeadline(DateTime purchaseDate) 47 | { 48 | return purchaseDate.AddDays(DaysForRefund); 49 | } 50 | 51 | public bool IsAfterPossibleRefundDate(DateTime purchaseDate) 52 | { 53 | return IsBeforeNow(RefundDateDeadline(purchaseDate)); 54 | } 55 | 56 | private bool IsBeforeNow(DateTime dateTime) 57 | { 58 | return dateTime < DateTime.Now; 59 | } 60 | 61 | //below code doesn't break the DRY principle - business requirement is only defined once 62 | //but it does contain repeated code 63 | 64 | //public void CommitOrder(Order order) 65 | //{ 66 | // if(string.IsNullOrEmpty(order.CustomerId)) 67 | // { 68 | // throw new Exception($"The CustomerId must not be empty"); 69 | // } 70 | // if (string.IsNullOrEmpty(order.ProductId)) 71 | // { 72 | // throw new Exception($"The ProductId must not be empty"); 73 | // } 74 | 75 | // //saving to database here... 76 | // Console.WriteLine("Order committed and saved to database"); 77 | //} 78 | 79 | public void CommitOrder(Order order) 80 | { 81 | Validate(order.CustomerId, nameof(order.CustomerId)); 82 | Validate(order.ProductId, nameof(order.ProductId)); 83 | 84 | //saving to database here... 85 | Console.WriteLine("Order committed and saved to database"); 86 | } 87 | 88 | private void Validate(string idToBeValidated, string propertyName) 89 | { 90 | if (string.IsNullOrEmpty(idToBeValidated)) 91 | { 92 | throw new Exception($"The {propertyName} must not be empty"); 93 | } 94 | } 95 | } 96 | 97 | class Program 98 | { 99 | static void Main(string[] args) 100 | { 101 | var onlineStore = new OnlineStore(); 102 | 103 | var purchaseDate = new DateTime(2021, 6, 21); 104 | Console.WriteLine($"Purchase date: {purchaseDate}"); 105 | Console.WriteLine($"Return deadline: {onlineStore.ReturnDateDeadline(purchaseDate)}"); 106 | Console.WriteLine($"Is after return deadline: {onlineStore.IsAfterPossibleReturnDate(purchaseDate)}\n"); 107 | 108 | var olderPurchaseDate = new DateTime(2020, 6, 21); 109 | Console.WriteLine($"Purchase date: {olderPurchaseDate}"); 110 | Console.WriteLine($"Return deadline: {onlineStore.ReturnDateDeadline(olderPurchaseDate)}"); 111 | Console.WriteLine($"Is after return deadline: {onlineStore.IsAfterPossibleReturnDate(olderPurchaseDate)}\n"); 112 | 113 | //the below will thrown an exception on validation 114 | //onlineStore.CommitOrder(new Order(null, null)); 115 | 116 | Console.ReadKey(); 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /DotNet50JuniorInterviewQuestions.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31321.278 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StaticKeyword", "StaticKeyword\StaticKeyword.csproj", "{1F659576-A6A2-4960-A1F8-6822D2077591}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NullOperators", "NullOperators\NullOperators.csproj", "{BA3F18A6-D238-411E-86ED-0E263861F955}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StaticClass", "StaticClass\StaticClass.csproj", "{675051D6-0A1F-4EF0-9AB7-CFE7D1F33CFF}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConstAndReadonly", "ConstAndReadonly\ConstAndReadonly.csproj", "{2CF84B13-B115-4722-AA63-99405CE0DE83}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RefAndOut", "RefAndOut\RefAndOut.csproj", "{E02B2809-5F95-4EC2-BDF5-DF2DA1C5E092}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Nullable", "Nullable\Nullable.csproj", "{E0A08682-9AE8-4E03-9BAE-F20C45FC0537}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TernaryOperator", "TernaryOperator\TernaryOperator.csproj", "{14628827-7D3B-49C7-9784-F2E391F58B19}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Params", "Params\Params.csproj", "{D272FD9F-2DE2-44BD-AE22-CB5620102650}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewKeyword", "NewKeyword\NewKeyword.csproj", "{7EFA190C-3C3E-4549-8771-FFC37BCD4C6A}" 23 | EndProject 24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SealedModifier", "SealedModifier\SealedModifier.csproj", "{81D743F6-B69A-49CD-BAA2-CED4388E61A4}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ValueTypesAndReferenceTypes", "ValueTypesAndReferenceTypes\ValueTypesAndReferenceTypes.csproj", "{2B6B37C6-9319-4AD0-9041-4A10CDB88750}" 27 | EndProject 28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClassVsStruct", "ClassVsStruct\ClassVsStruct.csproj", "{E500B87F-C3A7-4883-964D-938E4DD88B0C}" 29 | EndProject 30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExceptionsHandling", "ExceptionsHandling\ExceptionsHandling.csproj", "{4220BB37-2125-4264-AB5D-2DC90B4580D1}" 31 | EndProject 32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GarbageCollectorMemoryLeak", "GarbageCollectorMemoryLeak\GarbageCollectorMemoryLeak.csproj", "{92C335AF-A5B2-43E1-8FF0-16DB751CEE09}" 33 | EndProject 34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SingleResponsibilityPrinciple", "SingleResponsibilityPrinciple\SingleResponsibilityPrinciple.csproj", "{6EF2B4E3-6080-436D-918E-228E3C5FCB10}" 35 | EndProject 36 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LiskovSubstitutionPrinciple", "LiskovsSubstitutionPrinciple\LiskovSubstitutionPrinciple.csproj", "{EF1E4468-7499-41C7-A7F1-ED16D5EF03F9}" 37 | EndProject 38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MethodOverloading", "MethodOverloading\MethodOverloading.csproj", "{2B0B2400-6A24-4A48-A7F9-0FAB22B619E0}" 39 | EndProject 40 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MethodOverridingVsMethodHiding", "MethodOverridingVsMethodHiding\MethodOverridingVsMethodHiding.csproj", "{3C360BC1-FCF4-4CDF-96E3-11816166588D}" 41 | EndProject 42 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtualAndAbstractMethods", "VirtualAndAbstractMethods\VirtualAndAbstractMethods.csproj", "{9699711D-244A-48E7-ADE5-749792C7BD25}" 43 | EndProject 44 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generics", "Generics\Generics.csproj", "{6C31B635-E204-4C05-9D8D-90EC8818168E}" 45 | EndProject 46 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInversionPrinciple", "DependencyInversionPrinciple\DependencyInversionPrinciple.csproj", "{D65EC816-9DF3-485F-BDBB-DE8FB4B298A5}" 47 | EndProject 48 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LINQ", "LINQ\LINQ.csproj", "{04D04F22-8BE0-4C4F-BD3B-4ACB6B58A4FD}" 49 | EndProject 50 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Properties", "Properties\Properties.csproj", "{FB65CB80-58EF-4F49-BB38-DA3712D17216}" 51 | EndProject 52 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceSegregationPrinciple", "InterfaceSegregationPrinciple\InterfaceSegregationPrinciple.csproj", "{5F6DCA35-FF9A-45AD-8B53-9E85CE2C1834}" 53 | EndProject 54 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpaghettiCode", "SpaghettiCode\SpaghettiCode.csproj", "{EFCE698E-38EB-41BB-B3AE-82C99088186E}" 55 | EndProject 56 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MagicNumber", "MagicNumber\MagicNumber.csproj", "{87115A26-0BD5-452D-95F4-9F311FD3AAA7}" 57 | EndProject 58 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DefaultAccessModifiers", "DefaultAccessModifiers\DefaultAccessModifiers.csproj", "{30472DD8-6690-4C3D-982E-FE0485721AFF}" 59 | EndProject 60 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DeepCopyVsShallowCopy", "DeepCopyVsShallowCopy\DeepCopyVsShallowCopy.csproj", "{9B866BCF-8925-4D8B-A0E4-0135B9FF2FC8}" 61 | EndProject 62 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Goto", "Goto\Goto.csproj", "{51FE7B06-C238-4E16-8B5A-1615B6280A20}" 63 | EndProject 64 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Singleton\Singleton.csproj", "{32481A10-9B5A-4163-9B53-9B98047F960F}" 65 | EndProject 66 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Builder", "Builder\Builder.csproj", "{D0637FD8-DABC-4A19-9FD3-DAE63AAC102F}" 67 | EndProject 68 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommonIntermediateLanguage", "CommonIntermediateLanguage\CommonIntermediateLanguage.csproj", "{B5CFE309-005A-494C-89F4-8210AF08BB03}" 69 | EndProject 70 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultipleInheritance", "MultipleInheritance\MultipleInheritance.csproj", "{A40E7839-246A-4AA9-BE93-C3093508A80B}" 71 | EndProject 72 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EqualityOperatorVsEquals", "EqualityOperatorVsEquals\EqualityOperatorVsEquals.csproj", "{12B748CC-44A5-441B-92BD-3C4553AF0236}" 73 | EndProject 74 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter.csproj", "{D3702F00-4CEE-4AE5-89E5-D9770C3670F7}" 75 | EndProject 76 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{FCB15443-46FE-4E35-B0C8-26C84005051B}" 77 | EndProject 78 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DontRepeatYourself", "DontRepeatYourself\DontRepeatYourself.csproj", "{575EFA98-D434-469A-9016-6329AD30B106}" 79 | EndProject 80 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenClosedPrinciple", "OpenClosedPrinciple\OpenClosedPrinciple.csproj", "{9017AEE4-67F4-4698-A64F-F42934D9A018}" 81 | EndProject 82 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IEnumerable", "IEnumerable\IEnumerable.csproj", "{63760F41-0367-46D3-A053-FE236F2BA938}" 83 | EndProject 84 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypesOfErrors", "TypesOfErrors\TypesOfErrors.csproj", "{2B62B015-A506-4252-9995-360E246A8B5F}" 85 | EndProject 86 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Polymorphism", "Polymorphism\Polymorphism.csproj", "{5094CAEC-1F9A-4D6C-B962-4CB86C238F4C}" 87 | EndProject 88 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PartialClasses", "PartialClasses\PartialClasses.csproj", "{176606B5-A5E2-4E4B-9E50-CF15C02FD50A}" 89 | EndProject 90 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryMethod", "FactoryMethod\FactoryMethod.csproj", "{3A92742B-A343-49FE-A165-534FA80EB040}" 91 | EndProject 92 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BoxingAndUnboxing", "BoxingAndUnboxing\BoxingAndUnboxing.csproj", "{5D74437F-4839-44FA-AD87-B5B62480C286}" 93 | EndProject 94 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Encapsulation", "Encapsulation\Encapsulation.csproj", "{37A5EA0E-D207-4D69-BA53-C6CD3EB6245E}" 95 | EndProject 96 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterfaceVsAbstractClass", "InterfaceVsAbstractClass\InterfaceVsAbstractClass.csproj", "{CC1F2325-A5EF-47F1-BB77-1AB21120E667}" 97 | EndProject 98 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionMethods", "ExtensionMethods\ExtensionMethods.csproj", "{9DB2434E-6076-4DCC-8FA1-31248A721FE5}" 99 | EndProject 100 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessModifiers", "AccessModifiers\AccessModifiers.csproj", "{EC9A2D8E-F62F-49BA-94A3-A0D1DB11B4A8}" 101 | EndProject 102 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AccessModifiersOtherAssembly", "AccessModifiersOtherAssembly\AccessModifiersOtherAssembly.csproj", "{5FA7D9F7-7E5A-450F-9CE9-976F0CCCEA76}" 103 | EndProject 104 | Global 105 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 106 | Debug|Any CPU = Debug|Any CPU 107 | Release|Any CPU = Release|Any CPU 108 | EndGlobalSection 109 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 110 | {1F659576-A6A2-4960-A1F8-6822D2077591}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 111 | {1F659576-A6A2-4960-A1F8-6822D2077591}.Debug|Any CPU.Build.0 = Debug|Any CPU 112 | {1F659576-A6A2-4960-A1F8-6822D2077591}.Release|Any CPU.ActiveCfg = Release|Any CPU 113 | {1F659576-A6A2-4960-A1F8-6822D2077591}.Release|Any CPU.Build.0 = Release|Any CPU 114 | {BA3F18A6-D238-411E-86ED-0E263861F955}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 115 | {BA3F18A6-D238-411E-86ED-0E263861F955}.Debug|Any CPU.Build.0 = Debug|Any CPU 116 | {BA3F18A6-D238-411E-86ED-0E263861F955}.Release|Any CPU.ActiveCfg = Release|Any CPU 117 | {BA3F18A6-D238-411E-86ED-0E263861F955}.Release|Any CPU.Build.0 = Release|Any CPU 118 | {675051D6-0A1F-4EF0-9AB7-CFE7D1F33CFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 119 | {675051D6-0A1F-4EF0-9AB7-CFE7D1F33CFF}.Debug|Any CPU.Build.0 = Debug|Any CPU 120 | {675051D6-0A1F-4EF0-9AB7-CFE7D1F33CFF}.Release|Any CPU.ActiveCfg = Release|Any CPU 121 | {675051D6-0A1F-4EF0-9AB7-CFE7D1F33CFF}.Release|Any CPU.Build.0 = Release|Any CPU 122 | {2CF84B13-B115-4722-AA63-99405CE0DE83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 123 | {2CF84B13-B115-4722-AA63-99405CE0DE83}.Debug|Any CPU.Build.0 = Debug|Any CPU 124 | {2CF84B13-B115-4722-AA63-99405CE0DE83}.Release|Any CPU.ActiveCfg = Release|Any CPU 125 | {2CF84B13-B115-4722-AA63-99405CE0DE83}.Release|Any CPU.Build.0 = Release|Any CPU 126 | {E02B2809-5F95-4EC2-BDF5-DF2DA1C5E092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 127 | {E02B2809-5F95-4EC2-BDF5-DF2DA1C5E092}.Debug|Any CPU.Build.0 = Debug|Any CPU 128 | {E02B2809-5F95-4EC2-BDF5-DF2DA1C5E092}.Release|Any CPU.ActiveCfg = Release|Any CPU 129 | {E02B2809-5F95-4EC2-BDF5-DF2DA1C5E092}.Release|Any CPU.Build.0 = Release|Any CPU 130 | {E0A08682-9AE8-4E03-9BAE-F20C45FC0537}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 131 | {E0A08682-9AE8-4E03-9BAE-F20C45FC0537}.Debug|Any CPU.Build.0 = Debug|Any CPU 132 | {E0A08682-9AE8-4E03-9BAE-F20C45FC0537}.Release|Any CPU.ActiveCfg = Release|Any CPU 133 | {E0A08682-9AE8-4E03-9BAE-F20C45FC0537}.Release|Any CPU.Build.0 = Release|Any CPU 134 | {14628827-7D3B-49C7-9784-F2E391F58B19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 135 | {14628827-7D3B-49C7-9784-F2E391F58B19}.Debug|Any CPU.Build.0 = Debug|Any CPU 136 | {14628827-7D3B-49C7-9784-F2E391F58B19}.Release|Any CPU.ActiveCfg = Release|Any CPU 137 | {14628827-7D3B-49C7-9784-F2E391F58B19}.Release|Any CPU.Build.0 = Release|Any CPU 138 | {D272FD9F-2DE2-44BD-AE22-CB5620102650}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 139 | {D272FD9F-2DE2-44BD-AE22-CB5620102650}.Debug|Any CPU.Build.0 = Debug|Any CPU 140 | {D272FD9F-2DE2-44BD-AE22-CB5620102650}.Release|Any CPU.ActiveCfg = Release|Any CPU 141 | {D272FD9F-2DE2-44BD-AE22-CB5620102650}.Release|Any CPU.Build.0 = Release|Any CPU 142 | {7EFA190C-3C3E-4549-8771-FFC37BCD4C6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 143 | {7EFA190C-3C3E-4549-8771-FFC37BCD4C6A}.Debug|Any CPU.Build.0 = Debug|Any CPU 144 | {7EFA190C-3C3E-4549-8771-FFC37BCD4C6A}.Release|Any CPU.ActiveCfg = Release|Any CPU 145 | {7EFA190C-3C3E-4549-8771-FFC37BCD4C6A}.Release|Any CPU.Build.0 = Release|Any CPU 146 | {81D743F6-B69A-49CD-BAA2-CED4388E61A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 147 | {81D743F6-B69A-49CD-BAA2-CED4388E61A4}.Debug|Any CPU.Build.0 = Debug|Any CPU 148 | {81D743F6-B69A-49CD-BAA2-CED4388E61A4}.Release|Any CPU.ActiveCfg = Release|Any CPU 149 | {81D743F6-B69A-49CD-BAA2-CED4388E61A4}.Release|Any CPU.Build.0 = Release|Any CPU 150 | {2B6B37C6-9319-4AD0-9041-4A10CDB88750}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 151 | {2B6B37C6-9319-4AD0-9041-4A10CDB88750}.Debug|Any CPU.Build.0 = Debug|Any CPU 152 | {2B6B37C6-9319-4AD0-9041-4A10CDB88750}.Release|Any CPU.ActiveCfg = Release|Any CPU 153 | {2B6B37C6-9319-4AD0-9041-4A10CDB88750}.Release|Any CPU.Build.0 = Release|Any CPU 154 | {E500B87F-C3A7-4883-964D-938E4DD88B0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 155 | {E500B87F-C3A7-4883-964D-938E4DD88B0C}.Debug|Any CPU.Build.0 = Debug|Any CPU 156 | {E500B87F-C3A7-4883-964D-938E4DD88B0C}.Release|Any CPU.ActiveCfg = Release|Any CPU 157 | {E500B87F-C3A7-4883-964D-938E4DD88B0C}.Release|Any CPU.Build.0 = Release|Any CPU 158 | {4220BB37-2125-4264-AB5D-2DC90B4580D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 159 | {4220BB37-2125-4264-AB5D-2DC90B4580D1}.Debug|Any CPU.Build.0 = Debug|Any CPU 160 | {4220BB37-2125-4264-AB5D-2DC90B4580D1}.Release|Any CPU.ActiveCfg = Release|Any CPU 161 | {4220BB37-2125-4264-AB5D-2DC90B4580D1}.Release|Any CPU.Build.0 = Release|Any CPU 162 | {92C335AF-A5B2-43E1-8FF0-16DB751CEE09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 163 | {92C335AF-A5B2-43E1-8FF0-16DB751CEE09}.Debug|Any CPU.Build.0 = Debug|Any CPU 164 | {92C335AF-A5B2-43E1-8FF0-16DB751CEE09}.Release|Any CPU.ActiveCfg = Release|Any CPU 165 | {92C335AF-A5B2-43E1-8FF0-16DB751CEE09}.Release|Any CPU.Build.0 = Release|Any CPU 166 | {6EF2B4E3-6080-436D-918E-228E3C5FCB10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 167 | {6EF2B4E3-6080-436D-918E-228E3C5FCB10}.Debug|Any CPU.Build.0 = Debug|Any CPU 168 | {6EF2B4E3-6080-436D-918E-228E3C5FCB10}.Release|Any CPU.ActiveCfg = Release|Any CPU 169 | {6EF2B4E3-6080-436D-918E-228E3C5FCB10}.Release|Any CPU.Build.0 = Release|Any CPU 170 | {EF1E4468-7499-41C7-A7F1-ED16D5EF03F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 171 | {EF1E4468-7499-41C7-A7F1-ED16D5EF03F9}.Debug|Any CPU.Build.0 = Debug|Any CPU 172 | {EF1E4468-7499-41C7-A7F1-ED16D5EF03F9}.Release|Any CPU.ActiveCfg = Release|Any CPU 173 | {EF1E4468-7499-41C7-A7F1-ED16D5EF03F9}.Release|Any CPU.Build.0 = Release|Any CPU 174 | {2B0B2400-6A24-4A48-A7F9-0FAB22B619E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 175 | {2B0B2400-6A24-4A48-A7F9-0FAB22B619E0}.Debug|Any CPU.Build.0 = Debug|Any CPU 176 | {2B0B2400-6A24-4A48-A7F9-0FAB22B619E0}.Release|Any CPU.ActiveCfg = Release|Any CPU 177 | {2B0B2400-6A24-4A48-A7F9-0FAB22B619E0}.Release|Any CPU.Build.0 = Release|Any CPU 178 | {3C360BC1-FCF4-4CDF-96E3-11816166588D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 179 | {3C360BC1-FCF4-4CDF-96E3-11816166588D}.Debug|Any CPU.Build.0 = Debug|Any CPU 180 | {3C360BC1-FCF4-4CDF-96E3-11816166588D}.Release|Any CPU.ActiveCfg = Release|Any CPU 181 | {3C360BC1-FCF4-4CDF-96E3-11816166588D}.Release|Any CPU.Build.0 = Release|Any CPU 182 | {9699711D-244A-48E7-ADE5-749792C7BD25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 183 | {9699711D-244A-48E7-ADE5-749792C7BD25}.Debug|Any CPU.Build.0 = Debug|Any CPU 184 | {9699711D-244A-48E7-ADE5-749792C7BD25}.Release|Any CPU.ActiveCfg = Release|Any CPU 185 | {9699711D-244A-48E7-ADE5-749792C7BD25}.Release|Any CPU.Build.0 = Release|Any CPU 186 | {6C31B635-E204-4C05-9D8D-90EC8818168E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 187 | {6C31B635-E204-4C05-9D8D-90EC8818168E}.Debug|Any CPU.Build.0 = Debug|Any CPU 188 | {6C31B635-E204-4C05-9D8D-90EC8818168E}.Release|Any CPU.ActiveCfg = Release|Any CPU 189 | {6C31B635-E204-4C05-9D8D-90EC8818168E}.Release|Any CPU.Build.0 = Release|Any CPU 190 | {D65EC816-9DF3-485F-BDBB-DE8FB4B298A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 191 | {D65EC816-9DF3-485F-BDBB-DE8FB4B298A5}.Debug|Any CPU.Build.0 = Debug|Any CPU 192 | {D65EC816-9DF3-485F-BDBB-DE8FB4B298A5}.Release|Any CPU.ActiveCfg = Release|Any CPU 193 | {D65EC816-9DF3-485F-BDBB-DE8FB4B298A5}.Release|Any CPU.Build.0 = Release|Any CPU 194 | {04D04F22-8BE0-4C4F-BD3B-4ACB6B58A4FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 195 | {04D04F22-8BE0-4C4F-BD3B-4ACB6B58A4FD}.Debug|Any CPU.Build.0 = Debug|Any CPU 196 | {04D04F22-8BE0-4C4F-BD3B-4ACB6B58A4FD}.Release|Any CPU.ActiveCfg = Release|Any CPU 197 | {04D04F22-8BE0-4C4F-BD3B-4ACB6B58A4FD}.Release|Any CPU.Build.0 = Release|Any CPU 198 | {FB65CB80-58EF-4F49-BB38-DA3712D17216}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 199 | {FB65CB80-58EF-4F49-BB38-DA3712D17216}.Debug|Any CPU.Build.0 = Debug|Any CPU 200 | {FB65CB80-58EF-4F49-BB38-DA3712D17216}.Release|Any CPU.ActiveCfg = Release|Any CPU 201 | {FB65CB80-58EF-4F49-BB38-DA3712D17216}.Release|Any CPU.Build.0 = Release|Any CPU 202 | {5F6DCA35-FF9A-45AD-8B53-9E85CE2C1834}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 203 | {5F6DCA35-FF9A-45AD-8B53-9E85CE2C1834}.Debug|Any CPU.Build.0 = Debug|Any CPU 204 | {5F6DCA35-FF9A-45AD-8B53-9E85CE2C1834}.Release|Any CPU.ActiveCfg = Release|Any CPU 205 | {5F6DCA35-FF9A-45AD-8B53-9E85CE2C1834}.Release|Any CPU.Build.0 = Release|Any CPU 206 | {EFCE698E-38EB-41BB-B3AE-82C99088186E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 207 | {EFCE698E-38EB-41BB-B3AE-82C99088186E}.Debug|Any CPU.Build.0 = Debug|Any CPU 208 | {EFCE698E-38EB-41BB-B3AE-82C99088186E}.Release|Any CPU.ActiveCfg = Release|Any CPU 209 | {EFCE698E-38EB-41BB-B3AE-82C99088186E}.Release|Any CPU.Build.0 = Release|Any CPU 210 | {87115A26-0BD5-452D-95F4-9F311FD3AAA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 211 | {87115A26-0BD5-452D-95F4-9F311FD3AAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU 212 | {87115A26-0BD5-452D-95F4-9F311FD3AAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU 213 | {87115A26-0BD5-452D-95F4-9F311FD3AAA7}.Release|Any CPU.Build.0 = Release|Any CPU 214 | {30472DD8-6690-4C3D-982E-FE0485721AFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 215 | {30472DD8-6690-4C3D-982E-FE0485721AFF}.Debug|Any CPU.Build.0 = Debug|Any CPU 216 | {30472DD8-6690-4C3D-982E-FE0485721AFF}.Release|Any CPU.ActiveCfg = Release|Any CPU 217 | {30472DD8-6690-4C3D-982E-FE0485721AFF}.Release|Any CPU.Build.0 = Release|Any CPU 218 | {9B866BCF-8925-4D8B-A0E4-0135B9FF2FC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 219 | {9B866BCF-8925-4D8B-A0E4-0135B9FF2FC8}.Debug|Any CPU.Build.0 = Debug|Any CPU 220 | {9B866BCF-8925-4D8B-A0E4-0135B9FF2FC8}.Release|Any CPU.ActiveCfg = Release|Any CPU 221 | {9B866BCF-8925-4D8B-A0E4-0135B9FF2FC8}.Release|Any CPU.Build.0 = Release|Any CPU 222 | {51FE7B06-C238-4E16-8B5A-1615B6280A20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 223 | {51FE7B06-C238-4E16-8B5A-1615B6280A20}.Debug|Any CPU.Build.0 = Debug|Any CPU 224 | {51FE7B06-C238-4E16-8B5A-1615B6280A20}.Release|Any CPU.ActiveCfg = Release|Any CPU 225 | {51FE7B06-C238-4E16-8B5A-1615B6280A20}.Release|Any CPU.Build.0 = Release|Any CPU 226 | {32481A10-9B5A-4163-9B53-9B98047F960F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 227 | {32481A10-9B5A-4163-9B53-9B98047F960F}.Debug|Any CPU.Build.0 = Debug|Any CPU 228 | {32481A10-9B5A-4163-9B53-9B98047F960F}.Release|Any CPU.ActiveCfg = Release|Any CPU 229 | {32481A10-9B5A-4163-9B53-9B98047F960F}.Release|Any CPU.Build.0 = Release|Any CPU 230 | {D0637FD8-DABC-4A19-9FD3-DAE63AAC102F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 231 | {D0637FD8-DABC-4A19-9FD3-DAE63AAC102F}.Debug|Any CPU.Build.0 = Debug|Any CPU 232 | {D0637FD8-DABC-4A19-9FD3-DAE63AAC102F}.Release|Any CPU.ActiveCfg = Release|Any CPU 233 | {D0637FD8-DABC-4A19-9FD3-DAE63AAC102F}.Release|Any CPU.Build.0 = Release|Any CPU 234 | {B5CFE309-005A-494C-89F4-8210AF08BB03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 235 | {B5CFE309-005A-494C-89F4-8210AF08BB03}.Debug|Any CPU.Build.0 = Debug|Any CPU 236 | {B5CFE309-005A-494C-89F4-8210AF08BB03}.Release|Any CPU.ActiveCfg = Release|Any CPU 237 | {B5CFE309-005A-494C-89F4-8210AF08BB03}.Release|Any CPU.Build.0 = Release|Any CPU 238 | {A40E7839-246A-4AA9-BE93-C3093508A80B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 239 | {A40E7839-246A-4AA9-BE93-C3093508A80B}.Debug|Any CPU.Build.0 = Debug|Any CPU 240 | {A40E7839-246A-4AA9-BE93-C3093508A80B}.Release|Any CPU.ActiveCfg = Release|Any CPU 241 | {A40E7839-246A-4AA9-BE93-C3093508A80B}.Release|Any CPU.Build.0 = Release|Any CPU 242 | {12B748CC-44A5-441B-92BD-3C4553AF0236}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 243 | {12B748CC-44A5-441B-92BD-3C4553AF0236}.Debug|Any CPU.Build.0 = Debug|Any CPU 244 | {12B748CC-44A5-441B-92BD-3C4553AF0236}.Release|Any CPU.ActiveCfg = Release|Any CPU 245 | {12B748CC-44A5-441B-92BD-3C4553AF0236}.Release|Any CPU.Build.0 = Release|Any CPU 246 | {D3702F00-4CEE-4AE5-89E5-D9770C3670F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 247 | {D3702F00-4CEE-4AE5-89E5-D9770C3670F7}.Debug|Any CPU.Build.0 = Debug|Any CPU 248 | {D3702F00-4CEE-4AE5-89E5-D9770C3670F7}.Release|Any CPU.ActiveCfg = Release|Any CPU 249 | {D3702F00-4CEE-4AE5-89E5-D9770C3670F7}.Release|Any CPU.Build.0 = Release|Any CPU 250 | {FCB15443-46FE-4E35-B0C8-26C84005051B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 251 | {FCB15443-46FE-4E35-B0C8-26C84005051B}.Debug|Any CPU.Build.0 = Debug|Any CPU 252 | {FCB15443-46FE-4E35-B0C8-26C84005051B}.Release|Any CPU.ActiveCfg = Release|Any CPU 253 | {FCB15443-46FE-4E35-B0C8-26C84005051B}.Release|Any CPU.Build.0 = Release|Any CPU 254 | {575EFA98-D434-469A-9016-6329AD30B106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 255 | {575EFA98-D434-469A-9016-6329AD30B106}.Debug|Any CPU.Build.0 = Debug|Any CPU 256 | {575EFA98-D434-469A-9016-6329AD30B106}.Release|Any CPU.ActiveCfg = Release|Any CPU 257 | {575EFA98-D434-469A-9016-6329AD30B106}.Release|Any CPU.Build.0 = Release|Any CPU 258 | {9017AEE4-67F4-4698-A64F-F42934D9A018}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 259 | {9017AEE4-67F4-4698-A64F-F42934D9A018}.Debug|Any CPU.Build.0 = Debug|Any CPU 260 | {9017AEE4-67F4-4698-A64F-F42934D9A018}.Release|Any CPU.ActiveCfg = Release|Any CPU 261 | {9017AEE4-67F4-4698-A64F-F42934D9A018}.Release|Any CPU.Build.0 = Release|Any CPU 262 | {63760F41-0367-46D3-A053-FE236F2BA938}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 263 | {63760F41-0367-46D3-A053-FE236F2BA938}.Debug|Any CPU.Build.0 = Debug|Any CPU 264 | {63760F41-0367-46D3-A053-FE236F2BA938}.Release|Any CPU.ActiveCfg = Release|Any CPU 265 | {63760F41-0367-46D3-A053-FE236F2BA938}.Release|Any CPU.Build.0 = Release|Any CPU 266 | {2B62B015-A506-4252-9995-360E246A8B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 267 | {2B62B015-A506-4252-9995-360E246A8B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU 268 | {2B62B015-A506-4252-9995-360E246A8B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU 269 | {2B62B015-A506-4252-9995-360E246A8B5F}.Release|Any CPU.Build.0 = Release|Any CPU 270 | {5094CAEC-1F9A-4D6C-B962-4CB86C238F4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 271 | {5094CAEC-1F9A-4D6C-B962-4CB86C238F4C}.Debug|Any CPU.Build.0 = Debug|Any CPU 272 | {5094CAEC-1F9A-4D6C-B962-4CB86C238F4C}.Release|Any CPU.ActiveCfg = Release|Any CPU 273 | {5094CAEC-1F9A-4D6C-B962-4CB86C238F4C}.Release|Any CPU.Build.0 = Release|Any CPU 274 | {176606B5-A5E2-4E4B-9E50-CF15C02FD50A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 275 | {176606B5-A5E2-4E4B-9E50-CF15C02FD50A}.Debug|Any CPU.Build.0 = Debug|Any CPU 276 | {176606B5-A5E2-4E4B-9E50-CF15C02FD50A}.Release|Any CPU.ActiveCfg = Release|Any CPU 277 | {176606B5-A5E2-4E4B-9E50-CF15C02FD50A}.Release|Any CPU.Build.0 = Release|Any CPU 278 | {3A92742B-A343-49FE-A165-534FA80EB040}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 279 | {3A92742B-A343-49FE-A165-534FA80EB040}.Debug|Any CPU.Build.0 = Debug|Any CPU 280 | {3A92742B-A343-49FE-A165-534FA80EB040}.Release|Any CPU.ActiveCfg = Release|Any CPU 281 | {3A92742B-A343-49FE-A165-534FA80EB040}.Release|Any CPU.Build.0 = Release|Any CPU 282 | {5D74437F-4839-44FA-AD87-B5B62480C286}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 283 | {5D74437F-4839-44FA-AD87-B5B62480C286}.Debug|Any CPU.Build.0 = Debug|Any CPU 284 | {5D74437F-4839-44FA-AD87-B5B62480C286}.Release|Any CPU.ActiveCfg = Release|Any CPU 285 | {5D74437F-4839-44FA-AD87-B5B62480C286}.Release|Any CPU.Build.0 = Release|Any CPU 286 | {37A5EA0E-D207-4D69-BA53-C6CD3EB6245E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 287 | {37A5EA0E-D207-4D69-BA53-C6CD3EB6245E}.Debug|Any CPU.Build.0 = Debug|Any CPU 288 | {37A5EA0E-D207-4D69-BA53-C6CD3EB6245E}.Release|Any CPU.ActiveCfg = Release|Any CPU 289 | {37A5EA0E-D207-4D69-BA53-C6CD3EB6245E}.Release|Any CPU.Build.0 = Release|Any CPU 290 | {CC1F2325-A5EF-47F1-BB77-1AB21120E667}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 291 | {CC1F2325-A5EF-47F1-BB77-1AB21120E667}.Debug|Any CPU.Build.0 = Debug|Any CPU 292 | {CC1F2325-A5EF-47F1-BB77-1AB21120E667}.Release|Any CPU.ActiveCfg = Release|Any CPU 293 | {CC1F2325-A5EF-47F1-BB77-1AB21120E667}.Release|Any CPU.Build.0 = Release|Any CPU 294 | {9DB2434E-6076-4DCC-8FA1-31248A721FE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 295 | {9DB2434E-6076-4DCC-8FA1-31248A721FE5}.Debug|Any CPU.Build.0 = Debug|Any CPU 296 | {9DB2434E-6076-4DCC-8FA1-31248A721FE5}.Release|Any CPU.ActiveCfg = Release|Any CPU 297 | {9DB2434E-6076-4DCC-8FA1-31248A721FE5}.Release|Any CPU.Build.0 = Release|Any CPU 298 | {EC9A2D8E-F62F-49BA-94A3-A0D1DB11B4A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 299 | {EC9A2D8E-F62F-49BA-94A3-A0D1DB11B4A8}.Debug|Any CPU.Build.0 = Debug|Any CPU 300 | {EC9A2D8E-F62F-49BA-94A3-A0D1DB11B4A8}.Release|Any CPU.ActiveCfg = Release|Any CPU 301 | {EC9A2D8E-F62F-49BA-94A3-A0D1DB11B4A8}.Release|Any CPU.Build.0 = Release|Any CPU 302 | {5FA7D9F7-7E5A-450F-9CE9-976F0CCCEA76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 303 | {5FA7D9F7-7E5A-450F-9CE9-976F0CCCEA76}.Debug|Any CPU.Build.0 = Debug|Any CPU 304 | {5FA7D9F7-7E5A-450F-9CE9-976F0CCCEA76}.Release|Any CPU.ActiveCfg = Release|Any CPU 305 | {5FA7D9F7-7E5A-450F-9CE9-976F0CCCEA76}.Release|Any CPU.Build.0 = Release|Any CPU 306 | EndGlobalSection 307 | GlobalSection(SolutionProperties) = preSolution 308 | HideSolutionNode = FALSE 309 | EndGlobalSection 310 | GlobalSection(ExtensibilityGlobals) = postSolution 311 | SolutionGuid = {DD36339B-2D3F-4820-9747-4F50B88CDA91} 312 | EndGlobalSection 313 | EndGlobal 314 | -------------------------------------------------------------------------------- /Encapsulation/Encapsulation.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Encapsulation/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Encapsulation 4 | { 5 | class Point 6 | { 7 | public float X { get; } 8 | public float Y { get; } 9 | 10 | public Point(float x, float y) 11 | { 12 | X = x; 13 | Y = y; 14 | } 15 | } 16 | 17 | class LineSegment 18 | { 19 | public Point Start { get; } 20 | public Point End { get; } 21 | 22 | public LineSegment(Point start, Point end) 23 | { 24 | Start = start; 25 | End = end; 26 | } 27 | 28 | public float Length() 29 | { 30 | var xCoordinatesDifference = End.X - Start.X; 31 | var yCoordinatesDifference = End.Y - Start.Y; 32 | return (float)Math.Sqrt( 33 | (xCoordinatesDifference * xCoordinatesDifference) + 34 | (yCoordinatesDifference * yCoordinatesDifference)); 35 | } 36 | } 37 | 38 | class Program 39 | { 40 | static void Main(string[] args) 41 | { 42 | var lineSegment = new LineSegment(new Point(-3, -2), new Point(1, 1)); 43 | 44 | Console.WriteLine($"Length of the segment is {lineSegment.Length()}"); 45 | Console.ReadKey(); 46 | } 47 | 48 | //no encapsulation here 49 | //static float Length(LineSegment lineSegment) 50 | //{ 51 | // var xCoordinatesDifference = lineSegment.End.X - lineSegment.Start.X; 52 | // var yCoordinatesDifference = lineSegment.End.Y - lineSegment.Start.Y; 53 | // return (float)Math.Sqrt( 54 | // (xCoordinatesDifference * xCoordinatesDifference) + 55 | // (yCoordinatesDifference* yCoordinatesDifference)); 56 | //} 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /EqualityOperatorVsEquals/EqualityOperatorVsEquals.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /EqualityOperatorVsEquals/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace EqualityOperatorVsEquals 4 | { 5 | class CustomClass 6 | { 7 | int _x; 8 | public CustomClass(int x) 9 | { 10 | _x = x; 11 | } 12 | } 13 | 14 | struct CustomStruct 15 | { 16 | int _x; 17 | public CustomStruct(int x) 18 | { 19 | _x = x; 20 | } 21 | } 22 | 23 | class CustomClassOverridingEquals 24 | { 25 | int _x; 26 | public CustomClassOverridingEquals(int x) 27 | { 28 | _x = x; 29 | } 30 | 31 | public override bool Equals(object obj) 32 | { 33 | var item = obj as CustomClassOverridingEquals; 34 | 35 | if (item == null) 36 | { 37 | return false; 38 | } 39 | 40 | return _x == item._x; 41 | } 42 | 43 | public override int GetHashCode() 44 | { 45 | return _x.GetHashCode(); 46 | } 47 | } 48 | 49 | class Program 50 | { 51 | static void Main(string[] args) 52 | { 53 | Console.WriteLine("For strings:"); 54 | string string1 = "abc"; 55 | string string2 = "abc"; 56 | Console.WriteLine($"string1 == string2: {string1 == string2}"); 57 | Console.WriteLine($"string1.Equals(string2): {string1.Equals(string2)}\n"); 58 | 59 | Console.WriteLine("For CustomClass:"); 60 | var customClass1 = new CustomClass(1); 61 | var customClass2 = new CustomClass(1); 62 | Console.WriteLine($"customClass1 == customClass2: {customClass1 == customClass2}"); 63 | Console.WriteLine($"customClass1.Equals(customClass2): {customClass1.Equals(customClass2)}\n"); 64 | 65 | Console.WriteLine("For CustomClassOverridingEquals:"); 66 | var customClassOverridingEquals1 = new CustomClassOverridingEquals(1); 67 | var customClassOverridingEquals2 = new CustomClassOverridingEquals(1); 68 | Console.WriteLine($"customClassOverridingEquals1 == customClassOverridingEquals2: " + 69 | $"{customClassOverridingEquals1 == customClassOverridingEquals2}"); 70 | Console.WriteLine($"customClassOverridingEquals1.Equals(customClassOverridingEquals2): " + 71 | $"{customClassOverridingEquals1.Equals(customClassOverridingEquals2)}\n"); 72 | 73 | Console.WriteLine("For CustomStruct:"); 74 | var customStruct1 = new CustomStruct(1); 75 | var customStruct2 = new CustomStruct(1); 76 | // == operator is not supported for structs unless overloaded 77 | //Console.WriteLine($"CustomStruct1 == CustomStruct2: {customStruct1 == customStruct2}"); 78 | Console.WriteLine($"CustomStruct1.Equals(CustomStruct2): {customStruct1.Equals(customStruct2)}\n"); 79 | 80 | Console.ReadKey(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ExceptionsHandling/ExceptionsHandling.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ExceptionsHandling/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ExceptionsHandling 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine($"6/3 is {DivideNumbers(6, 3)}\n\n"); 10 | Console.WriteLine($"6/0 is {DivideNumbers(6, 0)}\n\n"); 11 | 12 | //global try-catch block 13 | try 14 | { 15 | Application.Run(); 16 | } 17 | catch (Exception ex) 18 | { 19 | Console.WriteLine($"Exception was thrown by the application," + 20 | $" error message is: {ex.Message}"); 21 | } 22 | finally 23 | { 24 | Console.WriteLine( 25 | "Application will close. " + 26 | "No exception have been caught by the global try-catch block."); 27 | } 28 | 29 | Console.ReadKey(); 30 | } 31 | 32 | private static int? DivideNumbers(int a, int b) 33 | { 34 | try 35 | { 36 | return a / b; 37 | } 38 | catch (DivideByZeroException ex) 39 | { 40 | Console.WriteLine($"Attempt to divide by zero." + 41 | $" Exception message: {ex.Message}"); 42 | return null; 43 | } 44 | finally 45 | { 46 | Console.WriteLine("Executing finally block"); 47 | } 48 | } 49 | 50 | private static int? ElementAtIndex(int[] numbers, int index) 51 | { 52 | try 53 | { 54 | return numbers[index]; 55 | } 56 | //catch clauses in order from the most specific to the most generic 57 | catch (NullReferenceException) 58 | { 59 | Console.WriteLine($"Input array is null"); 60 | return null; 61 | } 62 | catch (IndexOutOfRangeException) 63 | { 64 | Console.WriteLine($"Index {index} does not exist in input array"); 65 | return null; 66 | } 67 | catch (Exception ex) 68 | { 69 | Console.WriteLine($"Unexpected error, see message: {ex.Message}"); 70 | return null; 71 | } 72 | } 73 | } 74 | 75 | internal class Application 76 | { 77 | internal static void Run() 78 | { 79 | //imagine some application logic here... 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /ExtensionMethods/ExtensionMethods.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ExtensionMethods/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ExtensionMethods 4 | { 5 | public static class StringExtensions 6 | { 7 | public static int NumberOfLines(this string input) 8 | { 9 | return input.Split("\n").Length; 10 | } 11 | } 12 | 13 | public class Duck 14 | { 15 | public string Quack() 16 | { 17 | return "(Not an extension method) Quack, quack, I'm a duck"; 18 | } 19 | } 20 | 21 | public static class DuckExtensions 22 | { 23 | public static string Quack(this Duck duck) 24 | { 25 | return "(This is an extension method) Quack, quack, I'm a duck"; 26 | } 27 | } 28 | 29 | class Program 30 | { 31 | static void Main(string[] args) 32 | { 33 | 34 | var multilineString = @"Said the Duck to the Kangaroo, 35 | Good gracious! how you hop 36 | Over the fields, and the water too, 37 | As if you never would stop! 38 | My life is a bore in this nasty pond; 39 | And I long to go out in the world beyond: 40 | I wish I could hop like you, 41 | Said the Duck to the Kangaroo."; 42 | 43 | var numberOfLines = multilineString.NumberOfLines(); 44 | 45 | Console.WriteLine($"Number of lines is {numberOfLines}\n"); 46 | 47 | var duck = new Duck(); 48 | //the non-extension method will be called 49 | Console.WriteLine(duck.Quack()); 50 | 51 | Console.ReadKey(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /FactoryMethod/FactoryMethod.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /FactoryMethod/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace FactoryMethod 5 | { 6 | enum Skills 7 | { 8 | Jenkins, 9 | Docker, 10 | TeamCity, 11 | CSharp, 12 | CleanCode, 13 | Selenium, 14 | Postman, 15 | BlazeMeter 16 | } 17 | 18 | interface IEmployee 19 | { 20 | 21 | } 22 | 23 | class DevOps : IEmployee 24 | { 25 | 26 | } 27 | 28 | class CSharpDeveloper : IEmployee 29 | { 30 | 31 | } 32 | 33 | class Tester : IEmployee 34 | { 35 | 36 | } 37 | 38 | interface IHiringAgency 39 | { 40 | IEmployee Hire(Skills[] expectedSkills); 41 | } 42 | 43 | class EmployeeFactory : IHiringAgency 44 | { 45 | public IEmployee Hire(Skills[] expectedSkills) 46 | { 47 | if (Enumerable.SequenceEqual(expectedSkills, 48 | new[] { Skills.Jenkins, Skills.Docker, Skills.TeamCity })) 49 | { 50 | return new DevOps(); 51 | } 52 | if (Enumerable.SequenceEqual(expectedSkills, 53 | new[] { Skills.CSharp, Skills.CleanCode })) 54 | { 55 | return new CSharpDeveloper(); 56 | } 57 | if (Enumerable.SequenceEqual(expectedSkills, 58 | new[] { Skills.Selenium, Skills.Postman, Skills.BlazeMeter })) 59 | { 60 | return new Tester(); 61 | } 62 | throw new ArgumentException("Unexpected skillset"); 63 | 64 | } 65 | } 66 | 67 | //this class implement STATIC Factory Method design pattern - not to be confused with Factory Method design pattern! 68 | class BankAccount 69 | { 70 | private int _maxWithdrawalSum; 71 | 72 | private BankAccount(bool isForChildren) 73 | { 74 | if (isForChildren) 75 | { 76 | _maxWithdrawalSum = 1000; 77 | } 78 | else 79 | { 80 | _maxWithdrawalSum = 10000; 81 | } 82 | } 83 | 84 | public static BankAccount ForChildren() 85 | { 86 | return new BankAccount(true); 87 | } 88 | 89 | public static BankAccount Regular() 90 | { 91 | return new BankAccount(false); 92 | } 93 | } 94 | 95 | class Program 96 | { 97 | static void Main(string[] args) 98 | { 99 | var firstEmployeeRequiredSkills = new[] { Skills.Jenkins, Skills.Docker, Skills.TeamCity }; 100 | var secondEmployeeSkills = new[] { Skills.CSharp, Skills.CleanCode }; 101 | var thirdEmployeeSkills = new[] { Skills.Selenium, Skills.Postman, Skills.BlazeMeter }; 102 | 103 | var employee = new EmployeeFactory().Hire(firstEmployeeRequiredSkills); 104 | Console.WriteLine($"Hired employee is {employee}"); 105 | 106 | var bankAccount = BankAccount.ForChildren(); 107 | var otherBankAccount = BankAccount.Regular(); 108 | 109 | Console.ReadKey(); 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /GarbageCollectorMemoryLeak/GarbageCollectorMemoryLeak.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /GarbageCollectorMemoryLeak/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace GarbageCollectorMemoryLeak 4 | { 5 | class Program 6 | { 7 | public class MainWindow 8 | { 9 | public event EventHandler mainWindowEventHandler; 10 | 11 | private void buttonClick_OpenChildWindow(object sender, EventArgs e) 12 | { 13 | var childWindow = new ChildWindow(this); 14 | childWindow.Show(); 15 | } 16 | } 17 | 18 | public class ChildWindow 19 | { 20 | private MainWindow _mainApplicationWindow; 21 | 22 | public ChildWindow(MainWindow mainApplicationWindow) 23 | { 24 | _mainApplicationWindow = mainApplicationWindow; 25 | _mainApplicationWindow.mainWindowEventHandler += HandleEventFromMainWindowInChildWindow; 26 | } 27 | 28 | private void HandleEventFromMainWindowInChildWindow(object sender, EventArgs e) 29 | { 30 | } 31 | 32 | internal void Show() 33 | { 34 | //opening a window 35 | } 36 | 37 | void OnWindowClosing() 38 | { 39 | _mainApplicationWindow.mainWindowEventHandler -= HandleEventFromMainWindowInChildWindow; 40 | } 41 | } 42 | 43 | static void Main(string[] args) 44 | { 45 | Console.WriteLine("Hello World!"); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Generics/Generics.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 6 | Exe 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Generics/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Generics 5 | { 6 | //non-generic classes 7 | public class IntegersList 8 | { 9 | public void Add(int item) 10 | { 11 | //... 12 | } 13 | } 14 | 15 | public class StringsList 16 | { 17 | public void Add(string item) 18 | { 19 | //... 20 | } 21 | } 22 | 23 | public class DoublesList 24 | { 25 | public void Add(double item) 26 | { 27 | //... 28 | } 29 | } 30 | 31 | //generic class 32 | public class List 33 | { 34 | public void Add(T item) 35 | { 36 | //... 37 | } 38 | } 39 | 40 | #region constraints 41 | 42 | public class OnlyReferenceTypes where T : class 43 | { 44 | 45 | } 46 | 47 | public class OnlyValueTypes where T : struct 48 | { 49 | 50 | } 51 | 52 | public class OnlyTypesWithParameterlessConstructor where T : new() 53 | { 54 | 55 | } 56 | 57 | public class BaseClass 58 | { 59 | 60 | } 61 | 62 | public class OnlyDerivedFromBaseClass where T : BaseClass 63 | { 64 | 65 | } 66 | 67 | public interface IFlying 68 | { 69 | 70 | } 71 | 72 | public class OnlyImplementingIFlyingInterface where T : IFlying 73 | { 74 | 75 | } 76 | 77 | #endregion 78 | 79 | public class Program 80 | { 81 | static void Main(string[] args) 82 | { 83 | var currencies = new Dictionary 84 | { 85 | ["USA"] = "USD", 86 | ["Great Britain"] = "GBP" 87 | }; 88 | 89 | var yearsOfBirth = new Dictionary 90 | { 91 | ["John Smith"] = 1980, 92 | ["Monica Smith"] = 1983 93 | }; 94 | 95 | //var invalidObject = new OnlyImplementingIFlyingInterface(); //will not compile as constraint is not met 96 | 97 | Console.ReadKey(); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Goto/DatabaseMock.cs: -------------------------------------------------------------------------------- 1 | namespace Goto 2 | { 3 | public class DatabaseMock : IDatabase 4 | { 5 | public Person GetPerson(string personId) 6 | { 7 | if(personId == "John") 8 | { 9 | return new Person("John", "Smith", null, null, "Rex"); 10 | } 11 | 12 | return null; 13 | } 14 | 15 | public Pet GetPet(string petId) 16 | { 17 | if (petId == "Rex") 18 | { 19 | return new Pet("Rex"); 20 | } 21 | 22 | return null; 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Goto/Goto.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Goto/IDatabase.cs: -------------------------------------------------------------------------------- 1 | namespace Goto 2 | { 3 | public interface IDatabase 4 | { 5 | Person GetPerson(string personId); 6 | Pet GetPet(string petId); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Goto/ILogger.cs: -------------------------------------------------------------------------------- 1 | namespace Goto 2 | { 3 | public interface ILogger 4 | { 5 | public void LogError(string message); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Goto/Person.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Goto 4 | { 5 | public class Person 6 | { 7 | public string Name { get; } 8 | public string LastName { get; } 9 | public DateTime? DateOfBirth { get; } 10 | public string PetId { get; } 11 | public string FamilyPetId { get; } 12 | 13 | public Person(string name, string lastName, DateTime? dateOfBirth, string petId, string familyPetId) 14 | { 15 | Name = name; 16 | LastName = lastName; 17 | DateOfBirth = dateOfBirth; 18 | PetId = petId; 19 | FamilyPetId = familyPetId; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Goto/PersonalDataFormatter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Goto 4 | { 5 | class PersonalDataFormatter 6 | { 7 | private readonly IDatabase _database; 8 | 9 | public PersonalDataFormatter(IDatabase database) 10 | { 11 | _database = database; 12 | } 13 | 14 | public string FormatPersonalData(string personId) 15 | { 16 | var person = _database.GetPerson(personId); 17 | var personsPet = GetPet(person); 18 | if (person == null || personsPet == null) 19 | { 20 | Console.WriteLine("Database read error occured"); 21 | return null; 22 | } 23 | 24 | return person.DateOfBirth == null ? 25 | FormatPersonWithUnknownDateOfBirth(person, personsPet) : 26 | FormatPerson(person, personsPet); 27 | } 28 | 29 | private Pet GetPet(Person person) 30 | { 31 | return person == null ? null : _database.GetPet(person.PetId ?? person.FamilyPetId); 32 | } 33 | 34 | private static string FormatPerson(Person person, Pet personsPet) 35 | { 36 | return FormatPerson(person, personsPet, $"born in {person.DateOfBirth.Value.Year}"); 37 | } 38 | 39 | private static string FormatPersonWithUnknownDateOfBirth(Person person, Pet personsPet) 40 | { 41 | Console.WriteLine("The date of birth is not known"); 42 | return FormatPerson(person, personsPet, $"(date of birth is unknown)"); 43 | } 44 | 45 | private static string FormatPerson(Person person, Pet personsPet, string dateOfBirthInformation) 46 | { 47 | return $"{person.Name} {person.LastName} with pet " + 48 | $"{(personsPet.PetId == null ? "unknown" : personsPet.PetId)}" + 49 | $" {dateOfBirthInformation}"; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Goto/Pet.cs: -------------------------------------------------------------------------------- 1 | namespace Goto 2 | { 3 | public class Pet 4 | { 5 | public Pet(string petId) 6 | { 7 | PetId = petId; 8 | } 9 | 10 | public string PetId { get; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Goto/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Goto 5 | { 6 | class GotoSimpleUse 7 | { 8 | public int? TransformNumberToNullWhenZero(int a) 9 | { 10 | if (a == 0) 11 | { 12 | goto HandleZeroCase; 13 | } 14 | 15 | return a; 16 | 17 | HandleZeroCase: 18 | return null; 19 | } 20 | } 21 | 22 | class GotoInfiniteLoop 23 | { 24 | public void InfiniteLoop() 25 | { 26 | GotoMarker: 27 | goto GotoMarker; 28 | } 29 | } 30 | 31 | class GotoNestedLoops 32 | { 33 | public int NestedLoops(int[][][] input) 34 | { 35 | int totalNumberOfCalculations = 0; 36 | int maxNumberOfCalculations = 500; 37 | for (int i = 0; i < input.Length; i++) 38 | { 39 | for (int j = 0; j < input.Length; j++) 40 | { 41 | for (int k = 0; k < input.Length; k++) 42 | { 43 | //do some calculations here... 44 | ++totalNumberOfCalculations; 45 | if (totalNumberOfCalculations == maxNumberOfCalculations) 46 | { 47 | //break; //unfortunately, this will only break from the inner-most loop 48 | 49 | goto AfterLoop; 50 | //return totalNumberOfCalculations; //we can also return here to achieve the same effect without goto 51 | } 52 | } 53 | } 54 | } 55 | AfterLoop: 56 | return totalNumberOfCalculations; 57 | } 58 | } 59 | 60 | enum EntityType 61 | { 62 | Person, 63 | Pet 64 | } 65 | 66 | class GotoCommonCleanupLogic 67 | { 68 | private readonly IDatabase _database; 69 | private readonly ILogger _logger; 70 | 71 | public GotoCommonCleanupLogic(IDatabase database, ILogger logger) 72 | { 73 | _database = database; 74 | _logger = logger; 75 | } 76 | 77 | public bool CheckIfAllIdsExistInTheDatabase(List ids, EntityType entityType) 78 | { 79 | foreach (var id in ids) 80 | { 81 | switch (entityType) 82 | { 83 | case EntityType.Person: 84 | var person = _database.GetPerson(id); 85 | if (person == null) 86 | { 87 | goto ReportError; 88 | } 89 | break; 90 | case EntityType.Pet: 91 | var pet = _database.GetPet(id); 92 | if (pet == null) 93 | { 94 | goto ReportError; 95 | } 96 | break; 97 | default: 98 | goto ReportError; 99 | } 100 | } 101 | 102 | return true; 103 | 104 | ReportError: 105 | _logger.LogError("Database read error"); 106 | return false; 107 | } 108 | } 109 | 110 | class CommonCleanupLogicWithoutGoto 111 | { 112 | private readonly IDatabase _database; 113 | private readonly ILogger _logger; 114 | 115 | public CommonCleanupLogicWithoutGoto(IDatabase database, ILogger logger) 116 | { 117 | _database = database; 118 | _logger = logger; 119 | } 120 | 121 | public bool CheckIfAllIdsExistInTheDatabase(List ids, EntityType entityType) 122 | { 123 | foreach (var id in ids) 124 | { 125 | switch (entityType) 126 | { 127 | case EntityType.Person: 128 | var person = _database.GetPerson(id); 129 | if (person == null) 130 | { 131 | LogError(); 132 | return false; 133 | } 134 | break; 135 | case EntityType.Pet: 136 | var pet = _database.GetPet(id); 137 | if (pet == null) 138 | { 139 | LogError(); 140 | return false; 141 | } 142 | break; 143 | default: 144 | LogError(); 145 | return false; 146 | } 147 | } 148 | 149 | return true; 150 | } 151 | 152 | private void LogError() 153 | { 154 | _logger.LogError("Database read error"); 155 | } 156 | } 157 | 158 | 159 | class Program 160 | { 161 | static void Main(string[] args) 162 | { 163 | //careful - this will freeze the program because of the infinite loop 164 | //new GotoInfiniteLoop().InfiniteLoop(); 165 | 166 | var personalData = new VeryUglyPersonalDataFormatter(new DatabaseMock()).FormatPersonalData("John"); 167 | Console.WriteLine(personalData); 168 | 169 | Console.ReadKey(); 170 | } 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /Goto/VeryUglyPersonalDataFormatter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Goto 4 | { 5 | class VeryUglyPersonalDataFormatter 6 | { 7 | private readonly IDatabase _database; 8 | 9 | public VeryUglyPersonalDataFormatter(IDatabase database) 10 | { 11 | _database = database; 12 | } 13 | 14 | public string FormatPersonalData(string personId) 15 | { 16 | var person = _database.GetPerson(personId); 17 | if (person == null) 18 | { 19 | goto HandleError; 20 | } 21 | 22 | var petId = person.PetId; 23 | ReadPet: 24 | var personsPet = _database.GetPet(petId); 25 | if (personsPet == null) 26 | { 27 | if (person.FamilyPetId != null && petId != person.FamilyPetId) 28 | { 29 | petId = person.FamilyPetId; 30 | goto ReadPet; 31 | } 32 | goto HandleError; 33 | } 34 | 35 | if (person.DateOfBirth == null) 36 | { 37 | Console.WriteLine("The date of birth is not known"); 38 | goto FormatPersonWithUnknownDateOfBirthText; 39 | } 40 | else 41 | { 42 | goto FormatPersonText; 43 | } 44 | 45 | FormatPersonText: 46 | return $"{person.Name} {person.LastName} with pet " + 47 | $"{(personsPet.PetId == null ? "unknown" : personsPet.PetId)}" + 48 | $" born in {person.DateOfBirth.Value.Year}"; 49 | 50 | FormatPersonWithUnknownDateOfBirthText: 51 | return $"{person.Name} {person.LastName} with pet " + 52 | $"{(personsPet.PetId == null ? "unknown" : personsPet.PetId)}" + 53 | $" (date of birth is unknown)"; 54 | 55 | HandleError: 56 | Console.WriteLine("Database read error occured"); 57 | return null; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /IEnumerable/IEnumerable.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /IEnumerable/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Linq; 4 | 5 | namespace IEnumerable 6 | { 7 | class CustomCollection 8 | { 9 | public string[] Words { get; } 10 | 11 | public CustomCollection(string[] words) 12 | { 13 | Words = words; 14 | } 15 | } 16 | 17 | class WordsCollection : System.Collections.IEnumerable 18 | { 19 | private string[] _words; 20 | 21 | public WordsCollection(string[] words) 22 | { 23 | _words = words; 24 | } 25 | 26 | public IEnumerator GetEnumerator() 27 | { 28 | return new WordsEnumerator(_words); 29 | } 30 | } 31 | 32 | class WordsEnumerator : IEnumerator 33 | { 34 | private string[] _words; 35 | private int _position = -1; 36 | 37 | public WordsEnumerator(string[] words) 38 | { 39 | _words = words; 40 | } 41 | 42 | public object Current 43 | { 44 | get 45 | { 46 | try 47 | { 48 | return _words[_position]; 49 | } 50 | catch (IndexOutOfRangeException) 51 | { 52 | throw new InvalidOperationException("Collection end reached"); 53 | } 54 | } 55 | } 56 | 57 | public bool MoveNext() 58 | { 59 | _position++; 60 | return _position < _words.Length; 61 | } 62 | 63 | public void Reset() 64 | { 65 | _position = -1; 66 | } 67 | } 68 | 69 | class Program 70 | { 71 | static void Main(string[] args) 72 | { 73 | var words = new[] { "a", "little", "duck" }; 74 | 75 | Console.WriteLine("With foreach loop:"); 76 | foreach (var word in words) 77 | { 78 | Console.WriteLine(word); 79 | } 80 | 81 | //the code above and the code below are eqivalent 82 | 83 | Console.WriteLine("\nWith enumerator:"); 84 | IEnumerator wordsEnumerator = words.GetEnumerator(); 85 | string currentWord; 86 | while (wordsEnumerator.MoveNext()) 87 | { 88 | currentWord = (string)wordsEnumerator.Current; 89 | Console.WriteLine(currentWord); 90 | } 91 | 92 | //this doesn't work because CustomCollection does not implement IEnumerable 93 | var customCollection = new CustomCollection(words); 94 | //foreach(var word in customCollection) 95 | //{ 96 | // Console.WriteLine(word); 97 | //} 98 | 99 | // WordsCollection implements IEnumerable 100 | Console.WriteLine("\nCustom WordsCollection implementing IEnumerable:"); 101 | var wordsCollection = new WordsCollection(words); 102 | foreach (var word in wordsCollection) 103 | { 104 | Console.WriteLine(word); 105 | } 106 | 107 | Console.ReadKey(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /InterfaceSegregationPrinciple/InterfaceSegregationPrinciple.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /InterfaceSegregationPrinciple/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace InterfaceSegregationPrinciple 5 | { 6 | class Program 7 | { 8 | public interface IBike 9 | { 10 | void Ride(); 11 | void InflateTheTyre(); 12 | void Charge(); 13 | } 14 | 15 | public class ElectricBike : IBike 16 | { 17 | public void Ride() 18 | { 19 | Console.WriteLine("Use your muscles to move forward."); 20 | } 21 | 22 | public void InflateTheTyre() 23 | { 24 | Console.WriteLine("Use the pump to inflate the tyre."); 25 | } 26 | 27 | public void Charge() 28 | { 29 | Console.WriteLine("Charging the battery of an electric bike."); 30 | } 31 | } 32 | 33 | public class Bike : IBike 34 | { 35 | //this method is problematic 36 | public void Charge() 37 | { 38 | throw new NotSupportedException("Non-electric bike cannot be charged."); 39 | } 40 | 41 | public void Ride() 42 | { 43 | Console.WriteLine("Use your muscles to move forward."); 44 | } 45 | 46 | public void InflateTheTyre() 47 | { 48 | Console.WriteLine("Use the pump to inflate the tyre."); 49 | } 50 | } 51 | 52 | private static void AddElementToList(IList list) 53 | { 54 | list.Add(1); 55 | } 56 | 57 | static void Main(string[] args) 58 | { 59 | var list = new List(); 60 | var array = new int[0]; 61 | 62 | AddElementToList(list); //this will work fine 63 | AddElementToList(array); //this will throw NotSupportedException 64 | 65 | Console.ReadKey(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /InterfaceVsAbstractClass/InterfaceVsAbstractClass.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /InterfaceVsAbstractClass/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace InterfaceVsAbstractClass 4 | { 5 | interface IFlyable 6 | { 7 | void Fly(); //no "public", no method body 8 | 9 | void MethodWithDefaultImplementation() 10 | { 11 | Console.WriteLine("this method is implemented in the interface"); 12 | } 13 | } 14 | 15 | class Bird : IFlyable 16 | { 17 | public void Fly() 18 | { 19 | Console.WriteLine("Flying using fuel of grain and worms."); 20 | } 21 | } 22 | 23 | class Drone : IFlyable 24 | { 25 | public void Fly() 26 | { 27 | Console.WriteLine("Flying using energy stored in battery."); 28 | } 29 | } 30 | 31 | abstract class Animal 32 | { 33 | public abstract void Move(); 34 | } 35 | 36 | abstract class Mammal : Animal //abstract class inheriting from abstract class 37 | { 38 | public void ProduceMilk() 39 | { 40 | Console.WriteLine("Producing milk to feed its young"); 41 | } 42 | } 43 | 44 | class Snake : Animal 45 | { 46 | public override void Move() 47 | { 48 | Console.WriteLine("Slithering on belly"); 49 | } 50 | } 51 | 52 | class Dog : Mammal 53 | { 54 | public override void Move() 55 | { 56 | Console.WriteLine("Running using four legs"); 57 | } 58 | } 59 | 60 | //class Cat : Mammal //does not compile - MUST provide implementation of the Move method 61 | //{ 62 | //} 63 | 64 | class Program 65 | { 66 | static void Main(string[] args) 67 | { 68 | //var animal = new Animal(); //can't create an instance of an abstract class 69 | //var mammal = new Mammal(); //can't create an instance of an abstract class 70 | 71 | var dog = new Dog(); 72 | var snake = new Snake(); 73 | 74 | Console.ReadKey(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /LINQ/LINQ.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LINQ/Person.cs: -------------------------------------------------------------------------------- 1 | namespace LINQ 2 | { 3 | public class Person 4 | { 5 | public string Name; 6 | public string LastName; 7 | public int YearOfBirth; 8 | 9 | public Person(string name, string lastName, int yearOfBirth) 10 | { 11 | Name = name; 12 | LastName = lastName; 13 | YearOfBirth = yearOfBirth; 14 | } 15 | 16 | public override string ToString() 17 | { 18 | return $"{Name} {LastName} born in {YearOfBirth}"; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LINQ/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace LINQ 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | var pirates = new List 12 | { 13 | new Person("Anne", "Bonny", 1698), 14 | new Person("Charles", "Vane", 1680), 15 | new Person("Mary", "Read", 1690), 16 | new Person("Bartolomew", "Roberts", 1682), 17 | }; 18 | 19 | var bornAfter1685QuerySyntax = from pirate in pirates 20 | where pirate.YearOfBirth > 1685 21 | select pirate; 22 | 23 | var bornAfter1685MethodSyntax = pirates.Where(pirate => pirate.YearOfBirth > 1685); 24 | 25 | foreach (var pirate in bornAfter1685QuerySyntax) 26 | { 27 | Console.WriteLine($"{pirate.Name} {pirate.LastName} was born after 1685"); 28 | } 29 | 30 | IEnumerable orderedByLastName = pirates.OrderBy(pirate => pirate.LastName); 31 | 32 | IEnumerable onlyYearsOfBirth = pirates.Select(pirate => pirate.YearOfBirth); 33 | 34 | double averageYearOfBirth = pirates.Average(pirate => pirate.YearOfBirth); 35 | 36 | bool isAnyPirateBornBefore1650 = pirates.Any(pirate => pirate.YearOfBirth < 1650); 37 | 38 | bool areAllPiratesBornAfter1650 = pirates.All(pirate => pirate.YearOfBirth > 1650); 39 | 40 | IEnumerable piratesWithLastNameStartingWithR = pirates.Where( 41 | pirate => pirate.LastName.StartsWith("R")); 42 | 43 | Person firstPirateByAlphabet = pirates.OrderBy(pirate => pirate.LastName).First(); 44 | 45 | IEnumerable piratesFromYoungestToOldest = pirates.OrderBy( 46 | pirate => pirate.YearOfBirth).Reverse(); 47 | 48 | Console.ReadKey(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /LiskovsSubstitutionPrinciple/AbuseOfInterfaceImplementationBySubclass.cs: -------------------------------------------------------------------------------- 1 | namespace LiskovSubstitutionPrinciple 2 | { 3 | class Plane 4 | { 5 | protected virtual int MaxFuel => 1000; 6 | protected virtual int RemainingFuel => 100; 7 | 8 | public virtual float PercentOfRemainingFuel() 9 | { 10 | return ((float)RemainingFuel / MaxFuel) * 100; 11 | } 12 | } 13 | 14 | class ToyPlane : Plane 15 | { 16 | protected override int MaxFuel => 0; 17 | protected override int RemainingFuel => 0; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /LiskovsSubstitutionPrinciple/LiskovSubstitutionPrinciple.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LiskovsSubstitutionPrinciple/PreconditionStrengthenedBySubtype.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LiskovSubstitutionPrinciple 4 | { 5 | class BankAccount 6 | { 7 | public virtual void WithdrawMoney(int amount) 8 | { 9 | if (amount < 10000) 10 | { 11 | Console.WriteLine($"Withdrawing money (amount: {amount})"); 12 | } 13 | else 14 | { 15 | Console.WriteLine($"Withdrawing sum of {amount} requires extra authorization."); 16 | } 17 | } 18 | } 19 | 20 | class ChildBankAccount : BankAccount 21 | { 22 | public override void WithdrawMoney(int amount) 23 | { 24 | if (amount < 1000) 25 | { 26 | Console.WriteLine($"Withdrawing money (amount: {amount})"); 27 | } 28 | else 29 | { 30 | Console.WriteLine($"Withdrawing sum of {amount} requires extra authorization."); 31 | } 32 | } 33 | } 34 | 35 | class CashMachine 36 | { 37 | public void WithdrawFromAccount(BankAccount bankAccount, int amount) 38 | { 39 | bankAccount.WithdrawMoney(amount); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /LiskovsSubstitutionPrinciple/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LiskovSubstitutionPrinciple 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | var plane = new Plane(); 10 | var toyPlane = new ToyPlane(); 11 | PrintPercentOfRemainingFuel(plane); 12 | PrintPercentOfRemainingFuel(toyPlane); //this will produce NaN 13 | 14 | Console.ReadKey(); 15 | } 16 | 17 | private static void PrintPercentOfRemainingFuel(Plane plane) 18 | { 19 | Console.WriteLine($"\nFuel left: {plane.PercentOfRemainingFuel()}"); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LiskovsSubstitutionPrinciple/RuntimeTypeSwitching.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace LiskovSubstitutionPrinciple 4 | { 5 | interface IFlyable 6 | { 7 | void Fly(); 8 | } 9 | 10 | class Bird : IFlyable 11 | { 12 | public void Fly() 13 | { 14 | Console.WriteLine("Flying using fuel of grain and worms."); 15 | } 16 | 17 | public void FlapWings() 18 | { 19 | Console.WriteLine("Flapping my wings."); 20 | } 21 | } 22 | 23 | class Drone : IFlyable 24 | { 25 | public void Fly() 26 | { 27 | Console.WriteLine("Flying using energy stored in battery."); 28 | } 29 | } 30 | 31 | class AllFlyer 32 | { 33 | static void FlyAll(IFlyable[] flyables) 34 | { 35 | foreach (var flyable in flyables) 36 | { 37 | if (flyable is Bird bird) //this method should not be aware of specific types implementing the IFlyable interface 38 | { 39 | bird.FlapWings(); 40 | Console.WriteLine("Special case for a bird."); 41 | } 42 | flyable.Fly(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MagicNumber/MagicNumber.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MagicNumber/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MagicNumber 4 | { 5 | class TaxRulesProvider 6 | { 7 | public const decimal VatRateLow = 1.05m; 8 | public const decimal VatRateMedium = 1.08m; 9 | public const decimal VatRateHigh = 1.23m; 10 | 11 | public class BakingIndustry 12 | { 13 | public const int ShortExpiryLimit = 14; 14 | public const int LongExpiryLimit = 45; 15 | } 16 | } 17 | 18 | class PriceCalculator 19 | { 20 | public decimal Calculate( 21 | decimal basePrice, bool isFrozen, bool isBakedGoods, 22 | bool isPie, int daysToExpiry) 23 | { 24 | if (isFrozen) 25 | { 26 | return basePrice * TaxRulesProvider.VatRateLow; 27 | } 28 | if (isBakedGoods && daysToExpiry <= 29 | TaxRulesProvider.BakingIndustry.ShortExpiryLimit) 30 | { 31 | return basePrice * TaxRulesProvider.VatRateLow; 32 | } 33 | if (isPie && daysToExpiry <= 34 | TaxRulesProvider.BakingIndustry.LongExpiryLimit) 35 | { 36 | return basePrice * TaxRulesProvider.VatRateMedium; 37 | } 38 | if (isPie && daysToExpiry > 39 | TaxRulesProvider.BakingIndustry.LongExpiryLimit) 40 | { 41 | return basePrice * TaxRulesProvider.VatRateHigh; 42 | } 43 | else 44 | { 45 | throw new Exception("Invalid product"); 46 | } 47 | } 48 | 49 | //warning - magic numbers below! 50 | //public double Calculate( 51 | // double basePrice, bool isFrozen, bool isBakedGoods, 52 | // bool isPie, int daysToExpiry) 53 | //{ 54 | // if (isFrozen) 55 | // { 56 | // return basePrice * 1.05; 57 | // } 58 | // if (isBakedGoods && daysToExpiry <= 14) 59 | // { 60 | // return basePrice * 1.05; 61 | // } 62 | // if (isPie && daysToExpiry <= 45) 63 | // { 64 | // return basePrice * 1.08; 65 | // } 66 | // if (isPie && daysToExpiry > 45) 67 | // { 68 | // return basePrice * 1.23; 69 | // } 70 | // else 71 | // { 72 | // throw new Exception("Invalid product"); 73 | // } 74 | //} 75 | } 76 | 77 | class Program 78 | { 79 | static void Main(string[] args) 80 | { 81 | Console.WriteLine("Hello!"); 82 | Console.ReadKey(); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /MethodOverloading/MethodOverloading.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MethodOverloading/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace MethodOverloading 5 | { 6 | class Program 7 | { 8 | 9 | private static int Add(int a, int b) 10 | { 11 | return a + b; 12 | } 13 | 14 | private static int Add(ref int a, ref int b) 15 | { 16 | return a + b; 17 | } 18 | 19 | //does not compile - the methods can't differ only by ref/out modifiers 20 | //private static int Add(ref int a, out int b) 21 | //{ 22 | // return a + b; 23 | //} 24 | 25 | //does not compile - the methods can't differ only by return type 26 | //private static string Add(int a, int b) 27 | //{ 28 | // return a.ToString() + b.ToString(); 29 | //} 30 | 31 | private static string Add(string a, string b) 32 | { 33 | return a + b; 34 | } 35 | 36 | private static int Add(int a, int b, int c) 37 | { 38 | return a + b + c; 39 | } 40 | 41 | private static void Add(List list, int newElement) 42 | { 43 | list.Add(newElement); 44 | } 45 | 46 | private static void Add(int newElement, List list) 47 | { 48 | list.Add(newElement * 2); 49 | } 50 | 51 | private static void TestMethod(int a) 52 | { 53 | Console.WriteLine("calling method without optional parameter."); 54 | } 55 | 56 | private static void TestMethod(int a, int b = 0) 57 | { 58 | Console.WriteLine("calling method with optional parameter."); 59 | } 60 | 61 | static void Main(string[] args) 62 | { 63 | //var someInt = Add(1, 2); 64 | //var someString = Add(1, 2); //how can the compiler know which one I mean? 65 | 66 | //the method without optional parameters will be called 67 | TestMethod(5); 68 | 69 | Console.ReadKey(); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /MethodOverridingVsMethodHiding/MethodOverridingVsMethodHiding.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MethodOverridingVsMethodHiding/Program.cs: -------------------------------------------------------------------------------- 1 |  2 | 3 | using System; 4 | 5 | namespace MethodOverridingVsMethodHiding 6 | { 7 | public class Animal 8 | { 9 | public string AsText() 10 | { 11 | return $"This is an animal of type: {GetDescription()}"; 12 | } 13 | 14 | public virtual string GetDescription() 15 | { 16 | return "generic animal"; 17 | } 18 | } 19 | 20 | public class Tiger : Animal 21 | { 22 | public override string GetDescription() 23 | { 24 | return "tiger, the king of Asia"; 25 | } 26 | } 27 | 28 | public class Lion : Animal 29 | { 30 | public new string GetDescription() 31 | { 32 | return "lion, the ruler of Africa"; 33 | } 34 | } 35 | 36 | class Program 37 | { 38 | static void Main(string[] args) 39 | { 40 | Animal genericAnimal = new Animal(); 41 | Console.WriteLine($"generic animal: {genericAnimal.AsText()}\n"); 42 | 43 | Animal tiger = new Tiger(); 44 | Console.WriteLine($"tiger: {tiger.AsText()}\n"); 45 | 46 | Animal lion = new Lion(); 47 | Console.WriteLine($"lion: {lion.AsText()}\n"); 48 | 49 | 50 | Tiger tiger2 = new Tiger(); 51 | Console.WriteLine($"tiger description: {tiger.GetDescription()}\n"); 52 | Console.WriteLine($"tiger2 description: {tiger2.GetDescription()}\n"); 53 | 54 | Lion lion2 = new Lion(); 55 | Console.WriteLine($"lion description: {lion.GetDescription()}\n"); 56 | Console.WriteLine($"lion2 description: {lion2.GetDescription()}\n"); 57 | 58 | Circle circle = new Circle(1); 59 | Circle smartCircle = new SmartCircle(2); 60 | SmartCircle smartCircle2 = new SmartCircle(3); 61 | Console.WriteLine(circle.Draw()); 62 | Console.WriteLine(smartCircle.Draw()); 63 | Console.WriteLine(smartCircle2.Draw()); 64 | 65 | Console.ReadKey(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /MethodOverridingVsMethodHiding/Shapes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MethodOverridingVsMethodHiding 4 | { 5 | public class Circle //imagine this is defined in an external library that you use 6 | { 7 | public double Radius { get; } 8 | public Circle(double radius) 9 | { 10 | Radius = radius; 11 | } 12 | 13 | public double Circumference() 14 | { 15 | return 2 * Math.PI * Radius; 16 | } 17 | 18 | public double Area() 19 | { 20 | return Math.PI * Radius * Radius; 21 | } 22 | 23 | public virtual string Draw() 24 | { 25 | return @"O"; 26 | } 27 | } 28 | 29 | public class SmartCircle : Circle 30 | { 31 | public SmartCircle(double radius) : base(radius) 32 | { 33 | } 34 | 35 | public double Area() 36 | { 37 | return Math.PI * Radius * Radius; 38 | } 39 | 40 | public new string Draw() 41 | { 42 | return @" 43 | * * 44 | * * 45 | * * 46 | * * 47 | * * 48 | * * "; 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /MultipleInheritance/MultipleInheritance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /MultipleInheritance/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MultipleInheritance 4 | { 5 | class WithMultipleInheritance 6 | { 7 | public abstract class Animal 8 | { 9 | public abstract string MakeSound(); 10 | } 11 | 12 | public class HousePet : Animal 13 | { 14 | public override string MakeSound() 15 | { 16 | return ""; 17 | } 18 | } 19 | 20 | public class Feline : Animal 21 | { 22 | public override string MakeSound() 23 | { 24 | return "Purr purr, I'm a ball of fur"; 25 | } 26 | } 27 | 28 | //does not compile, we can't derive from two base classes 29 | //public class DomesticCat : HousePet, Feline 30 | //{ 31 | 32 | //} 33 | } 34 | 35 | 36 | public interface IAnimal 37 | { 38 | string MakeSound(); 39 | } 40 | 41 | public interface IHousePet : IAnimal 42 | { 43 | } 44 | 45 | public interface IFeline : IAnimal 46 | { 47 | } 48 | 49 | public class HousePet : IHousePet 50 | { 51 | public string MakeSound() 52 | { 53 | return ""; 54 | } 55 | } 56 | 57 | public class Feline : IFeline 58 | { 59 | public string MakeSound() 60 | { 61 | return "Purr purr, I'm a ball of fur"; 62 | } 63 | } 64 | 65 | public class DomesticCat : IFeline, IHousePet 66 | { 67 | public string MakeSound() 68 | { 69 | return "Purr purr, I'm a ball of fur, " + 70 | "but I am not too excited when human comes back home."; 71 | } 72 | } 73 | 74 | class Program 75 | { 76 | static void Main(string[] args) 77 | { 78 | //multiple inheritance 79 | //how can CLR know which method call - the one from Feline or HousePet? 80 | //var domesticCat = new WithMultipleInheritance.DomesticCat(); 81 | //Console.WriteLine(domesticCat.MakeSound()); 82 | 83 | //multiple interfaces implementation 84 | 85 | DomesticCat domesticCat = new DomesticCat(); 86 | Console.WriteLine(domesticCat.MakeSound()); 87 | 88 | IAnimal domesticCatAsAnimal = new DomesticCat(); 89 | Console.WriteLine(domesticCatAsAnimal.MakeSound()); 90 | 91 | IHousePet domesticCatAsHousePet = new DomesticCat(); 92 | Console.WriteLine(domesticCatAsHousePet.MakeSound()); 93 | 94 | IFeline domesticCatAsFeline = new DomesticCat(); 95 | Console.WriteLine(domesticCatAsFeline.MakeSound()); 96 | 97 | Console.ReadKey(); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /NewKeyword/NewKeyword.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /NewKeyword/NewOperator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace NewKeyword 4 | { 5 | class NewOperator 6 | { 7 | public static void CreateSomeObjects() 8 | { 9 | //new operator 10 | var rachel = new Person("Rachel", 34); //constructor call 11 | 12 | Person john = new("John", 19); //C# 9 style 13 | 14 | var steve = new Person { Name = "Steve", Age = 45 }; //object initialization 15 | 16 | var currencies = new Dictionary //collection initialization 17 | { 18 | ["USA"] = "USD", 19 | ["Great Britain"] = "GBP" 20 | }; 21 | 22 | var numbers = new int[4]; //array creation 23 | 24 | var person = new { Name = "Anna", Age = 55 }; //anonymous type 25 | } 26 | } 27 | 28 | class Person 29 | { 30 | public string Name; 31 | public int Age; 32 | 33 | public Person() 34 | { 35 | 36 | } 37 | 38 | public Person(string name, int age) 39 | { 40 | this.Name = name; 41 | this.Age = age; 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /NewKeyword/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace NewKeyword 5 | { 6 | class Program 7 | { 8 | static void Main(string[] args) 9 | { 10 | var lazyList = new NewConstraint.LazyInitializer>(); 11 | //Pet class doesn't have parameterless constructor 12 | //var lazyPet = new NewConstraint.LazyInitializer(); 13 | 14 | Console.ReadKey(); 15 | } 16 | } 17 | 18 | class NewConstraint 19 | { 20 | //without this constraint, the "new T()" below would not work 21 | public class LazyInitializer where T: new() 22 | { 23 | private T value; 24 | 25 | public T Get() 26 | { 27 | if(value == null) 28 | { 29 | value = new T(); 30 | } 31 | return value; 32 | } 33 | } 34 | } 35 | 36 | class Pet 37 | { 38 | string Name; 39 | 40 | public Pet(string name) 41 | { 42 | Name = name; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /NullOperators/NullOperators.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /NullOperators/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace NullOperators 5 | { 6 | class Greeter 7 | { 8 | public static string Greet(string name) 9 | { 10 | return $"Hello, {name ?? "Stranger"}!"; 11 | } 12 | } 13 | 14 | static class ListExtensions 15 | { 16 | public static int GetAtIndex(this List numbers, int index) 17 | { 18 | return numbers?[index] ?? 19 | throw new ArgumentException($"Index {index} not found " + 20 | $"in the list or value is null."); 21 | } 22 | 23 | public static void ClearIfNotNull(this List numbers) 24 | { 25 | numbers?.Clear(); 26 | } 27 | } 28 | 29 | class Program 30 | { 31 | private static List _numbers; 32 | 33 | static void Main(string[] args) 34 | { 35 | (_numbers ??= new List()).Add(5); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Nullable/Nullable.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Nullable/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Nullable 6 | { 7 | class NullableSample 8 | { 9 | //this doesnt work because string is already nullable (as reference type) 10 | // private Nullable nullableString; 11 | 12 | private Nullable nullableInt; //default is null 13 | private int? alsoNullableInt; //shorter syntax 14 | private int nonNullableInt; //default is zero 15 | 16 | public void SomeMethod() 17 | { 18 | nullableInt = null; 19 | //nonNullableInt = null; //we can't assign null to non-nullable int 20 | nullableInt = 5; 21 | nonNullableInt = 5; 22 | 23 | if(nullableInt.HasValue) 24 | { 25 | Console.WriteLine($"Value is {nullableInt.Value}"); 26 | } 27 | else 28 | { 29 | Console.WriteLine("Value is not present"); 30 | } 31 | } 32 | } 33 | 34 | class Program 35 | { 36 | static void Main(string[] args) 37 | { 38 | var people = new List 39 | { 40 | new Person("Jon", 160), 41 | new Person("Anna",-1), 42 | new Person("Monica",185), 43 | new Person("Sebastian",-1), 44 | new Person("Alice",170), 45 | }; 46 | 47 | Console.WriteLine($"Average height is {people.Average(person => person.Height)}"); 48 | 49 | var peopleWithNullableHeight = new List 50 | { 51 | new PersonWithNullableHeight("Jon", 160), 52 | new PersonWithNullableHeight("Anna"), 53 | new PersonWithNullableHeight("Monica", 185), 54 | new PersonWithNullableHeight("Sebastian"), 55 | new PersonWithNullableHeight("Alice", 170), 56 | }; 57 | 58 | Console.WriteLine($"[NULLABLE] Average height is {peopleWithNullableHeight.Average(person => person.Height)}"); 59 | 60 | Console.ReadKey(); 61 | } 62 | } 63 | 64 | class Person 65 | { 66 | public int Height; 67 | public string Name; 68 | 69 | public Person(string name, int height) 70 | { 71 | Name = name; 72 | Height = height; 73 | } 74 | } 75 | 76 | class PersonWithNullableHeight 77 | { 78 | public int? Height; 79 | 80 | public string Name; 81 | 82 | public PersonWithNullableHeight(string name) 83 | { 84 | Name = name; 85 | Height = null; 86 | } 87 | 88 | public PersonWithNullableHeight(string name, int height) 89 | { 90 | Name = name; 91 | Height = height; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /OpenClosedPrinciple/OpenClosedPrinciple.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /OpenClosedPrinciple/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OpenClosedPrinciple 4 | { 5 | public interface IIceCreamFactory 6 | { 7 | IceCream Create(IceCreamType iceCreamType); 8 | } 9 | 10 | public class IceCreamFactory : IIceCreamFactory 11 | { 12 | public IceCream Create(IceCreamType iceCreamType) 13 | { 14 | switch (iceCreamType) 15 | { 16 | case IceCreamType.Vanilla: 17 | return new IceCream(IceCreamType.Vanilla, 18 | new[] { "Cream", "Sugar", "Vanilla" }); 19 | case IceCreamType.Chocolate: 20 | return new IceCream(IceCreamType.Chocolate, 21 | new[] { "Cream", "Sugar", "Chocolate" }); 22 | case IceCreamType.Strawberry: 23 | return new IceCream(IceCreamType.Strawberry, 24 | new[] { "Sugar", "Strawberry", "Coconut Cream" }); 25 | default: 26 | throw new ArgumentException( 27 | $"Invalid type of ice cream: {iceCreamType}"); 28 | } 29 | } 30 | } 31 | 32 | public enum IceCreamType 33 | { 34 | Vanilla, 35 | Chocolate, 36 | Strawberry 37 | } 38 | 39 | public class IceCream 40 | { 41 | public IceCreamType IceCreamType { get; } 42 | public string[] Ingredients { get; } 43 | 44 | public IceCream(IceCreamType iceCreamType, string[] ingredients) 45 | { 46 | IceCreamType = iceCreamType; 47 | Ingredients = ingredients; 48 | } 49 | 50 | public override string ToString() 51 | { 52 | return IceCreamType.ToString(); 53 | } 54 | } 55 | 56 | public class RandomIceCreamGenerator 57 | { 58 | private Random _random = new Random(); 59 | private readonly IIceCreamFactory _iceCreamFactory; 60 | 61 | public RandomIceCreamGenerator(IIceCreamFactory iceCreamFactory) 62 | { 63 | _iceCreamFactory = iceCreamFactory; 64 | } 65 | 66 | public IceCream Generate() 67 | { 68 | var randomType = GetRandomIceCreamType(); 69 | return _iceCreamFactory.Create(randomType); 70 | } 71 | 72 | private IceCreamType GetRandomIceCreamType() 73 | { 74 | var values = Enum.GetValues(typeof(IceCreamType)); 75 | 76 | return (IceCreamType)values.GetValue(_random.Next(values.Length)); 77 | } 78 | } 79 | 80 | class Program 81 | { 82 | static void Main(string[] args) 83 | { 84 | Console.WriteLine( 85 | $"Triangle of base 10, height 5. Area is: {new AreaCalculator().Calculate(new Triangle(10, 5))}"); 86 | 87 | var randomIceCreamGenerator = new RandomIceCreamGenerator(new IceCreamFactory()); 88 | for (int i = 0; i < 10; i++) 89 | { 90 | Console.WriteLine($"Random ice cream: {randomIceCreamGenerator.Generate()}"); 91 | } 92 | Console.ReadKey(); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /OpenClosedPrinciple/Shapes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace OpenClosedPrinciple 4 | { 5 | public interface IShape 6 | { 7 | double CalculateArea(); 8 | } 9 | 10 | public class Circle : IShape 11 | { 12 | public double Radius { get; } 13 | 14 | public Circle(double radius) 15 | { 16 | Radius = radius; 17 | } 18 | 19 | public double CalculateArea() 20 | { 21 | return Math.PI * Radius * Radius; 22 | } 23 | } 24 | 25 | public class Triangle : IShape 26 | { 27 | public double Base { get; } 28 | public double Height { get; } 29 | 30 | public Triangle(double @base, double height) 31 | { 32 | Base = @base; 33 | Height = height; 34 | } 35 | 36 | public double CalculateArea() 37 | { 38 | return Base * Height / 2.0; 39 | } 40 | } 41 | 42 | public class Square : IShape 43 | { 44 | public double Side { get; } 45 | 46 | public Square(double side) 47 | { 48 | Side = side; 49 | } 50 | 51 | public double CalculateArea() 52 | { 53 | return Side * Side; 54 | } 55 | } 56 | 57 | public class AreaCalculator 58 | { 59 | public double Calculate(IShape shape) 60 | { 61 | return shape.CalculateArea(); 62 | } 63 | } 64 | 65 | //that breaks Open-Closed principle 66 | //public class AreaCalculator 67 | //{ 68 | // public double Calculate(Circle circle) 69 | // { 70 | // return Math.PI * circle.Radius * circle.Radius; 71 | // } 72 | 73 | // public double Calculate(Triangle triangle) 74 | // { 75 | // return triangle.Base * triangle.Height / 2.0; 76 | // } 77 | 78 | // public double Calculate(Square square) 79 | // { 80 | // return square.Side * square.Side; 81 | // } 82 | //} 83 | } 84 | -------------------------------------------------------------------------------- /Params/Params.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Params/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Params 4 | { 5 | class Program 6 | { 7 | //this will not compile - params keyword can only be applied to the last parameter 8 | //static void SomeFunction(params int[] numbers, int multiplier, params[] otherNumber) 9 | //{ 10 | 11 | //} 12 | 13 | //this will not compile - params keyword must be used with single-dimensional array 14 | //static void SomeFunction(params List numbers) 15 | //{ 16 | 17 | //} 18 | 19 | static int ClumsySum(int[] numbers) 20 | { 21 | var sum = 0; 22 | foreach (var number in numbers) 23 | { 24 | sum += number; 25 | } 26 | return sum; 27 | } 28 | 29 | static int Sum(params int[] numbers) 30 | { 31 | var sum = 0; 32 | foreach (var number in numbers) 33 | { 34 | sum += number; 35 | } 36 | return sum; 37 | } 38 | 39 | static void Main(string[] args) 40 | { 41 | Console.WriteLine("Hello!"); 42 | 43 | ClumsySum(new[] { 1, 2 }); 44 | ClumsySum(new[] { 1, 2, 3, 4 }); 45 | ClumsySum(new int[] { }); 46 | 47 | //the below definitely looks better than the above 48 | Sum(1, 2); 49 | Sum(1, 2, 3, 4); 50 | Sum(); 51 | 52 | Console.ReadKey(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /PartialClasses/DuckPartOne.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PartialClasses 4 | { 5 | partial class Duck 6 | { 7 | private void Quack() 8 | { 9 | Console.WriteLine("Quack, quack, I'm a duck"); 10 | } 11 | 12 | public partial void Fly(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /PartialClasses/DuckPartTwo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PartialClasses 4 | { 5 | partial class Duck 6 | { 7 | private void Swim() 8 | { 9 | Console.WriteLine("Swimming in a pond"); 10 | } 11 | 12 | public partial void Fly() 13 | { 14 | Console.WriteLine("Flying high in the sky"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /PartialClasses/PartialClasses.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /PartialClasses/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PartialClasses 4 | { 5 | //the below class is exactly the same as the 6 | //class defined in two parts in files 7 | //DuckPartOne.cs and DuckPartTwo.cs 8 | //after compilation both parts will be merged together 9 | 10 | //class Duck 11 | //{ 12 | // private void Quack() 13 | // { 14 | // Console.WriteLine("Quack, quack, I'm a duck"); 15 | // } 16 | 17 | // public void Fly() 18 | // { 19 | // Console.WriteLine("Flying high in the sky"); 20 | // } 21 | //} 22 | 23 | class Program 24 | { 25 | static void Main(string[] args) 26 | { 27 | Console.WriteLine("Hello!"); 28 | Console.ReadKey(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Polymorphism/Polymorphism.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /Polymorphism/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Polymorphism 5 | { 6 | interface IFlyable 7 | { 8 | void Fly(); 9 | } 10 | 11 | class Duck : IFlyable 12 | { 13 | public void Fly() 14 | { 15 | Console.WriteLine("Flying by flapping the wings"); 16 | } 17 | } 18 | 19 | class Plane : IFlyable 20 | { 21 | public void Fly() 22 | { 23 | Console.WriteLine("Flying by jet propulsion"); 24 | } 25 | } 26 | 27 | class Kite : IFlyable 28 | { 29 | public void Fly() 30 | { 31 | Console.WriteLine("Flying by being carried by the wind"); 32 | } 33 | } 34 | 35 | class Program 36 | { 37 | static void Main(string[] args) 38 | { 39 | List flyables = new List 40 | { 41 | new Duck(), 42 | new Plane(), 43 | new Kite() 44 | }; 45 | 46 | FlyAll(flyables); 47 | 48 | Console.ReadKey(); 49 | } 50 | 51 | private static void FlyAll(List flyables) 52 | { 53 | foreach (var flyable in flyables) 54 | { 55 | flyable.Fly(); 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Properties/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Properties 4 | { 5 | class Person 6 | { 7 | public int YearOfBirth 8 | { 9 | get 10 | { 11 | return _dateOfBirth.Year; 12 | } 13 | set 14 | { 15 | _dateOfBirth = new DateTime(value, _dateOfBirth.Month, _dateOfBirth.Day); 16 | } 17 | } 18 | 19 | private DateTime _dateOfBirth; 20 | public DateTime DateOfBirth 21 | { 22 | get 23 | { 24 | return _dateOfBirth; 25 | } 26 | set 27 | { 28 | if (value < DateTime.Now) //we don't allow dates from the future 29 | { 30 | _dateOfBirth = value; 31 | } 32 | } 33 | } 34 | 35 | public DateTime? DateOfDeath { get; set; } 36 | 37 | public int? LengthOfLife 38 | { 39 | get 40 | { 41 | return DateOfDeath == null ? 42 | null : 43 | (int)((DateOfDeath.Value - DateOfBirth).TotalDays / 365); 44 | } 45 | } 46 | 47 | public string Name { get; } 48 | 49 | public Person(int yearOfBirth, string lastName, string name) 50 | { 51 | YearOfBirth = yearOfBirth; 52 | LastName = lastName; 53 | Name = name; 54 | } 55 | 56 | private void SetName(string name) 57 | { 58 | //Name = name; //does not work since Name is readonly 59 | } 60 | 61 | 62 | public string LastName { get; private set; } 63 | 64 | private void SetLastName(string lastName) 65 | { 66 | //it's ok - we can change this private property within the Person class 67 | LastName = lastName; 68 | } 69 | } 70 | 71 | 72 | class Program 73 | { 74 | static void Main(string[] args) 75 | { 76 | var person = new Person(1950, "Smith", "John"); 77 | //we can't do that because the setter is private 78 | //person.LastName = "Swanson"; 79 | 80 | Console.ReadKey(); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Properties/Properties.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 50InterviewQuestionsJunior 2 | 3 | Hello! 4 | 5 | My name is Krystyna and welcome to my GitHub. 6 | 7 | This repository is part of my "C#/.NET - 50 Essential Interview Questions (Junior Level)" course, which you can find under this link: https://bit.ly/3hSRpOq 8 | 9 | ## FAQ: 10 | 11 | ### Q1: How do I download the files? 12 | A: If you're not familiar with GitHub and just want to download the entire solution, click the green button saying "Code", and then select the "Download ZIP". 13 | 14 | -------------------------------------------------------------------------------- /RefAndOut/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace RefAndOut 4 | { 5 | class Program 6 | { 7 | private static void AddOne(int a) 8 | { 9 | Console.WriteLine($"Calling AddOne function"); 10 | ++a; 11 | } 12 | 13 | private static void AddOneByRef(ref int a) 14 | { 15 | Console.WriteLine($"Calling AddOneByRef function"); 16 | ++a; 17 | } 18 | 19 | private static bool TryParseToInt(string input, out int result) 20 | { 21 | try 22 | { 23 | result = int.Parse(input); 24 | return true; 25 | } 26 | catch 27 | { 28 | result = 0; 29 | return false; 30 | } 31 | } 32 | 33 | //private static bool TryParseToDouble(string input, out double result) 34 | //{ 35 | // try 36 | // { 37 | // result = double.Parse(input); 38 | // return true; 39 | // } 40 | // catch 41 | // { 42 | // //this will not compile because result out parameter MUST be assigned a value in this function 43 | // return false; 44 | // } 45 | //} 46 | 47 | static void Main(string[] args) 48 | { 49 | Console.WriteLine("---REF---\n"); 50 | 51 | var number = 10; 52 | Console.WriteLine($"number is {number}"); 53 | AddOne(number); 54 | Console.WriteLine($"number is still {number}\n"); 55 | 56 | AddOneByRef(ref number); 57 | Console.WriteLine($"now number is {number}"); 58 | 59 | int uninitializedValue; 60 | //AddOneByRef(ref uninitializedValue); //this will not compile as value passed with ref must be initialized first 61 | 62 | Console.WriteLine(); 63 | Console.WriteLine("---OUT---"); 64 | var validInput = "1"; 65 | 66 | bool wasParsingSuccessful = TryParseToInt(validInput, out int result); 67 | 68 | int variableForResult; 69 | bool wasParsingSuccessful2 = TryParseToInt(validInput, out variableForResult); 70 | 71 | PrintParsingResult(validInput, wasParsingSuccessful, result); 72 | 73 | 74 | var invalidInput = "rubbish"; 75 | bool wasParsingOfRubbishSuccessful = TryParseToInt(invalidInput, out int rubbishResult); 76 | PrintParsingResult(invalidInput, wasParsingOfRubbishSuccessful, rubbishResult); 77 | 78 | Console.ReadKey(); 79 | } 80 | 81 | private static void PrintParsingResult(string input, bool wasParsingSuccessful, int result) 82 | { 83 | if (wasParsingSuccessful) 84 | { 85 | Console.WriteLine($"Successfully parsed \"{input}\" to int {result}"); 86 | } 87 | else 88 | { 89 | Console.WriteLine($"Could not parse \"{input}\" to int, result is {result}"); 90 | } 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /RefAndOut/RefAndOut.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SealedModifier/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SealedModifier 4 | { 5 | public class Base 6 | { 7 | public virtual void DoSomething() 8 | { 9 | Console.WriteLine("Base class"); 10 | } 11 | } 12 | 13 | public class Derived : Base 14 | { 15 | public override sealed void DoSomething() //we are sealing this method 16 | { 17 | Console.WriteLine("Derived class"); 18 | } 19 | } 20 | 21 | public class DerivedFromDerived : Derived 22 | { 23 | //does not compile - this method was sealed in Derived class 24 | //public override void DoSomething() 25 | //{ 26 | //} 27 | } 28 | 29 | public sealed class SealedBase 30 | { 31 | 32 | } 33 | 34 | //does not compile - can't derive from sealed class 35 | //public class DerivedFromSealed : SealedBase 36 | //{ 37 | //} 38 | 39 | 40 | class Program 41 | { 42 | static void Main(string[] args) 43 | { 44 | Console.WriteLine("Hello!"); 45 | Console.ReadKey(); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /SealedModifier/SealedModifier.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SingleResponsibilityPrinciple/PeopleInformationPrinter.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.SqlClient; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | namespace SingleResponsibilityPrinciple 7 | { 8 | public class PeopleInformationPrinter 9 | { 10 | private readonly IReader _reader; 11 | private readonly IPeopleTextFormatter _peopleTextFormatter; 12 | private readonly IWriter _writer; 13 | 14 | public void Print() 15 | { 16 | var people = _reader.Read(); 17 | var text = _peopleTextFormatter.BuildText(people); 18 | _writer.Write(text); 19 | } 20 | } 21 | 22 | public class DatabaseReader : IReader 23 | { 24 | private readonly string _connectionString; 25 | public DatabaseReader(string connectionString) 26 | { 27 | _connectionString = connectionString; 28 | } 29 | 30 | public IEnumerable Read() 31 | { 32 | var people = new List(); 33 | using (var connection = new SqlConnection(_connectionString)) 34 | { 35 | using (var command = new SqlCommand("select * from People", connection)) 36 | { 37 | connection.Open(); 38 | using (SqlDataReader reader = command.ExecuteReader()) 39 | { 40 | while (reader.Read()) 41 | { 42 | people.Add( 43 | new Person( 44 | reader["Name"] as string, 45 | reader["LastName"] as string, 46 | (int)reader["Username"])); 47 | } 48 | } 49 | } 50 | } 51 | return people; 52 | } 53 | } 54 | 55 | public interface IPeopleTextFormatter 56 | { 57 | string BuildText(IEnumerable people); 58 | } 59 | 60 | public class PeopleTextFormatter : IPeopleTextFormatter 61 | { 62 | public string BuildText(IEnumerable people) 63 | { 64 | return string.Join("\n", people.Select(person => person.ToString())); 65 | } 66 | } 67 | 68 | public class TextWriter : IWriter 69 | { 70 | private readonly string _filePath; 71 | 72 | public TextWriter(string resultFilePath) 73 | { 74 | _filePath = resultFilePath; 75 | } 76 | 77 | public void Write(string text) 78 | { 79 | File.WriteAllText(_filePath, text); 80 | } 81 | } 82 | 83 | public interface IReader 84 | { 85 | IEnumerable Read(); 86 | } 87 | 88 | public interface IWriter 89 | { 90 | void Write(string text); 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /SingleResponsibilityPrinciple/PeopleInformationPrinterBreakingSRP.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data.SqlClient; 3 | using System.IO; 4 | using System.Linq; 5 | 6 | namespace SingleResponsibilityPrinciple 7 | { 8 | public class PeopleInformationPrinterBreakingSRP 9 | { 10 | private readonly string _connectionString; 11 | private readonly string _resultFilePath; 12 | 13 | public PeopleInformationPrinterBreakingSRP(string connectionString, string resultFilePath) 14 | { 15 | _connectionString = connectionString; 16 | _resultFilePath = resultFilePath; 17 | } 18 | 19 | public void Print() 20 | { 21 | var people = ReadFromDatabase(); 22 | var text = BuildText(people); 23 | File.WriteAllText(_resultFilePath, text); 24 | } 25 | 26 | private IEnumerable ReadFromDatabase() 27 | { 28 | var people = new List(); 29 | using (var connection = new SqlConnection(_connectionString)) 30 | { 31 | using (var command = new SqlCommand("select * from People", connection)) 32 | { 33 | connection.Open(); 34 | using (SqlDataReader reader = command.ExecuteReader()) 35 | { 36 | while (reader.Read()) 37 | { 38 | people.Add( 39 | new Person( 40 | reader["Name"] as string, 41 | reader["LastName"] as string, 42 | (int)reader["Username"])); 43 | } 44 | } 45 | } 46 | } 47 | return people; 48 | } 49 | 50 | private string BuildText(IEnumerable people) 51 | { 52 | return string.Join("\n", people.Select(person => person.ToString())); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /SingleResponsibilityPrinciple/Person.cs: -------------------------------------------------------------------------------- 1 | namespace SingleResponsibilityPrinciple 2 | { 3 | public class Person 4 | { 5 | public string Name; 6 | public string LastName; 7 | public int YearOfBirth; 8 | 9 | public Person(string name, string lastName, int yearOfBirth) 10 | { 11 | Name = name; 12 | LastName = lastName; 13 | YearOfBirth = yearOfBirth; 14 | } 15 | 16 | public override string ToString() 17 | { 18 | return $"{Name} {LastName} born in {YearOfBirth}"; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /SingleResponsibilityPrinciple/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SingleResponsibilityPrinciple 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Console.WriteLine("Hello!"); 10 | Console.ReadKey(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SingleResponsibilityPrinciple/SingleResponsibilityPrinciple.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | 6 | Exe 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Singleton/Database.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton 2 | { 3 | internal class Database 4 | { 5 | private ILogger singleLoggerPerWholeApplication; 6 | 7 | public Database(ILogger singleLoggerPerWholeApplication) 8 | { 9 | this.singleLoggerPerWholeApplication = singleLoggerPerWholeApplication; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Singleton/ILogger.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton 2 | { 3 | public interface ILogger 4 | { 5 | } 6 | } -------------------------------------------------------------------------------- /Singleton/InterfaceHandler.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton 2 | { 3 | internal class InterfaceHandler 4 | { 5 | private ILogger singleLoggerPerWholeApplication; 6 | 7 | public InterfaceHandler(ILogger singleLoggerPerWholeApplication) 8 | { 9 | this.singleLoggerPerWholeApplication = singleLoggerPerWholeApplication; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Singleton/Logger.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton 2 | { 3 | public class Logger : ILogger 4 | { 5 | 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Singleton/NetworkConnector.cs: -------------------------------------------------------------------------------- 1 | namespace Singleton 2 | { 3 | internal class NetworkConnector 4 | { 5 | private ILogger singleLoggerPerWholeApplication; 6 | 7 | public NetworkConnector(ILogger singleLoggerPerWholeApplication) 8 | { 9 | this.singleLoggerPerWholeApplication = singleLoggerPerWholeApplication; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Singleton/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Singleton 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | //this is Singleton design pattern 10 | var singleton = Singleton.Instance; 11 | var nextSingleton = Singleton.Instance; 12 | Console.WriteLine($"singleton: {singleton.Id}"); 13 | Console.WriteLine($"nextSingleton: {nextSingleton.Id}"); 14 | 15 | //this is application singleton 16 | var singleLoggerPerWholeApplication = new Logger(); 17 | var database = new Database(singleLoggerPerWholeApplication); 18 | var networkConnector = new NetworkConnector(singleLoggerPerWholeApplication); 19 | var interfaceHandler = new InterfaceHandler(singleLoggerPerWholeApplication); 20 | 21 | RunApplication(database, networkConnector, interfaceHandler); 22 | 23 | Console.ReadKey(); 24 | } 25 | 26 | private static void RunApplication( 27 | Database database, 28 | NetworkConnector networkConnector, 29 | InterfaceHandler interfaceHandler) 30 | { 31 | //run application 32 | } 33 | } 34 | 35 | public sealed class Singleton 36 | { 37 | private static Singleton instance = null; 38 | public string Id { get; } //only for presentation purposes 39 | 40 | private Singleton() 41 | { 42 | Id = Guid.NewGuid().ToString(); 43 | } 44 | 45 | public static Singleton Instance 46 | { 47 | get 48 | { 49 | if (instance == null) 50 | { 51 | instance = new Singleton(); 52 | } 53 | return instance; 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Singleton/Singleton.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SpaghettiCode/ConsoleReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | static class ConsoleReader 6 | { 7 | public static double ReadDouble(string variableName) 8 | { 9 | double result; 10 | bool wasParsingSuccessful; 11 | do 12 | { 13 | Console.WriteLine($"Enter {variableName}"); 14 | var userInput = Console.ReadLine(); 15 | wasParsingSuccessful = double.TryParse(userInput, out result); 16 | if (!wasParsingSuccessful) 17 | { 18 | Console.WriteLine("Invalid format, please try again."); 19 | } 20 | } 21 | while (!wasParsingSuccessful); 22 | 23 | return result; 24 | } 25 | 26 | public static bool ReadBool(string question) 27 | { 28 | bool result = false; 29 | bool wasParsingSuccessful; 30 | Console.WriteLine($"{question} Enter Y or N"); 31 | do 32 | { 33 | var userInput = Console.ReadLine(); 34 | if (userInput == "Y") 35 | { 36 | result = false; 37 | wasParsingSuccessful = true; 38 | } 39 | else if (userInput == "N") 40 | { 41 | result = true; 42 | wasParsingSuccessful = true; 43 | } 44 | else 45 | { 46 | Console.WriteLine("Invalid format, please try again. Enter Y or N"); 47 | wasParsingSuccessful = false; 48 | } 49 | } 50 | while (!wasParsingSuccessful); 51 | 52 | return result; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /SpaghettiCode/IQuadraticFunctionRootsCalculator.cs: -------------------------------------------------------------------------------- 1 | namespace SpaghettiCode 2 | { 3 | interface IQuadraticFunctionRootsCalculator 4 | { 5 | void Calculate(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /SpaghettiCode/MathUtilities.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | static class MathUtilities 6 | { 7 | public static QuadraticFunctionRoots CalculateQuadraticFunctionRoots( 8 | double a, double b, double c) 9 | { 10 | var delta = b * b - 4 * a * c; 11 | if (delta > 0) 12 | { 13 | var firstRoot = (-b - Math.Sqrt(delta)) / (2 * a); 14 | var secondRoot = (-b + Math.Sqrt(delta)) / (2 * a); 15 | return new QuadraticFunctionRoots(firstRoot, secondRoot); 16 | } 17 | else if (delta == 0) 18 | { 19 | var onlyRoot = -b / (2 * a); 20 | return new QuadraticFunctionRoots(onlyRoot); 21 | } 22 | else 23 | { 24 | return new QuadraticFunctionRoots(); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /SpaghettiCode/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | //new SpaghettiQuadraticFunctionRootsCalculator().Calculate(); //this also works 10 | new QuadraticFunctionRootsCalculator().Calculate(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /SpaghettiCode/QuadraticFunctionRoots.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | class QuadraticFunctionRoots 6 | { 7 | public bool IsNone { get; } 8 | public bool IsOne { get; } 9 | public bool AreTwo { get; } 10 | 11 | public double FirstRoot 12 | { 13 | get 14 | { 15 | if (IsNone) 16 | { 17 | throw new InvalidOperationException( 18 | "There is zero quadratic function roots."); 19 | } 20 | return _firstRoot; 21 | } 22 | } 23 | 24 | public double SecondRoot 25 | { 26 | get 27 | { 28 | if (IsNone) 29 | { 30 | throw new InvalidOperationException( 31 | "There is zero quadratic function roots."); 32 | } 33 | if (IsOne) 34 | { 35 | throw new InvalidOperationException( 36 | "There is one quadratic function root."); 37 | } 38 | return _secondRoot; 39 | } 40 | } 41 | 42 | private readonly double _firstRoot; 43 | private readonly double _secondRoot; 44 | 45 | public QuadraticFunctionRoots() 46 | { 47 | IsNone = true; 48 | } 49 | 50 | public QuadraticFunctionRoots(double onlyRoot) 51 | { 52 | IsOne = true; 53 | _firstRoot = onlyRoot; 54 | } 55 | 56 | public QuadraticFunctionRoots(double firstRoot, double secondRoot) 57 | { 58 | AreTwo = true; 59 | _firstRoot = firstRoot; 60 | _secondRoot = secondRoot; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /SpaghettiCode/QuadraticFunctionRootsCalculator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | class QuadraticFunctionRootsCalculator : IQuadraticFunctionRootsCalculator 6 | { 7 | public void Calculate() 8 | { 9 | var isFinished = false; 10 | while (!isFinished) 11 | { 12 | Console.WriteLine("Quadratic Function: y = ax^2 + bx + c"); 13 | 14 | double a = ConsoleReader.ReadDouble("a"); 15 | double b = ConsoleReader.ReadDouble("b"); 16 | double c = ConsoleReader.ReadDouble("c"); 17 | 18 | var roots = MathUtilities.CalculateQuadraticFunctionRoots(a, b, c); 19 | if (roots.AreTwo) 20 | { 21 | Console.WriteLine($"Two roots: {roots.FirstRoot}, {roots.SecondRoot}"); 22 | } 23 | else if (roots.IsOne) 24 | { 25 | Console.WriteLine($"One root: {roots.FirstRoot}"); 26 | } 27 | else 28 | { 29 | Console.WriteLine("Zero roots."); 30 | } 31 | 32 | isFinished = ConsoleReader.ReadBool("Run calculation again?"); 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /SpaghettiCode/SpaghettiCode.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /SpaghettiCode/SpaghettiQuadraticFunctionRootsCalculator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SpaghettiCode 4 | { 5 | class SpaghettiQuadraticFunctionRootsCalculator : IQuadraticFunctionRootsCalculator 6 | { 7 | public void Calculate() 8 | { 9 | var f = false; //means "is finished" 10 | while (!f) 11 | { 12 | Console.WriteLine("Quadratic Function: y = ax^2 + bx + c"); 13 | 14 | //variables for a,b,c 15 | double a; 16 | string astr; 17 | double b; 18 | string bstr; 19 | double c; 20 | string cstr; 21 | do 22 | { 23 | //reading a 24 | Console.WriteLine("Enter a"); 25 | astr = Console.ReadLine(); 26 | if (!double.TryParse(astr, out a)) 27 | { 28 | Console.WriteLine("Invalid format, please try again."); 29 | } 30 | } 31 | while (!double.TryParse(astr, out a)); 32 | 33 | do 34 | { 35 | //reading b 36 | Console.WriteLine("Enter b"); 37 | bstr = Console.ReadLine(); 38 | if (!double.TryParse(bstr, out b)) 39 | { 40 | Console.WriteLine("Invalid format, please try again."); 41 | } 42 | } 43 | while (!double.TryParse(bstr, out b)); 44 | 45 | do 46 | { 47 | //reading c 48 | Console.WriteLine("Enter c"); 49 | cstr = Console.ReadLine(); 50 | if (!double.TryParse(cstr, out c)) 51 | { 52 | Console.WriteLine("Invalid format, please try again."); 53 | } 54 | } 55 | while (!double.TryParse(cstr, out c)); 56 | 57 | var d = b * b - 4 * a * c; 58 | if (d > 0) 59 | { 60 | Console.WriteLine("Two roots:"); 61 | Console.WriteLine((-b - Math.Sqrt(d)) / (2 * a)); 62 | Console.WriteLine((-b + Math.Sqrt(d)) / (2 * a)); 63 | } 64 | else 65 | { 66 | if (d == 0) 67 | { 68 | Console.WriteLine("One root:"); 69 | Console.WriteLine((-b + Math.Sqrt(d)) / (2 * a)); 70 | } 71 | else Console.WriteLine("Zero roots."); 72 | } 73 | 74 | 75 | 76 | 77 | bool ok; 78 | string b1str; 79 | Console.WriteLine("Run calculation again? Enter Y or N"); 80 | do 81 | { 82 | b1str = Console.ReadLine(); 83 | if (b1str == "Y") 84 | { 85 | f = false; 86 | ok = true; 87 | } 88 | else if (b1str == "N") 89 | { 90 | f = true; 91 | ok = true; 92 | } 93 | else { Console.WriteLine("Invalid format, please try again. Enter Y or N"); ok = false; } 94 | } 95 | while (!ok); 96 | 97 | } 98 | } } 99 | } 100 | -------------------------------------------------------------------------------- /StaticClass/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StaticClass 4 | { 5 | static class Calculator 6 | { 7 | public static double Add(double a, double b) 8 | { 9 | return a + b; 10 | } 11 | 12 | public static double Multiply(double a, double b) 13 | { 14 | return a * b; 15 | } 16 | 17 | public static double Power(double a) 18 | { 19 | return a * a; 20 | } 21 | } 22 | 23 | static class SystemMonitor 24 | { 25 | private static readonly DateTime _startTime; 26 | 27 | static SystemMonitor() 28 | { 29 | _startTime = DateTime.UtcNow; 30 | } 31 | 32 | public static string Report() 33 | { 34 | return $"Monitoring the system for " + 35 | $"{(DateTime.UtcNow - _startTime).TotalSeconds} seconds"; 36 | } 37 | } 38 | 39 | class Program 40 | { 41 | static void Main(string[] args) 42 | { 43 | // var calculator = new Calculator(); //this will not compile 44 | var sum = Calculator.Add(4, 5); 45 | 46 | Console.WriteLine(SystemMonitor.Report()); 47 | System.Threading.Thread.Sleep(1000); //wait one second 48 | Console.WriteLine(SystemMonitor.Report()); 49 | 50 | Console.ReadKey(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /StaticClass/StaticClass.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /StaticKeyword/Geometry.cs: -------------------------------------------------------------------------------- 1 | using static System.Math; 2 | 3 | namespace DotNet50JuniorInterviewQuestions 4 | { 5 | static class Geometry 6 | { 7 | public static double SquareArea(double side) 8 | { 9 | return Pow(side, 2); 10 | } 11 | 12 | public static double CircleArea(double radius) 13 | { 14 | return PI * Pow(radius, 2); 15 | } 16 | 17 | public static double EquilateralTriangleArea(double side) 18 | { 19 | return (Pow(side, 2) * Sqrt(3)) / 4d; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /StaticKeyword/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace StaticKeyword 5 | { 6 | class Box 7 | { 8 | public static int MaxCount = 50; 9 | 10 | private List elements = new List(); 11 | 12 | public void Add(string element) 13 | { 14 | if(elements.Count < MaxCount) 15 | { 16 | elements.Add(element); 17 | } 18 | } 19 | 20 | //can't be made static as it refers 21 | //to non-static elements field 22 | public int GetCurrentCount() 23 | { 24 | return elements.Count; 25 | } 26 | 27 | public static string FormatMaxCount() 28 | { 29 | return $"The max count for this Box is {MaxCount}"; 30 | } 31 | } 32 | 33 | class Program 34 | { 35 | static void Main(string[] args) 36 | { 37 | var box1 = new Box(); 38 | var box2 = new Box(); 39 | //var invalidMaxCount = box1.MaxCount; //this does not compile as we try to access static field on instance 40 | var maxCount = Box.MaxCount; 41 | var elementsCount = box2.GetCurrentCount(); 42 | var maxCountFormatted = Box.FormatMaxCount(); 43 | 44 | Console.ReadKey(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /StaticKeyword/StaticKeyword.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /TernaryOperator/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TernaryOperator 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | var dog = new Dog(15); 10 | 11 | //string size; 12 | //if (dog.Weight > 25) 13 | //{ 14 | // size = "big"; 15 | //} 16 | //else 17 | //{ 18 | // size = "small"; 19 | //} 20 | 21 | var size = dog.Weight > 25 ? "big" : "small"; 22 | 23 | Console.WriteLine($"The dog is {size}"); 24 | 25 | var moreSpecificSize = dog.Weight > 25 ? "big" : dog.Weight < 5 ? "small" : "medium"; 26 | 27 | Console.WriteLine($"The dog is {moreSpecificSize}"); 28 | 29 | //the below does not compile because there is no assignment here 30 | //dog.Weight > 100 ? 31 | // Console.WriteLine("Wow!") : 32 | // Console.WriteLine("It's a normal dog"); 33 | 34 | Console.ReadKey(); 35 | } 36 | } 37 | 38 | class Dog 39 | { 40 | public int Weight; 41 | 42 | public Dog(int weight) 43 | { 44 | Weight = weight; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /TernaryOperator/TernaryOperator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /TypesOfErrors/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace TypesOfErrors 6 | { 7 | 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | //compilation error - missing semicolon 13 | //var number = 5 14 | 15 | //the code below will execute without exception, but the result will not be as expected 16 | var sentence = MergeWords("A", "little", "duck", "swims", "in", "a", "pond"); 17 | Console.WriteLine(sentence); 18 | 19 | //the code below will produce a runtime error because we try to access first element of an empty list 20 | var list = new List(); 21 | var firstElement = list.First(); 22 | 23 | Console.ReadKey(); 24 | } 25 | 26 | private static object MergeWords(params string[] words) 27 | { 28 | return string.Join("", words); 29 | //should be: 30 | //return string.Join(" ", words); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /TypesOfErrors/TypesOfErrors.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ValueTypesAndReferenceTypes/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ValueTypesAndReferenceTypes 5 | { 6 | class Program 7 | { 8 | //does not compile - all value types are sealed 9 | //public class DerivedFromInt : int 10 | //{ 11 | 12 | //} 13 | 14 | public class SpecialList : List //this is fine, as List is a reference type 15 | { 16 | 17 | } 18 | 19 | static void Main(string[] args) 20 | { 21 | //int is a value type 22 | int a = 5; 23 | Console.WriteLine($"Number is {a}"); 24 | AddOne(a); 25 | Console.WriteLine($"Now number is {a}\n"); 26 | 27 | //List is a reference type 28 | var list = new List(); 29 | Console.WriteLine($"List contains {list.Count} elements"); 30 | AddOneToList(list); 31 | Console.WriteLine($"Now list contains {list.Count} elements\n"); 32 | 33 | //base types for value types and reference types 34 | Console.WriteLine($"int's base type is {typeof(int).BaseType}"); 35 | Console.WriteLine($"List's base type is {typeof(List).BaseType}\n"); 36 | 37 | //for value types, a copy is created on assignment 38 | int b = 10; 39 | int c = b; 40 | ++c; 41 | Console.WriteLine($"Number 'b' is {b}"); 42 | Console.WriteLine($"Number 'c' is {c}\n"); 43 | 44 | //for reference types, only the reference is copied 45 | //The variable points to the same object 46 | List listB = new List { 1, 2, 3 }; 47 | List listC = listB; 48 | listC.Add(4); 49 | Console.WriteLine($"listB contains {listB.Count} elements"); 50 | Console.WriteLine($"listC contains {listC.Count} elements\n"); 51 | 52 | Console.ReadKey(); 53 | } 54 | 55 | private static void AddOne(int number) 56 | { 57 | //this WILL NOT affect the variable passed to this method, as value types are passed by copy 58 | ++number; 59 | } 60 | 61 | private static void AddOneToList(List list) 62 | { 63 | //this WILL affect the variable passed to this method, as reference types are passed by reference 64 | list.Add(1); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /ValueTypesAndReferenceTypes/ValueTypesAndReferenceTypes.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /VirtualAndAbstractMethods/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | 5 | namespace VirtualAndAbstractMethods 6 | { 7 | class Program 8 | { 9 | public abstract class Printer 10 | { 11 | public abstract void Print(string text); 12 | } 13 | 14 | public class TextFilePrinter : Printer 15 | { 16 | public override void Print(string text) 17 | { 18 | File.WriteAllText("someFile.txt", text); 19 | } 20 | } 21 | 22 | public class PersonDataBuilder 23 | { 24 | public virtual string BuildPersonData(string name, string lastName, int yearOfBirth) 25 | { 26 | return $"{name} {lastName} was born in {yearOfBirth}"; 27 | } 28 | } 29 | 30 | public class EmbellishedPersonDataBuilder : PersonDataBuilder 31 | { 32 | public override string BuildPersonData(string name, string lastName, int yearOfBirth) 33 | { 34 | var prettyLine = "**――**――**――*****――**――**――**"; 35 | return $"{prettyLine}\n" + 36 | $"{base.BuildPersonData(name, lastName, yearOfBirth)}\n" + 37 | $"{prettyLine}"; 38 | } 39 | } 40 | 41 | static void Main(string[] args) 42 | { 43 | var personDataBuilders = new List 44 | { 45 | new PersonDataBuilder(), 46 | new EmbellishedPersonDataBuilder() 47 | }; 48 | 49 | foreach (var personDataBuilder in personDataBuilders) 50 | { 51 | Console.WriteLine($"{personDataBuilder.BuildPersonData("Jack", "Smith", 1798)}\n"); 52 | } 53 | 54 | Console.ReadKey(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /VirtualAndAbstractMethods/VirtualAndAbstractMethods.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | 7 | 8 | 9 | --------------------------------------------------------------------------------