├── .gitignore ├── 01. OOP-Defining-Classes ├── Constants │ ├── 1. Constants.csproj │ ├── Constants.cs │ ├── Constants.sln │ └── Constants.v11.suo ├── Constructors │ ├── 3. Constructors.csproj │ ├── 3. Constructors.csproj.user │ ├── AlarmClock.cs │ ├── Constructors.cs │ ├── Person.cs │ └── Point.cs ├── Defining-Classes-Demos.sln ├── Defining-Properties │ ├── 5. Defining Properties.csproj │ ├── 5. Defining Properties.csproj.user │ ├── DefiningProperties.cs │ ├── Rectangle.cs │ └── UserProfile.cs ├── DogMeeting │ ├── 2. Dog Meeting.csproj │ ├── 2. Dog Meeting.csproj.user │ ├── Dog.cs │ └── DogMeeting.cs ├── Keep-the-Object-State │ ├── 6. Protect the Object State.csproj │ ├── Person.cs │ └── ProtectTheObjectStateDemo.cs └── Methods │ ├── 4. Methods.csproj │ └── MethodsExample.cs ├── 02. Exception-Handling-Demos ├── CallStack │ ├── 3. CallStack.csproj │ ├── App.config │ ├── Primes.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── CustomExceptions │ ├── 8. CustomExceptions.csproj │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Tank.cs │ ├── TankBattle.cs │ └── TankException.cs ├── Exceptions-Demos.sln ├── ExceptionsProperties │ ├── 2. ExceptionsProperties.csproj │ └── ExceptionsProperties.cs ├── HandlingExceptions │ ├── 1. Handling Exceptions.csproj │ ├── 1. Handling Exceptions.csproj.user │ └── HandlingExceptions.cs ├── Read-Text-File │ ├── 7. Read-Text-File.csproj │ └── ReadTextFile.cs ├── StackTrace │ ├── 4. StackTrace.csproj │ ├── App.config │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── Sale.cs │ └── Sales.cs ├── ThrowingExceptions │ ├── 5. Throwing Exceptions.csproj │ ├── 5. Throwing Exceptions.csproj.user │ └── ThrowingExceptions.cs └── TryFinally │ ├── 6. Try - Finally.csproj │ ├── 6. Try - Finally.csproj.user │ └── TryFinallyExample.cs ├── 02. Static-Members-and-Namespaces-Demos ├── Class-Counter │ ├── 1. Class-Counter.csproj │ ├── ClassCounter.cs │ └── Person.cs ├── Indexers │ ├── 3. Indexers.csproj │ ├── BitArray32.cs │ └── BitArrayMain.cs ├── Matrices │ ├── App.config │ ├── Matrices.csproj │ ├── Program.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Namespaces │ ├── 5. Namespaces.csproj │ ├── 5. Namespaces.csproj.user │ ├── SoftUni.Data │ │ ├── Faculty.cs │ │ ├── Professor.cs │ │ ├── Specialty.cs │ │ └── Student.cs │ ├── SoftUni.UI │ │ ├── ProfessorAdminForm.cs │ │ ├── ProfessorAdminForm.resx │ │ ├── StudentAdminForm.cs │ │ └── StudentAdminForm.resx │ └── SoftUni │ │ └── AdministrationSystem.cs ├── Overloading-Operators │ ├── 4. Operators.csproj │ ├── Fraction.cs │ └── TestFractions.cs ├── Static-Members-Namespaces-Demos.sln └── StaticMembers │ ├── 2. Static-Members.csproj │ ├── 2. Static-Members.csproj.user │ └── SqrtPrecalculated.cs ├── 03. Inheritance-and-Abstraction-Demos ├── Abstract-Classes │ ├── 4. Abstract-Classes.csproj │ ├── AbstractClasses.cs │ ├── Animal.cs │ ├── Cat.cs │ ├── Cheetah.cs │ ├── Kitten.cs │ ├── Tomcat.cs │ └── Turtle.cs ├── Access-Modifiers │ ├── 2. Access-Modifiers.csproj │ ├── AccessModifiers.cs │ ├── Creature.cs │ ├── Dog.cs │ └── Mammal.cs ├── Class-Diagrams │ ├── 5. Class-Diagrams.csproj │ ├── Color.cs │ ├── FiguresClassDiagram.cd │ ├── FiguresExample.cs │ ├── FilledRectangle.cs │ ├── FilledSquare.cs │ ├── ISufraceCalculatable.cs │ ├── Point.cs │ ├── Rectangle.cs │ ├── Shape.cs │ └── Square.cs ├── Inheritance-and-Abstraction-Demos.sln ├── Interfaces-and-Implementation │ ├── 3. Interfaces-and-Implementation.csproj │ ├── Circle.cs │ ├── IMovable.cs │ ├── IResizable.cs │ ├── IShape.cs │ ├── PlayWithInterfaces.cs │ ├── Rectangle.cs │ └── Square.cs └── Simple-Inheritance │ ├── 1. Simple-Inheritance.csproj │ ├── Cat.cs │ ├── Dog.cs │ ├── Mammal.cs │ └── SimpleInheritance.cs ├── 04. Encapsulation-and-Polymorphism-Demos ├── Encapsulation-ReferenceTypes │ ├── App.config │ ├── EncapsulatingReferenceTypes.cs │ ├── Encapsulation-ReferenceTypes.csproj │ ├── Person.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Encapsulation-and-Polymorphism-Demos.sln ├── Encapsulation │ ├── Encapsulation.csproj │ ├── EncapsulationExample.cs │ └── Person.cs ├── Polymorphism │ ├── Circle.cs │ ├── ClassDiagram.cd │ ├── Figure.cs │ ├── Polymorphism.cs │ ├── Polymorphism.csproj │ ├── Rectangle.cs │ └── Square.cs └── Virtual-Methods │ ├── Circle.cs │ ├── Figure.cs │ ├── Line.cs │ ├── SpecialFigure.cs │ ├── Virtual-Methods.csproj │ ├── VirtualMethods.cs │ └── WrongFigure.cs ├── 06. OOP-Other-Types-in-OOP-Demos ├── AttributeValidation │ ├── 8. AttributeValidation.csproj │ ├── App.config │ ├── AttributeValidaiton.cs │ ├── Attributes │ │ └── RequiredAttribute.cs │ ├── Models │ │ ├── Warrior.cs │ │ └── Weapon.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Attributes │ ├── 6. Attributes.csproj │ ├── Properties │ │ └── AssemblyInfo.cs │ └── UsingAttributes.cs ├── CofeeSize │ ├── 2. CofeeSize.csproj │ └── CofeeSize.cs ├── Custom-Attributes │ ├── 7. Custom Attributes.csproj │ ├── 7. Custom Attributes.csproj.user │ └── CustomAttributesDemo.cs ├── CustomStackExample │ ├── 5.2 CustomStackExample.csproj │ ├── App.config │ ├── CustomStack.cs │ ├── CustomStackExample.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── DynamicType │ ├── 4. DynamicType.csproj │ ├── App.config │ ├── Dynamic.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── Enums │ ├── 1. Enums.csproj │ ├── DayOfWeek.cs │ └── EnumsExample.cs ├── Generic-Classes │ ├── 5. Generic Classes.csproj │ ├── GenericList.cs │ └── GenericListExample.cs ├── Generic-Methods │ ├── 5.1. Generic Methods.csproj │ ├── GenericMethod.cs │ └── Point.cs ├── Other-Types-in-OOP-Demos.sln ├── Other-Types-in-OOP-Demos.v12.suo ├── StructVsClass │ ├── 3.1 StructVsClass.csproj │ ├── App.config │ ├── ColorClass.cs │ ├── ColorStruct.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── StructClassComparison.cs └── Structures │ ├── 3. Structures.csproj │ ├── Color.cs │ ├── Edges.cs │ ├── Point.cs │ ├── Square.cs │ └── StructuresExample.cs ├── 07. OOP-Delegates-and-Events-Demos ├── Action-and-Func │ ├── 3. Action-and-Func.csproj │ └── ActionAndFunc.cs ├── AnonymousMethods │ ├── 4. Anonymous-Methods.csproj │ └── AnonymousMethods.cs ├── CustomLinqExtensions │ ├── 6. CustomLinqExtensions.csproj │ ├── App.config │ ├── CustomLinqExtensions.cs │ ├── Extensions.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Student.cs ├── Delegates-Example │ ├── 1. Delegates-Example.csproj │ └── DelegatesExample.cs ├── Delegates-and-Events-Demos.sln ├── EventHandlers │ ├── 8. EventHandler-Example.csproj │ ├── Button.cs │ └── ButtonExample.cs ├── Events │ ├── 7. Custom-Events-Example.csproj │ ├── TimeChangedEventArgs.cs │ ├── TimeChangedEventHandler.cs │ ├── Timer.cs │ └── TimerDemo.cs ├── EventsAndInterfaces │ ├── 8. EventsAndInterfaces.csproj │ ├── Button.cs │ ├── ButtonTest.cs │ ├── ClickEventHandler.cs │ └── IClickable.cs ├── MulticastDelegates │ ├── 2. Multicast-Delegates.csproj │ └── MultiDelegates.cs ├── Predicates │ ├── 5. Predicates-Example.csproj │ └── PredicatesExample.cs ├── UIApplication │ ├── 9. UI-Application.csproj │ ├── App.config │ ├── App.xaml │ ├── App.xaml.cs │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ ├── Resources.resx │ │ ├── Settings.Designer.cs │ │ └── Settings.settings │ └── site.html └── WallStreet │ ├── 8.1 WallStreet.csproj │ ├── App.config │ ├── IBM.cs │ ├── Program.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── Stock.cs │ └── StockChangedEventArgs.cs ├── 08. OOP-Workshop-RPG-Game ├── OOP-Workshop-2015-06-17.sln └── SuperRpgGame │ ├── App.config │ ├── Attributes │ └── EnemyAttribute.cs │ ├── Characters │ ├── Character.cs │ ├── Fairy.cs │ ├── Ninja.cs │ ├── Ork.cs │ ├── Player.cs │ ├── PlayerRace.cs │ └── Troll.cs │ ├── Engine │ └── SuperEngine.cs │ ├── Exceptions │ ├── NotEnoughBeerException.cs │ └── ObjectOutOfBoundsException.cs │ ├── GameObject.cs │ ├── HelpInfo.txt │ ├── Interfaces │ ├── IAttack.cs │ ├── ICharacter.cs │ ├── ICollect.cs │ ├── IDestroyable.cs │ ├── IHeal.cs │ ├── IInputReader.cs │ ├── IMoveable.cs │ ├── IPlayer.cs │ └── IRenderer.cs │ ├── Items │ ├── Beer.cs │ ├── BeerSize.cs │ ├── Item.cs │ └── ItemState.cs │ ├── Position.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── SuperRpgClassDiagram.cd │ ├── SuperRpgGame.csproj │ ├── SuperRpgGameApp.cs │ └── UI │ ├── ConsoleInputReader.cs │ └── ConsoleRenderer.cs ├── 09. SolidLogger-Complete ├── LoggerTests │ ├── App.config │ ├── JsonFormatter.cs │ ├── LoggerTests.csproj │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── SqlAppender.cs ├── SOLIDLogger │ ├── Appenders │ │ ├── Appender.cs │ │ ├── ConsoleAppender.cs │ │ └── FileAppender.cs │ ├── Formatters │ │ ├── SimpleFormatter.cs │ │ └── XmlFormatter.cs │ ├── Interfaces │ │ ├── IAppender.cs │ │ └── IFormatter.cs │ ├── Logger.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ReportLevel.cs │ └── SOLIDLogger.csproj ├── SolidLogger.sln └── WpfApplication │ ├── App.config │ ├── App.xaml │ ├── App.xaml.cs │ ├── ListBoxAppender.cs │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ ├── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings │ └── WpfApplication.csproj ├── 09. SolidLogger-Skeleton ├── SOLIDLogger │ ├── Logger.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ReportLevel.cs │ └── SOLIDLogger.csproj └── SolidLogger.sln ├── 10. OOP-Common-Type-System-Demos ├── Boxing-Unboxing-Primitive-Types │ ├── 07. Boxing-Unboxing-Primitive-Types.csproj │ └── BoxingUnboxing.cs ├── Boxing-and-Unboxing │ ├── 08. Boxing-and-Unboxing.csproj │ └── BoxingAndUnboxing.cs ├── CTS-Demos.sln ├── Implementing-IComparable │ ├── 04. Implementing-IComparable.csproj │ └── Implementing IComparable.cs ├── Implementing-IEnumerable │ ├── 05. Implementing-IEnumerable.csproj │ ├── ImplementingIEnumerable.cs │ ├── LinkedList.cs │ └── LinkedListEnumerator.cs ├── Is-and-as-Operators │ ├── 02. Is-and-As-Operators.csproj │ └── IsAndAsOperators.cs ├── Object-Cloning │ ├── 03. Object-Cloning.csproj │ ├── LinkedList.cs │ └── ObjectCloning.cs ├── Passing-Out-Parameters │ ├── 10. PassingOutParameters.csproj │ └── PassingOutParameters.cs ├── Passing-Ref-Parameters │ ├── 09. Passing-Ref-Parameters.csproj │ └── PassingRefParameters.cs ├── System.Object-Overloading │ ├── 01. System.Object-Overriding.csproj │ ├── OverloadingSystemObject.cs │ └── Student.cs └── Value-and-Reference-Types │ ├── 06. Value-and-Reference-Types.csproj │ └── ValueAndReferenceTypes.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /*.suo 2 | bin 3 | obj 4 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constants/Constants.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Constants 4 | { 5 | public const double PI = 3.1415926535897932385; 6 | public readonly double Size; 7 | 8 | public Constants(int size) 9 | { 10 | this.Size = size; // Cannot be further modified! 11 | } 12 | 13 | static void Main() 14 | { 15 | Console.WriteLine(Constants.PI); 16 | Constants c = new Constants(5); 17 | Console.WriteLine(c.Size); 18 | 19 | //c.Size = 10; // Compilation error: readonly field 20 | //Console.WriteLine(Constants.Size); // compile error 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constants/Constants.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Constants", "Constants.csproj", "{1192D5A3-652C-4F58-8DF8-CA2FDAC1530B}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Any CPU = Debug|Any CPU 9 | Release|Any CPU = Release|Any CPU 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {1192D5A3-652C-4F58-8DF8-CA2FDAC1530B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 13 | {1192D5A3-652C-4F58-8DF8-CA2FDAC1530B}.Debug|Any CPU.Build.0 = Debug|Any CPU 14 | {1192D5A3-652C-4F58-8DF8-CA2FDAC1530B}.Release|Any CPU.ActiveCfg = Release|Any CPU 15 | {1192D5A3-652C-4F58-8DF8-CA2FDAC1530B}.Release|Any CPU.Build.0 = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constants/Constants.v11.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SoftUni/Object-Oriented-Programming/5f728c83f0542dbd113de7484f981f172306d9e9/01. OOP-Defining-Classes/Constants/Constants.v11.suo -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constructors/3. Constructors.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | en-US 17 | false 18 | 19 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constructors/AlarmClock.cs: -------------------------------------------------------------------------------- 1 | public class AlarmClock 2 | { 3 | private int hours = 9; // Inline initialization 4 | private int minutes = 0; // Inline initialization 5 | 6 | // Parameterless constructor (intentionally left empty) 7 | public AlarmClock() 8 | { 9 | } 10 | 11 | // Constructor with parameters 12 | public AlarmClock(int hours, int minutes) 13 | { 14 | this.hours = hours; 15 | this.minutes = minutes; 16 | } 17 | 18 | public int Hours 19 | { 20 | get { return this.hours; } 21 | set { this.hours = value; } 22 | } 23 | 24 | public int Minutes 25 | { 26 | get { return this.minutes; } 27 | set { this.minutes = value; } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constructors/Constructors.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Constructors 4 | { 5 | static void Main() 6 | { 7 | Person anonymous = new Person(); 8 | Console.WriteLine("Person 1: name: {0}, age: {1}", 9 | anonymous.Name ?? "[unnamed]", anonymous.Age); 10 | 11 | Person peter = new Person("Peter", 19); 12 | Console.WriteLine("Person 2: name: {0}, age: {1}", 13 | peter.Name, peter.Age); 14 | Console.WriteLine(); 15 | 16 | AlarmClock defaultAlarm = new AlarmClock(); 17 | Console.WriteLine("Wake up! It's {0:D2}:{1:D2} o'clock.", 18 | defaultAlarm.Hours, defaultAlarm.Minutes); 19 | 20 | AlarmClock earlyAlarm = new AlarmClock(5, 15); 21 | Console.WriteLine("Wake up! It's {0:D2}:{1:D2} o'clock.", 22 | earlyAlarm.Hours, earlyAlarm.Minutes); 23 | Console.WriteLine(); 24 | 25 | Point centerPoint = new Point(); 26 | centerPoint.Name = "Center of the coordinate system"; 27 | Console.WriteLine("First point: ({0},{1}) has name '{2}'", 28 | centerPoint.XCoord, centerPoint.YCoord, centerPoint.Name); 29 | 30 | Point point77 = new Point(7,7); 31 | Console.WriteLine("Second point: ({0},{1}) has name '{2}'", 32 | point77.XCoord, point77.YCoord, point77.Name); 33 | Console.WriteLine(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constructors/Person.cs: -------------------------------------------------------------------------------- 1 | public class Person 2 | { 3 | private string name; 4 | private int age; 5 | 6 | // Default constructor 7 | public Person() 8 | { 9 | this.name = null; 10 | this.age = 0; 11 | } 12 | 13 | // Constructor with parameters 14 | public Person(string name, int age) 15 | { 16 | this.name = name; 17 | this.age = age; 18 | } 19 | 20 | public string Name 21 | { 22 | get { return this.name; } 23 | set { this.name = value; } 24 | } 25 | 26 | public int Age 27 | { 28 | get { return this.age; } 29 | set { this.age = value; } 30 | } 31 | } -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Constructors/Point.cs: -------------------------------------------------------------------------------- 1 | public class Point 2 | { 3 | private int xCoord; 4 | private int yCoord; 5 | private string name; 6 | 7 | public Point() : this(0, 0) // Reuse the constructor 8 | { 9 | } 10 | 11 | public Point(int xCoord, int yCoord) 12 | { 13 | this.xCoord = xCoord; 14 | this.yCoord = yCoord; 15 | this.name = string.Format( 16 | "Point({0},{1})", xCoord, yCoord); 17 | } 18 | 19 | public int XCoord 20 | { 21 | get { return this.xCoord; } 22 | set { this.xCoord = value; } 23 | } 24 | 25 | public int YCoord 26 | { 27 | get { return this.yCoord; } 28 | set { this.yCoord = value; } 29 | } 30 | 31 | public string Name 32 | { 33 | get { return this.name; } 34 | set { this.name = value; } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Defining-Properties/5. Defining Properties.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | en-US 11 | false 12 | ProjectFiles 13 | 14 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Defining-Properties/DefiningProperties.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class PropertiesExample 4 | { 5 | static void Main() 6 | { 7 | Rectangle firstRect = new Rectangle(5.0f, 2.3f); 8 | Console.WriteLine("Rectangle 1 is {0}x{1} and has area {2}", 9 | firstRect.Height, firstRect.Width, firstRect.Area); 10 | 11 | Rectangle secondRect2 = new Rectangle(3.0f, 4.0f); 12 | Console.WriteLine("Rectangle 2 is {0}x{1} and has area {2}", 13 | secondRect2.Height, secondRect2.Width, secondRect2.Area); 14 | 15 | // Can't set a value to readonly property! 16 | //secondRect.Area = 20.3f; 17 | 18 | Console.WriteLine(); 19 | 20 | UserProfile profile = new UserProfile(91112, "Steve", "Balmer"); 21 | Console.WriteLine(profile); 22 | 23 | // Can't assign a private set property! 24 | // profile.UserId = 12345; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Defining-Properties/Rectangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Rectangle 4 | { 5 | private float height; 6 | private float width; 7 | 8 | public Rectangle(float height, float width) 9 | { 10 | this.height = height; 11 | this.width = width; 12 | } 13 | 14 | public float Height 15 | { 16 | get { return height; } 17 | set { height = value; } 18 | } 19 | 20 | public float Width 21 | { 22 | get { return width; } 23 | set { width = value; } 24 | } 25 | 26 | public float Area 27 | { 28 | get 29 | { 30 | return height * width; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Defining-Properties/UserProfile.cs: -------------------------------------------------------------------------------- 1 | public class UserProfile 2 | { 3 | public int UserId { get; private set; } 4 | public string FirstName { get; private set; } 5 | public string LastName { get; private set; } 6 | 7 | public UserProfile(int userId, string firstName, string lastName) 8 | { 9 | this.UserId = userId; 10 | this.FirstName = firstName; 11 | this.LastName = lastName; 12 | } 13 | 14 | public override string ToString() 15 | { 16 | return string.Format("Id: {0}, First name: {1}, Last name: {2}", 17 | this.UserId, this.FirstName, this.LastName); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/DogMeeting/2. Dog Meeting.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | en-US 17 | false 18 | 19 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/DogMeeting/Dog.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Dog 4 | { 5 | private string name; 6 | private string breed; 7 | 8 | public Dog() 9 | { 10 | } 11 | 12 | public Dog(string name, string breed) 13 | { 14 | this.name = name; 15 | this.breed = breed; 16 | } 17 | 18 | public string Name 19 | { 20 | get { return this.name; } 21 | set { this.name = value; } 22 | } 23 | 24 | public string Breed 25 | { 26 | get { return this.breed; } 27 | set { this.breed = value; } 28 | } 29 | 30 | public void SayBau() 31 | { 32 | Console.WriteLine("{0} said: Bauuuuuu!", 33 | this.name ?? "[unnamed dog]"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/DogMeeting/DogMeeting.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class DogMeeting 4 | { 5 | static void Main() 6 | { 7 | Console.Write("Enter first dog's name: "); 8 | string dogName = Console.ReadLine(); 9 | Console.Write("Enter first dog's breed: "); 10 | string dogBreed = Console.ReadLine(); 11 | 12 | // Use the Dog constructor to assign name and breed 13 | Dog firstDog = new Dog(dogName, dogBreed); 14 | 15 | // Create a new dog using the parameterless constructor 16 | Dog secondDog = new Dog(); 17 | 18 | // Use properties to set name and breed 19 | Console.Write("Enter second dog's name: "); 20 | secondDog.Name = Console.ReadLine(); 21 | Console.Write("Enter second dog's breed: "); 22 | secondDog.Breed = Console.ReadLine(); 23 | 24 | // Create a Dog with no name and breed 25 | Dog thirdDog = new Dog(); 26 | 27 | // Save the dogs in an array 28 | Dog[] dogs = new Dog[] { firstDog, secondDog, thirdDog }; 29 | 30 | // Ask each of the dogs to bark 31 | foreach(Dog dog in dogs) 32 | { 33 | dog.SayBau(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Keep-the-Object-State/Person.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Person 4 | { 5 | private string name; 6 | private int age; 7 | 8 | public Person(string name, int age) 9 | { 10 | this.Name = name; 11 | this.Age = age; 12 | } 13 | 14 | public string Name 15 | { 16 | get { return this.name; } 17 | set 18 | { 19 | if (String.IsNullOrEmpty(value)) 20 | { 21 | throw new ArgumentException("Name cannot be empty!"); 22 | } 23 | if (value.Length < 2) 24 | { 25 | throw new ArgumentException("Name too short! It should be at least 2 letters"); 26 | } 27 | if (value.Length >= 50) 28 | { 29 | throw new ArgumentException("Name too long! It should be less than 50 letters"); 30 | } 31 | foreach (char ch in value) 32 | { 33 | if (!IsLetterAllowedInNames(ch)) 34 | { 35 | throw new ArgumentException("Invalid name! Use only letters, space and hyphen"); 36 | } 37 | } 38 | 39 | this.name = value; 40 | } 41 | } 42 | 43 | public int Age 44 | { 45 | get { return this.age; } 46 | set 47 | { 48 | if (value < 0 || value > 120) 49 | { 50 | throw new ArgumentException("Invalid age! It should be in the range [0...120]."); 51 | } 52 | this.age = value; 53 | } 54 | } 55 | 56 | private bool IsLetterAllowedInNames(char ch) 57 | { 58 | bool isAllowed = 59 | char.IsLetter(ch) || ch == '-' || ch == ' '; 60 | return isAllowed; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Keep-the-Object-State/ProtectTheObjectStateDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ProtectTheObjectStateDemo 4 | { 5 | static void Main() 6 | { 7 | Console.Write("Enter your name: "); 8 | string name = Console.ReadLine(); 9 | 10 | Console.Write("Enter your age: "); 11 | int age = int.Parse(Console.ReadLine()); 12 | 13 | try 14 | { 15 | Person person = new Person(name, age); 16 | Console.WriteLine("Hello, {0}!", person.Name); 17 | Console.WriteLine("Your age is {0}.", person.Age); 18 | } 19 | catch (Exception ex) 20 | { 21 | Console.WriteLine("Cannot create person object: " + ex); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /01. OOP-Defining-Classes/Methods/MethodsExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Point 4 | { 5 | private int xCoord; 6 | private int yCoord; 7 | 8 | public Point(int xCoord, int yCoord) 9 | { 10 | this.xCoord = xCoord; 11 | this.yCoord = yCoord; 12 | } 13 | 14 | public double CalcDistance(Point p) 15 | { 16 | return Math.Sqrt( 17 | (p.xCoord - this.xCoord) * (p.xCoord - this.xCoord) + 18 | (p.yCoord - this.yCoord) * (p.yCoord - this.yCoord)); 19 | } 20 | } 21 | 22 | class TestMethods 23 | { 24 | static void Main() 25 | { 26 | Point p1 = new Point(2, 3); 27 | Point p2 = new Point(3, 4); 28 | System.Console.WriteLine(p1.CalcDistance(p2)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CallStack/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CallStack/Primes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace CallStack 5 | { 6 | public class Primes 7 | { 8 | static void Main() 9 | { 10 | string[] numbersAsString = { "5", "16", "33", "10" }; 11 | 12 | int[] numbers = ParseStringCollection(numbersAsString); 13 | PrintPrimes(numbers); 14 | } 15 | 16 | static void PrintPrimes(int[] numbers) 17 | { 18 | List primes = new List(); 19 | for (int i = 0; i < numbers.Length; i++) 20 | { 21 | bool isPrime = IsPrime(numbers[i]); 22 | if (isPrime) 23 | { 24 | primes.Add(numbers[i]); 25 | } 26 | } 27 | 28 | Console.WriteLine(string.Join(" ", primes)); 29 | } 30 | 31 | static bool IsPrime(int num) 32 | { 33 | int sqrt = (int) Math.Sqrt(num); 34 | for (int i = 2; i < sqrt; i++) 35 | { 36 | if (num % i == 0) 37 | { 38 | return false; 39 | } 40 | } 41 | 42 | return true; 43 | } 44 | 45 | 46 | static int[] ParseStringCollection(string[] strings) 47 | { 48 | int[] numbers = new int[strings.Length]; 49 | for (int i = 0; i < numbers.Length; i++) 50 | { 51 | numbers[i] = int.Parse(strings[i]); 52 | } 53 | 54 | return numbers; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CallStack/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CallStack")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CallStack")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("27464118-3822-4fd8-b535-69cddd315c1d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CustomExceptions/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CustomExceptions/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CustomExceptions")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CustomExceptions")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5808b3fe-95b2-493d-b0af-ed9a0c7b366f")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CustomExceptions/Tank.cs: -------------------------------------------------------------------------------- 1 | namespace CustomExceptions 2 | { 3 | public class Tank 4 | { 5 | private const int ShellDamage = 40; 6 | 7 | public Tank(int health) 8 | { 9 | this.Health = 200; 10 | this.Shells = 3; 11 | } 12 | 13 | public int Health { get; set; } 14 | 15 | public int Shells { get; private set; } 16 | 17 | public void Shoot(Tank enemy) 18 | { 19 | if (this.Shells == 0) 20 | { 21 | throw new TankException("Not enough shells to shoot"); 22 | } 23 | 24 | enemy.Health -= ShellDamage; 25 | this.Shells--; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CustomExceptions/TankBattle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CustomExceptions 4 | { 5 | public class TankBattle 6 | { 7 | static void Main() 8 | { 9 | try 10 | { 11 | var germanTank = new Tank(300); 12 | var russianTank = new Tank(200); 13 | ExecuteAttack(germanTank, russianTank); 14 | } 15 | catch (TankException ex) 16 | { 17 | Console.WriteLine(ex.Message); 18 | } 19 | } 20 | 21 | private static void ExecuteAttack(Tank tankA, Tank tankB) 22 | { 23 | while (tankB.Health > 0) 24 | { 25 | tankA.Shoot(tankB); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/CustomExceptions/TankException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CustomExceptions 4 | { 5 | public class TankException : Exception 6 | { 7 | public TankException(string msg) 8 | : base(msg) 9 | { 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/ExceptionsProperties/ExceptionsProperties.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ExceptionsProperties 4 | { 5 | public static void Main() 6 | { 7 | try 8 | { 9 | CauseFormatException(); 10 | } 11 | catch (FormatException fe) 12 | { 13 | Console.Error.WriteLine("Exception caught: " + fe); 14 | Console.Error.WriteLine("\nMessage: " + fe.Message); 15 | Console.Error.WriteLine("\nStack Trace: " + fe.StackTrace); 16 | } 17 | } 18 | 19 | public static void CauseFormatException() 20 | { 21 | string str = "an invalid number"; 22 | int.Parse(str); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/HandlingExceptions/1. Handling Exceptions.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | en-US 11 | false 12 | 13 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/HandlingExceptions/HandlingExceptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class HandlingExceptions 4 | { 5 | public static void Main() 6 | { 7 | string str = Console.ReadLine(); 8 | 9 | try 10 | { 11 | int.Parse(str); 12 | Console.WriteLine("You entered a valid Int32 number {0}.", str); 13 | } 14 | catch (FormatException) 15 | { 16 | Console.WriteLine("Invalid integer number!"); 17 | } 18 | catch (OverflowException) 19 | { 20 | Console.WriteLine("The number is too big to fit in Int32!"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/Read-Text-File/ReadTextFile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | 4 | public class ReadTextFile 5 | { 6 | private const string FilePath = @"..\..\ReadTextFile.cs"; 7 | 8 | public static void Main() 9 | { 10 | StreamReader reader = null; 11 | 12 | try 13 | { 14 | reader = new StreamReader(FilePath); 15 | int lineNumber = 0; 16 | string line = reader.ReadLine(); 17 | 18 | while (line != null) 19 | { 20 | lineNumber++; 21 | Console.WriteLine("Line {0}: {1}", lineNumber, line); 22 | line = reader.ReadLine(); 23 | } 24 | } 25 | catch (FileNotFoundException) 26 | { 27 | Console.WriteLine("File {0} not found", FilePath); 28 | } 29 | catch (DirectoryNotFoundException ex) 30 | { 31 | Console.WriteLine("File path contains an invalid directory"); 32 | } 33 | catch (UnauthorizedAccessException) 34 | { 35 | Console.WriteLine("Unauthorized access to specified file path"); 36 | } 37 | catch (IOException) 38 | { 39 | Console.WriteLine("Unknown IO has occurred. Verify that the file path is correct."); 40 | } 41 | finally 42 | { 43 | if (reader != null) 44 | { 45 | reader.Close(); 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/StackTrace/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/StackTrace/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CallStack")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CallStack")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fc64789a-1336-4d49-9ea7-8df176e30c18")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/StackTrace/Sale.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StackTrace 4 | { 5 | public class Sale 6 | { 7 | private string customer; 8 | private string product; 9 | private double price; 10 | 11 | public Sale(string customer, string product, double price) 12 | { 13 | this.Customer = customer; 14 | this.Product = product; 15 | this.Price = price; 16 | } 17 | 18 | public string Customer 19 | { 20 | get { return this.customer; } 21 | set 22 | { 23 | if (string.IsNullOrEmpty(value)) 24 | { 25 | throw new ArgumentNullException("Customer", "Customer cannot be null"); 26 | } 27 | 28 | this.customer = value; 29 | } 30 | } 31 | 32 | public string Product 33 | { 34 | get { return this.product; } 35 | set 36 | { 37 | if (string.IsNullOrEmpty(value)) 38 | { 39 | throw new ArgumentNullException("Product", "Product cannot be null"); 40 | } 41 | 42 | this.product = value; 43 | } 44 | } 45 | 46 | public double Price 47 | { 48 | get { return this.price; } 49 | set 50 | { 51 | if (value < 0) 52 | { 53 | throw new ArgumentOutOfRangeException("Price cannot be nagative"); 54 | } 55 | 56 | this.price = value; 57 | } 58 | } 59 | 60 | public override string ToString() 61 | { 62 | return string.Format("-Customer: {0}, Product: {1}, Price: ${2:F2}", 63 | this.Customer, this.Product, this.Price); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/StackTrace/Sales.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StackTrace 4 | { 5 | class Sales 6 | { 7 | static void Main() 8 | { 9 | string input = " SteamOp 50"; 10 | string[] args = input.Split(' '); 11 | 12 | string customer = args[0]; 13 | string product = args[1]; 14 | double price = double.Parse(args[2]); 15 | 16 | try 17 | { 18 | ProcessSale(customer, product, price); 19 | } 20 | catch (ArgumentOutOfRangeException ex) 21 | { 22 | Console.WriteLine("Price cannot be a negative number. Please enter a positive number."); 23 | } 24 | catch (ArgumentNullException ex) 25 | { 26 | Console.WriteLine("{0} cannot be left empty.", ex.ParamName); 27 | } 28 | 29 | Console.WriteLine("Finished"); 30 | } 31 | 32 | private static void ProcessSale(string customer, string product, double price) 33 | { 34 | var sale = new Sale(customer, product, price); 35 | Console.WriteLine(sale); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/ThrowingExceptions/5. Throwing Exceptions.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | en-US 11 | false 12 | 13 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/ThrowingExceptions/ThrowingExceptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ThrowingExceptions 4 | { 5 | public static void Main() 6 | { 7 | try 8 | { 9 | Sqrt(-1); 10 | } 11 | catch (ArgumentOutOfRangeException ex) 12 | { 13 | Console.Error.WriteLine("Error: " + ex.Message); 14 | throw; 15 | } 16 | } 17 | 18 | public static double Sqrt(double value) 19 | { 20 | if (value < 0) 21 | { 22 | throw new ArgumentOutOfRangeException( 23 | "Sqrt for negative numbers is undefined!"); 24 | } 25 | 26 | return Math.Sqrt(value); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/TryFinally/6. Try - Finally.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | en-US 11 | false 12 | 13 | -------------------------------------------------------------------------------- /02. Exception-Handling-Demos/TryFinally/TryFinallyExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class TryFinallyExample 4 | { 5 | public static void Main() 6 | { 7 | PerformTryFinally(); 8 | } 9 | 10 | public static void PerformTryFinally() 11 | { 12 | Console.WriteLine("Code executed before try-finally."); 13 | 14 | try 15 | { 16 | Console.Write("Enter a number: "); 17 | string str = Console.ReadLine(); 18 | 19 | int.Parse(str); 20 | 21 | Console.WriteLine("Parsing was successful."); 22 | return; // Exit the current method 23 | } 24 | catch (FormatException) 25 | { 26 | Console.WriteLine("Parsing failed!"); 27 | } 28 | finally 29 | { 30 | Console.WriteLine("This cleanup code is always executed."); 31 | } 32 | 33 | Console.WriteLine("This code is after the try-finally block."); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Class-Counter/ClassCounter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ClassCounter 4 | { 5 | public static void Main() 6 | { 7 | Person p = new Person("Pesho"); 8 | Console.WriteLine("Person name: {0}", p.Name); 9 | 10 | Person g = new Person("Gosho"); 11 | Console.WriteLine("Person name: {0}", g.Name); 12 | 13 | Console.WriteLine("Persons count: {0}", Person.PersonCounter); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Class-Counter/Person.cs: -------------------------------------------------------------------------------- 1 | public class Person 2 | { 3 | private static int instanceCounter = 0; 4 | 5 | public Person(string name = null) 6 | { 7 | Person.instanceCounter++; 8 | this.Name = name; 9 | } 10 | 11 | public static int PersonCounter 12 | { 13 | get 14 | { 15 | return Person.instanceCounter; 16 | } 17 | } 18 | 19 | public string Name { get; set; } 20 | } -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Indexers/BitArray32.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | struct BitArray32 4 | { 5 | public const int BitsCount = 32; 6 | 7 | private uint bitValues; 8 | 9 | // Indexer declaration 10 | public int this[int index] 11 | { 12 | get 13 | { 14 | if (0 <= index && index < BitsCount) 15 | { 16 | // Check the bit at position index 17 | if ((bitValues & (1 << index)) == 0) 18 | { 19 | return 0; 20 | } 21 | 22 | return 1; 23 | } 24 | 25 | throw new IndexOutOfRangeException(string.Format( 26 | "Index {0} is invalid!", 27 | index)); 28 | } 29 | 30 | set 31 | { 32 | if (index < 0 || index > BitsCount - 1) 33 | { 34 | throw new IndexOutOfRangeException(string.Format( 35 | "Index {0} is invalid!", 36 | index)); 37 | } 38 | 39 | if (value < 0 || value > 1) 40 | { 41 | throw new ArgumentException(string.Format( 42 | "Value {0} is invalid!", 43 | value)); 44 | } 45 | 46 | // Clear the bit at position index 47 | bitValues &= ~((uint)(1 << index)); 48 | 49 | // Set the bit at position index to value 50 | bitValues |= (uint)(value << index); 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Indexers/BitArrayMain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class IndexerExample 4 | { 5 | public static void Main() 6 | { 7 | BitArray32 bits = new BitArray32(); 8 | 9 | bits[0] = 1; 10 | bits[5] = 1; 11 | bits[5] = 0; 12 | bits[25] = 1; 13 | bits[31] = 1; 14 | 15 | for (int i = 0; i <= 31; i++) 16 | { 17 | Console.WriteLine("arr[{0}] = {1}", i, bits[i]); 18 | } 19 | 20 | Console.Write("bits = "); 21 | for (int i = 0; i <= 31; i++) 22 | { 23 | Console.Write(bits[i]); 24 | } 25 | 26 | Console.WriteLine(); 27 | } 28 | } -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Matrices/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Matrices/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Drawing = System.Drawing.Drawing2D; 3 | 4 | namespace Matrices 5 | { 6 | class Program 7 | { 8 | static void TestTryFinally() 9 | { 10 | Console.WriteLine("Code executed before try-finally."); 11 | try 12 | { 13 | string str = Console.ReadLine(); 14 | int.Parse(str); 15 | Console.WriteLine("Parsing was successful."); 16 | return; // Exit from the current method 17 | } 18 | catch (FormatException) 19 | { 20 | Console.WriteLine("Parsing failed!"); 21 | } 22 | finally 23 | { 24 | Console.WriteLine("This cleanup code is always executed."); 25 | } 26 | 27 | Console.WriteLine("This code is after the try-finally block."); 28 | } 29 | } 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Matrices/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Matrices")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Matrices")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("5c31715d-e74e-4e9a-aed0-014ae0fddd9c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/5. Namespaces.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ProjectFiles 5 | 6 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.Data/Faculty.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni.Data 2 | { 3 | public struct Faculty 4 | { 5 | public string Name { get; set; } 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.Data/Professor.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni.Data 2 | { 3 | public class Professor 4 | { 5 | public string FirstName { get; set; } 6 | 7 | public string LastName { get; set; } 8 | 9 | public Faculty Faculty { get; set; } 10 | 11 | public string Subject { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.Data/Specialty.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni.Data 2 | { 3 | public enum Specialty 4 | { 5 | ComputerScience, 6 | SoftwareEngineering, 7 | InformationSystems 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.Data/Student.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni.Data 2 | { 3 | public class Student 4 | { 5 | public string FirstName { get; set; } 6 | 7 | public string LastName { get; set; } 8 | 9 | public Faculty Faculty { get; set; } 10 | 11 | public Specialty Specialty { get; set; } 12 | 13 | public int FacultyNumber { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.UI/ProfessorAdminForm.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni.UI 2 | { 3 | public class ProfessorAdminForm : System.Windows.Forms.Form 4 | { 5 | // ... 6 | private void InitializeComponent() 7 | { 8 | AdministrationSystem system = new AdministrationSystem(); 9 | this.SuspendLayout(); 10 | // 11 | // ProfessorAdminForm 12 | // 13 | this.ClientSize = new System.Drawing.Size(284, 262); 14 | this.Name = "ProfessorAdminForm"; 15 | this.Text = "Professors"; 16 | this.ResumeLayout(false); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni.UI/StudentAdminForm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SoftUni.UI 4 | { 5 | public class StudentAdminForm : System.Windows.Forms.Form 6 | { 7 | // ... 8 | private void InitializeComponent() 9 | { 10 | this.SuspendLayout(); 11 | // 12 | // StudentAdminForm 13 | // 14 | this.ClientSize = new System.Drawing.Size(284, 262); 15 | this.Name = "StudentAdminForm"; 16 | this.Text = "Students"; 17 | this.ResumeLayout(false); 18 | 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Namespaces/SoftUni/AdministrationSystem.cs: -------------------------------------------------------------------------------- 1 | namespace SoftUni 2 | { 3 | public class AdministrationSystem 4 | { 5 | public static void Main() 6 | { 7 | // ... 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/Overloading-Operators/TestFractions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class TestFractions 4 | { 5 | public static void Main() 6 | { 7 | Fraction fr = new Fraction(56, 42); 8 | Fraction f1 = (double)1 / 4; 9 | Console.WriteLine("f1 = {0}", f1); 10 | Fraction f2 = (double)7 / 10; 11 | Console.WriteLine("f2 = {0}", f2); 12 | Console.WriteLine("-f1 = {0}", -f1); 13 | Console.WriteLine("f1 + f2 = {0}", f1 + f2); 14 | Console.WriteLine("f1 - f2 = {0}", f1 - f2); 15 | Console.WriteLine("f1 * f2 = {0}", f1 * f2); 16 | Console.WriteLine("f1 / f2 = {0}", f1 / f2); 17 | Console.WriteLine("f1 / f2 as double = {0}", (double)(f1 / f2)); 18 | Console.WriteLine("-(f1+f2)*(f1-f2/f1) = {0}", -(f1 + f2) * (f1 - f2 / f1)); 19 | Console.WriteLine("++f1 = {0}", ++f1); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/StaticMembers/2. Static-Members.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | en-US 17 | false 18 | 19 | -------------------------------------------------------------------------------- /02. Static-Members-and-Namespaces-Demos/StaticMembers/SqrtPrecalculated.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class SqrtPrecalculated 4 | { 5 | public const int MaxValue = 10000; 6 | 7 | // Static field 8 | private static int[] sqrtValues; 9 | 10 | // Static constructor 11 | static SqrtPrecalculated() 12 | { 13 | sqrtValues = new int[MaxValue + 1]; 14 | for (int i = 0; i < sqrtValues.Length; i++) 15 | { 16 | sqrtValues[i] = (int)Math.Sqrt(i); 17 | } 18 | } 19 | 20 | // Static method 21 | public static int GetSqrt(int value) 22 | { 23 | return sqrtValues[value]; 24 | } 25 | 26 | // The Main() method is always static 27 | static void Main() 28 | { 29 | Console.WriteLine(SqrtPrecalculated.GetSqrt(254)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/AbstractClasses.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class AbstractClasses 4 | { 5 | public static void Main() 6 | { 7 | Turtle turtle = new Turtle(); 8 | Console.WriteLine(turtle); 9 | Console.WriteLine("The {0} can go {1} km/h ", turtle.GetName(), turtle.Speed); 10 | 11 | Console.WriteLine(); 12 | 13 | Cheetah cheetah = new Cheetah(); 14 | Console.WriteLine(cheetah); 15 | Console.WriteLine("The {0} can go {1} km/h ", cheetah.GetName(), cheetah.Speed); 16 | 17 | Console.WriteLine(); 18 | 19 | Tomcat tomcat = new Tomcat(); 20 | Console.WriteLine(tomcat); 21 | Console.WriteLine("The {0} can go {1} km/h ", tomcat.GetName(), tomcat.Speed); 22 | tomcat.SayMyaau(); 23 | 24 | Console.WriteLine(); 25 | 26 | Kitten kitten = new Kitten(); 27 | Console.WriteLine(kitten); 28 | Console.WriteLine("The {0} can go {1} km/h ", kitten.GetName(), kitten.Speed); 29 | kitten.SayMyaau(); 30 | 31 | // This will not compile (Cat is abstract -> cannot be instantiated) 32 | //Cat cat = new Cat(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Animal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public abstract class Animal : IComparable 4 | { 5 | // Abstract methods --> should be implemented in the child classes 6 | public abstract int Speed { get; } 7 | 8 | public abstract string GetName(); 9 | 10 | // Non-abstract method 11 | public override string ToString() 12 | { 13 | return "I am " + this.GetName(); 14 | } 15 | 16 | // Interface method 17 | public int CompareTo(Animal other) 18 | { 19 | return this.Speed.CompareTo(other.Speed); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Cat.cs: -------------------------------------------------------------------------------- 1 | public abstract class Cat : Animal 2 | { 3 | public string Breed { get; set; } 4 | 5 | public abstract void SayMyaau(); 6 | } 7 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Cheetah.cs: -------------------------------------------------------------------------------- 1 | public class Cheetah : Animal 2 | { 3 | public override int Speed 4 | { 5 | get 6 | { 7 | return 100; 8 | } 9 | } 10 | 11 | public override string GetName() 12 | { 13 | return "cheetah"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Kitten.cs: -------------------------------------------------------------------------------- 1 | public class Kitten : Cat 2 | { 3 | public override int Speed 4 | { 5 | get 6 | { 7 | return 5; 8 | } 9 | } 10 | 11 | public override string GetName() 12 | { 13 | return "kitten"; 14 | } 15 | 16 | public override void SayMyaau() 17 | { 18 | System.Console.WriteLine("Kitten miau, miaau"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Tomcat.cs: -------------------------------------------------------------------------------- 1 | public class Tomcat : Cat 2 | { 3 | public override int Speed 4 | { 5 | get 6 | { 7 | return 20; 8 | } 9 | } 10 | 11 | public override string GetName() 12 | { 13 | return "tomcat"; 14 | } 15 | 16 | public override void SayMyaau() 17 | { 18 | System.Console.WriteLine("Tomcat muaaaaaaaau"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Abstract-Classes/Turtle.cs: -------------------------------------------------------------------------------- 1 | public class Turtle : Animal 2 | { 3 | public override int Speed 4 | { 5 | get 6 | { 7 | return 1; 8 | } 9 | } 10 | 11 | public override string GetName() 12 | { 13 | return "turtle"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Access-Modifiers/AccessModifiers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class AccessModifiers 4 | { 5 | public static void Main() 6 | { 7 | Dog joe = new Dog("Sharo", 6, "labrador"); 8 | joe.Sleep(); // Sleep() is public 9 | Console.WriteLine("Breed: " + joe.Breed); 10 | joe.WagTail(); // WagTail() is internal 11 | 12 | //joe.Talk(); // This will not compile - Talk() is private 13 | //joe.Walk(); // This will not compile - Walk() is protected 14 | //Console.WriteLine("Name: " + joe.Name); // Name is protected property 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Access-Modifiers/Creature.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Creature 4 | { 5 | public Creature(string name) 6 | { 7 | this.Name = name; 8 | } 9 | 10 | protected string Name { get; private set; } 11 | 12 | private void Talk() 13 | { 14 | Console.WriteLine("I am creature ..."); 15 | } 16 | 17 | protected void Walk() 18 | { 19 | Console.WriteLine("Walking, walking, walking ...."); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Access-Modifiers/Dog.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Dog : Mammal 4 | { 5 | public Dog(string name, int age, string breed) 6 | : base(name, age) 7 | { 8 | this.Breed = breed; 9 | } 10 | 11 | public string Breed { get; private set; } 12 | 13 | internal void WagTail() 14 | { 15 | Console.WriteLine( 16 | "{0} is {1} wagging his {2}-aged tail ...", 17 | this.Name, 18 | this.Breed, 19 | this.Age); 20 | 21 | //this.Walk(); // This will successfully compile - Walk() is protected 22 | //this.Talk(); // This will not compile - Talk() is private 23 | //this.Name = "Doggy"; // This will not compile - Name.set is private 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Access-Modifiers/Mammal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Mammal : Creature 4 | { 5 | public Mammal(string name, int age) 6 | : base(name) 7 | { 8 | this.Age = age; 9 | } 10 | 11 | public int Age { get; set; } 12 | 13 | public void Sleep() 14 | { 15 | Console.WriteLine("Shhh! {0} is sleeping!", this.Name); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/Color.cs: -------------------------------------------------------------------------------- 1 | public struct Color 2 | { 3 | public Color(byte redValue, byte greenValue, byte blueValue) 4 | : this() 5 | { 6 | this.RedValue = redValue; 7 | this.GreenValue = greenValue; 8 | this.BlueValue = blueValue; 9 | } 10 | 11 | public byte RedValue { get; set; } 12 | 13 | public byte GreenValue { get; set; } 14 | 15 | public byte BlueValue { get; set; } 16 | } 17 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/FiguresExample.cs: -------------------------------------------------------------------------------- 1 | public class FiguresExample 2 | { 3 | public static void Main() 4 | { 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/FilledRectangle.cs: -------------------------------------------------------------------------------- 1 | public class FilledRectangle : Rectangle 2 | { 3 | public FilledRectangle(float width, float height, int x, int y, Color color) 4 | : base(width, height, x, y) 5 | { 6 | this.Color = color; 7 | } 8 | 9 | private Color Color { get; set; } 10 | } 11 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/FilledSquare.cs: -------------------------------------------------------------------------------- 1 | public class FilledSquare : Square 2 | { 3 | public FilledSquare(float size, int x, int y, Color color) 4 | : base(size, x, y) 5 | { 6 | this.Color = color; 7 | } 8 | 9 | private Color Color { get; set; } 10 | } 11 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/ISufraceCalculatable.cs: -------------------------------------------------------------------------------- 1 | public interface ISufraceCalculatable 2 | { 3 | float CalculateSurface(); 4 | } 5 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/Point.cs: -------------------------------------------------------------------------------- 1 | public struct Point 2 | { 3 | public Point(int x, int y) 4 | : this() 5 | { 6 | this.X = x; 7 | this.Y = y; 8 | } 9 | 10 | public int X { get; set; } 11 | 12 | public int Y { get; set; } 13 | } 14 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/Rectangle.cs: -------------------------------------------------------------------------------- 1 | public class Rectangle : Shape, ISufraceCalculatable 2 | { 3 | public Rectangle(float width, float height, int x, int y) 4 | : base(new Point(x, y)) 5 | { 6 | this.Width = width; 7 | this.Height = height; 8 | } 9 | 10 | private float Width { get; set; } 11 | 12 | private float Height { get; set; } 13 | 14 | public float CalculateSurface() 15 | { 16 | return this.Width * this.Height; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/Shape.cs: -------------------------------------------------------------------------------- 1 | public abstract class Shape 2 | { 3 | protected Shape(Point position) 4 | { 5 | this.Position = position; 6 | } 7 | 8 | protected Point Position { get; set; } 9 | } -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Class-Diagrams/Square.cs: -------------------------------------------------------------------------------- 1 | class Square : Shape, ISufraceCalculatable 2 | { 3 | public Square(float size, int x, int y) 4 | : base(new Point(x, y)) 5 | { 6 | this.Size = size; 7 | } 8 | 9 | private float Size { get; set; } 10 | 11 | public float CalculateSurface() 12 | { 13 | return this.Size * this.Size; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/Circle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Circle : IShape, IMovable 4 | { 5 | private int x; 6 | private int y; 7 | private int radius; 8 | 9 | public Circle(int x, int y, int radius) 10 | { 11 | this.x = x; 12 | this.y = y; 13 | this.radius = radius; 14 | } 15 | 16 | public void SetPosition(int x, int y) // From IShape 17 | { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | 22 | public double CalculateSurface() // From IShape 23 | { 24 | return Math.PI * radius * radius; 25 | } 26 | 27 | public void Move(int deltaX, int deltaY) // From IMovable 28 | { 29 | this.x += deltaX; 30 | this.y += deltaY; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/IMovable.cs: -------------------------------------------------------------------------------- 1 | public interface IMovable 2 | { 3 | void Move(int deltaX, int deltaY); 4 | } 5 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/IResizable.cs: -------------------------------------------------------------------------------- 1 | public interface IResizable 2 | { 3 | void Resize(int weightX, int weightY); 4 | 5 | void ResizeByX(int weightX); 6 | 7 | void ResizeByY(int weightY); 8 | } 9 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/IShape.cs: -------------------------------------------------------------------------------- 1 | public interface IShape 2 | { 3 | void SetPosition(int x, int y); 4 | 5 | double CalculateSurface(); 6 | } 7 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/PlayWithInterfaces.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class PlayWithInterfaces 4 | { 5 | public static void Main() 6 | { 7 | Square square = new Square(0, 0, 10); 8 | Rectangle rect = new Rectangle(0, 0, 10, 12); 9 | Circle circle = new Circle(0, 0, 5); 10 | 11 | if (square is IShape) 12 | { 13 | Console.WriteLine("{0} is IShape", square.GetType()); 14 | } 15 | 16 | if (rect is IResizable) 17 | { 18 | Console.WriteLine("{0} is IResizable", rect.GetType()); 19 | } 20 | 21 | if (circle is IResizable) 22 | { 23 | Console.WriteLine("{0} is IResizable", circle.GetType()); 24 | } 25 | 26 | IShape[] shapes = { square, rect, circle }; 27 | 28 | foreach (IShape shape in shapes) 29 | { 30 | shape.SetPosition(5, 5); 31 | Console.WriteLine( 32 | "Type: {0}; surface: {1}", 33 | shape.GetType(), 34 | shape.CalculateSurface()); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/Rectangle.cs: -------------------------------------------------------------------------------- 1 | public struct Rectangle : IShape, IMovable, IResizable 2 | { 3 | private int x; 4 | private int y; 5 | private int width; 6 | private int height; 7 | 8 | public Rectangle(int x, int y, int width, int height) 9 | { 10 | this.x = x; 11 | this.y = y; 12 | this.width = width; 13 | this.height = height; 14 | } 15 | 16 | public void SetPosition(int x, int y) 17 | { 18 | this.x = x; 19 | this.y = y; 20 | } 21 | 22 | public double CalculateSurface() 23 | { 24 | return width * height; 25 | } 26 | 27 | public void Move(int deltaX, int deltaY) // From IMovable 28 | { 29 | this.x += deltaX; 30 | this.y += deltaY; 31 | } 32 | 33 | public void Resize(int weightX, int weightY) // IResizable 34 | { 35 | width = width * weightX; 36 | height = height * weightY; 37 | } 38 | 39 | public void ResizeByX(int weightX) // From IResizable 40 | { 41 | width = width * weightX; 42 | } 43 | 44 | public void ResizeByY(int weightY) // From IResizable 45 | { 46 | height = height * weightY; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Interfaces-and-Implementation/Square.cs: -------------------------------------------------------------------------------- 1 | public class Square : IShape 2 | { 3 | private int x; 4 | private int y; 5 | private int size; 6 | 7 | public Square(int x, int y, int size) 8 | { 9 | this.x = x; 10 | this.y = y; 11 | this.size = size; 12 | } 13 | 14 | // Derived from IShape 15 | public void SetPosition(int x, int y) 16 | { 17 | this.x = x; 18 | this.y = y; 19 | } 20 | 21 | // Derived from IShape 22 | public double CalculateSurface() 23 | { 24 | return size * size; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Simple-Inheritance/Cat.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Cat : Mammal 4 | { 5 | public Cat(int age) 6 | : base(age) 7 | { 8 | } 9 | 10 | public void SayMyaau() 11 | { 12 | Console.WriteLine("Myaau..."); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Simple-Inheritance/Dog.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Dog : Mammal 4 | { 5 | public Dog(int age, string breed) 6 | : base(age) 7 | { 8 | this.Breed = breed; 9 | } 10 | 11 | public string Breed { get; set; } 12 | 13 | public void WagTail() 14 | { 15 | Console.WriteLine("Tail wagging..."); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Simple-Inheritance/Mammal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Mammal 4 | { 5 | public Mammal(int age) 6 | { 7 | this.Age = age; 8 | } 9 | 10 | public int Age { get; set; } 11 | 12 | public void Sleep() 13 | { 14 | Console.WriteLine("Shhh! I'm sleeping!"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /03. Inheritance-and-Abstraction-Demos/Simple-Inheritance/SimpleInheritance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class SimpleInheritance 4 | { 5 | public static void Main() 6 | { 7 | Dog joe = new Dog(8, "Labrador"); 8 | joe.Sleep(); 9 | joe.WagTail(); 10 | Console.WriteLine("Joe is {0} years old dog of breed {1}.", joe.Age, joe.Breed); 11 | 12 | Console.WriteLine(); 13 | 14 | Cat dolly = new Cat(3); 15 | dolly.Sleep(); 16 | dolly.SayMyaau(); 17 | Console.WriteLine("Dolly is {0} years old cat.", dolly.Age); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation-ReferenceTypes/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation-ReferenceTypes/EncapsulatingReferenceTypes.cs: -------------------------------------------------------------------------------- 1 | namespace _1._1.Encapsulation_ReferenceTypes 2 | { 3 | using System; 4 | 5 | public class EncapsulatingReferenceTypes 6 | { 7 | public static void Main() 8 | { 9 | Person pesho = new Person("Pesho", 22); 10 | pesho.AddAlias("Peter"); 11 | pesho.AddAlias("Petar"); 12 | pesho.AddAlias("Gandalf"); 13 | 14 | Console.WriteLine(string.Join(", ", pesho.Aliases)); 15 | 16 | pesho.Aliases[0] = "Changed!!!"; 17 | Console.WriteLine(string.Join(", ", pesho.Aliases)); 18 | 19 | //Console.WriteLine(string.Join(", ", pesho.AliasesEnumerable)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation-ReferenceTypes/Person.cs: -------------------------------------------------------------------------------- 1 | namespace _1._1.Encapsulation_ReferenceTypes 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | public class Person 7 | { 8 | private const int MinPersonAge = 0; 9 | private const int MaxPersonAge = 120; 10 | 11 | private string name; 12 | private int age; 13 | private readonly List aliases = new List(); 14 | 15 | public Person(string name, int age) 16 | { 17 | this.Name = name; 18 | this.Age = age; 19 | } 20 | 21 | public string Name 22 | { 23 | get 24 | { 25 | return this.name; 26 | } 27 | 28 | set 29 | { 30 | if (string.IsNullOrWhiteSpace(value)) 31 | { 32 | throw new ArgumentException( 33 | "A person's name cannot be null, empty or whitespace.", 34 | "value"); 35 | } 36 | 37 | this.name = value; 38 | } 39 | } 40 | 41 | public int Age 42 | { 43 | get 44 | { 45 | return this.age; 46 | } 47 | 48 | set 49 | { 50 | if (value < MinPersonAge || MaxPersonAge < value) 51 | { 52 | throw new ArgumentOutOfRangeException( 53 | "value", 54 | string.Format("A person's age must be in the range [{0} ... {1}].", MinPersonAge, MaxPersonAge)); 55 | } 56 | 57 | this.age = value; 58 | } 59 | } 60 | 61 | public List Aliases 62 | { 63 | get 64 | { 65 | return this.aliases; 66 | } 67 | } 68 | 69 | //public IEnumerable AliasesEnumerable 70 | //{ 71 | // get 72 | // { 73 | // return this.aliases; 74 | // } 75 | //} 76 | 77 | public void AddAlias(string alias) 78 | { 79 | this.aliases.Add(alias); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation-ReferenceTypes/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("1.1.Encapsulation-ReferenceTypes")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("1.1.Encapsulation-ReferenceTypes")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("51e949ea-cfc0-4162-bc1f-fd63d5017d94")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation/EncapsulationExample.cs: -------------------------------------------------------------------------------- 1 | namespace Encapsulation 2 | { 3 | using System; 4 | 5 | public class EncapsulationExample 6 | { 7 | public static void Main() 8 | { 9 | Person p = new Person("Bay Ivan", 73); 10 | Console.WriteLine(p); 11 | 12 | // Person n = new Person("Negative age", -5); 13 | 14 | // Person e = new Person("", 20); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Encapsulation/Person.cs: -------------------------------------------------------------------------------- 1 | namespace Encapsulation 2 | { 3 | using System; 4 | 5 | public class Person 6 | { 7 | private string name; 8 | private int age; 9 | 10 | public Person(string name, int age) 11 | { 12 | this.Name = name; 13 | this.Age = age; 14 | } 15 | 16 | public string Name 17 | { 18 | get 19 | { 20 | return this.name; 21 | } 22 | 23 | set 24 | { 25 | if (string.IsNullOrEmpty(value)) 26 | { 27 | throw new ArgumentException("Invalid person name."); 28 | } 29 | 30 | this.name = value; 31 | } 32 | } 33 | 34 | public int Age 35 | { 36 | get 37 | { 38 | return this.age; 39 | } 40 | 41 | set 42 | { 43 | if (value < 0 || value > 120) 44 | { 45 | throw new ArgumentOutOfRangeException("age", "Invalid person age."); 46 | } 47 | 48 | this.age = value; 49 | } 50 | } 51 | 52 | public override string ToString() 53 | { 54 | return string.Format("Person (name={0}, age={1})", this.name, this.age); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/Circle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Circle : Figure 4 | { 5 | public double Radius { get; set; } 6 | 7 | public override double CalcSurface() 8 | { 9 | return Math.PI * this.Radius * this.Radius; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/ClassDiagram.cd: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | AAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAA= 7 | Circle.cs 8 | 9 | 10 | 11 | 12 | 13 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAA= 14 | Figure.cs 15 | 16 | 17 | 18 | 19 | 20 | AAAAAAAABAAAAAAAAAAAAEAAAAAAAAAAAAEAAAAAAAA= 21 | Rectangle.cs 22 | 23 | 24 | 25 | 26 | 27 | AAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAEAAAAAAAA= 28 | Square.cs 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/Figure.cs: -------------------------------------------------------------------------------- 1 | public abstract class Figure 2 | { 3 | public abstract double CalcSurface(); 4 | } 5 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/Polymorphism.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Polymorphism 4 | { 5 | public static void Main() 6 | { 7 | Figure[] figures = { 8 | new Square { Size = 3 }, 9 | new Circle { Radius = 2 }, 10 | new Rectangle { Width = 2, Height = 3 }, 11 | new Circle { Radius = 3.5 }, 12 | new Square { Size = 2.5 }, 13 | new Square { Size = 4 }, 14 | new SpecialSquare { Size = 3 } 15 | }; 16 | 17 | foreach (Figure figure in figures) 18 | { 19 | Console.WriteLine( 20 | "Figure = {0} surface = {1:F2}", 21 | figure.GetType().Name.PadRight(15, ' '), 22 | figure.CalcSurface()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/Rectangle.cs: -------------------------------------------------------------------------------- 1 | public class Rectangle : Figure 2 | { 3 | public double Width { get; set; } 4 | 5 | public double Height { get; set; } 6 | 7 | public override double CalcSurface() 8 | { 9 | return this.Width * this.Height; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Polymorphism/Square.cs: -------------------------------------------------------------------------------- 1 | public class Square : Figure 2 | { 3 | public double Size { get; set; } 4 | 5 | public override double CalcSurface() 6 | { 7 | return this.Size * this.Size; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/Circle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Circle : Figure 4 | { 5 | public override void Draw() 6 | { 7 | Console.WriteLine("I am a circle:"); 8 | Console.WriteLine(" --- "); 9 | Console.WriteLine("| |"); 10 | Console.WriteLine(" --- "); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/Figure.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public abstract class Figure 4 | { 5 | public virtual void Draw() 6 | { 7 | Console.WriteLine( 8 | "I am a figure of unknown kind: {0}", 9 | this.GetType().Name); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/Line.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Line : Figure 4 | { 5 | public override void Draw() 6 | { 7 | Console.WriteLine("I am a line:"); 8 | Console.WriteLine("-----"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/SpecialFigure.cs: -------------------------------------------------------------------------------- 1 | public class SpecialFigure : Circle 2 | { 3 | } -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/VirtualMethods.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Virtual_Methods; 3 | 4 | public class VirtualMethods 5 | { 6 | public static void Main() 7 | { 8 | Figure[] figures = 9 | { 10 | new Line(), 11 | new Circle(), 12 | new Line(), 13 | new SpecialFigure(), 14 | new Line(), 15 | new WrongFigure() 16 | }; 17 | 18 | foreach (var f in figures) 19 | { 20 | f.Draw(); 21 | Console.WriteLine(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /04. Encapsulation-and-Polymorphism-Demos/Virtual-Methods/WrongFigure.cs: -------------------------------------------------------------------------------- 1 | namespace Virtual_Methods 2 | { 3 | using System; 4 | 5 | public class WrongFigure : Figure 6 | { 7 | public new void Draw() 8 | { 9 | Console.WriteLine("This method hides inherited member, does not override it!"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/AttributeValidaiton.cs: -------------------------------------------------------------------------------- 1 | namespace AttributeValidation 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | using AttributeValidation.Attributes; 8 | using AttributeValidation.Models; 9 | 10 | public class AttributeValidaiton 11 | { 12 | static void Main() 13 | { 14 | var warriors = new List() 15 | { 16 | new Warrior() 17 | { 18 | Name = "Gosho", 19 | //Weapon = new Weapon() 20 | }, 21 | new Warrior() 22 | { 23 | //Name = "Pesho", 24 | Weapon = new Weapon() 25 | }, 26 | new Warrior() 27 | { 28 | //Name = "Gencho", 29 | //Weapon = new Weapon() 30 | } 31 | }; 32 | 33 | try 34 | { 35 | ValidateRequiredProperties(warriors); 36 | } 37 | catch (AggregateException ex) 38 | { 39 | Console.Error.WriteLine(ex.Flatten()); 40 | } 41 | } 42 | 43 | private static void ValidateRequiredProperties(IEnumerable entities) 44 | { 45 | var requiredProperties = typeof(T) 46 | .GetProperties() 47 | .Where(pr => pr.GetCustomAttributes(false) 48 | .Any(a => a is RequiredAttribute)); 49 | 50 | var exceptions = new List(); 51 | foreach (var warrior in entities) 52 | { 53 | foreach (var prop in requiredProperties) 54 | { 55 | if (prop.GetValue(warrior) == null) 56 | { 57 | var ex = new ArgumentNullException(prop.Name); 58 | 59 | exceptions.Add(ex); 60 | } 61 | } 62 | } 63 | 64 | if (exceptions.Count > 0) 65 | { 66 | throw new AggregateException(exceptions); 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/Attributes/RequiredAttribute.cs: -------------------------------------------------------------------------------- 1 | namespace AttributeValidation.Attributes 2 | { 3 | using System; 4 | 5 | [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] 6 | public class RequiredAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/Models/Warrior.cs: -------------------------------------------------------------------------------- 1 | namespace AttributeValidation.Models 2 | { 3 | using AttributeValidation.Attributes; 4 | 5 | public class Warrior 6 | { 7 | [Required] 8 | public string Name { get; set; } 9 | 10 | [Required] 11 | public Weapon Weapon { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/Models/Weapon.cs: -------------------------------------------------------------------------------- 1 | namespace AttributeValidation.Models 2 | { 3 | public class Weapon 4 | { 5 | public string Name { get; set; } 6 | 7 | public int Damage { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/AttributeValidation/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AttributeValidation")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AttributeValidation")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0211f5f2-1b93-40ce-8ee6-7bbdbedaadde")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Attributes/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Attributes Demo")] 9 | [assembly: AssemblyDescription("Attributes - example")] 10 | [assembly: AssemblyCompany("DemoSoft")] 11 | [assembly: AssemblyProduct("Enterprise Demo Suite")] 12 | [assembly: AssemblyCopyright("Copyright © Some Company 2014")] 13 | 14 | // Setting ComVisible to false makes the types in this assembly not visible 15 | // to COM components. If you need to access a type in this assembly from 16 | // COM, set the ComVisible attribute to true on that type. 17 | [assembly: ComVisible(false)] 18 | 19 | // The following GUID is for the ID of the typelib if this project is exposed to COM 20 | [assembly: Guid("0e2a168f-c36c-4df7-9946-9a3403a1d78f")] 21 | 22 | // Version information for an assembly consists of the following four values: 23 | // 24 | // Major Version 25 | // Minor Version 26 | // Build Number 27 | // Revision 28 | // 29 | // You can specify all the values or you can default the Build and Revision Numbers 30 | // by using the '*' as shown below: 31 | // [assembly: AssemblyVersion("1.0.*")] 32 | [assembly: AssemblyVersion("2.0.1.37")] 33 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Attributes/UsingAttributes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | class UsingAttributes 5 | { 6 | [DllImport("user32.dll", EntryPoint = "MessageBox")] 7 | public static extern int ShowMessageBox(int hWnd, string text, string caption, int type); 8 | 9 | static void Main() 10 | { 11 | ShowMessageBox(0, "Some text", "Some caption", 0); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/CofeeSize/CofeeSize.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public enum CoffeeSize 4 | { 5 | Small = 100, 6 | Normal = 150, 7 | Double = 300 8 | } 9 | 10 | public class Coffee 11 | { 12 | public CoffeeSize size; 13 | 14 | public Coffee(CoffeeSize size) 15 | { 16 | this.size = size; 17 | } 18 | 19 | public CoffeeSize Size 20 | { 21 | get { return size; } 22 | } 23 | } 24 | 25 | public class CoffeeMachine 26 | { 27 | 28 | static void Main() 29 | { 30 | Coffee normalCoffee = new Coffee(CoffeeSize.Normal); 31 | Coffee doubleCoffee = new Coffee(CoffeeSize.Double); 32 | 33 | Console.WriteLine("The {0} coffee is {1} ml.", 34 | normalCoffee.Size, (int)normalCoffee.Size); 35 | Console.WriteLine("The {0} coffee is {1} ml.", 36 | doubleCoffee.Size, (int)doubleCoffee.Size); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Custom-Attributes/7. Custom Attributes.csproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | ProjectFiles 5 | 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Custom-Attributes/CustomAttributesDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | [AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class | AttributeTargets.Interface, 4 | AllowMultiple = true)] 5 | public class AuthorAttribute : System.Attribute 6 | { 7 | public string Name { get; private set; } 8 | 9 | public AuthorAttribute(string name) 10 | { 11 | this.Name = name; 12 | } 13 | } 14 | 15 | [Author("Svetlin Nakov")] 16 | [Author("Bay Ivan")] 17 | class CustomAttributesDemo 18 | { 19 | static void Main(string[] args) 20 | { 21 | Type type = typeof(CustomAttributesDemo); 22 | object[] allAttributes = type.GetCustomAttributes(false); 23 | foreach (AuthorAttribute authorAttribute in allAttributes) 24 | { 25 | Console.WriteLine("This class is written by {0}. ", authorAttribute.Name); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/CustomStackExample/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/CustomStackExample/CustomStackExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CustomStackExample 4 | { 5 | class Student : IComparable 6 | { 7 | public Student(string name, int age) 8 | { 9 | this.Name = name; 10 | this.Age = age; 11 | } 12 | 13 | public string Name { get; set; } 14 | 15 | public int Age { get; set; } 16 | 17 | public int CompareTo(Student other) 18 | { 19 | if (this.Age > other.Age) 20 | { 21 | return 1; 22 | } 23 | else if (this.Age < other.Age) 24 | { 25 | return -1; 26 | } 27 | 28 | return 0; 29 | // return this.Age.CompareTo(other.Age); 30 | } 31 | } 32 | 33 | class CustomStackExample 34 | { 35 | static void Main() 36 | { 37 | var studentStack = new CustomStack(); 38 | studentStack.Push(new Student("Pesho", 22)); 39 | studentStack.Push(new Student("Penka", 41)); 40 | studentStack.Push(new Student("Gosho", 7)); 41 | studentStack.Push(new Student("Kuci", 15)); 42 | 43 | Console.WriteLine(studentStack.Min().Name); 44 | 45 | var intStack = new CustomStack(); 46 | 47 | intStack.Push(4); 48 | intStack.Push(5); 49 | intStack.Push(14); 50 | intStack.Push(42); 51 | intStack.Push(455); 52 | 53 | intStack.Pop(); 54 | intStack.Pop(); 55 | 56 | Console.WriteLine(intStack.Min()); 57 | 58 | intStack.Clear(); 59 | 60 | Console.WriteLine(intStack.Count); 61 | Console.WriteLine(intStack.IsEmpty); 62 | Console.WriteLine(intStack); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/CustomStackExample/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CustomStackExample")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CustomStackExample")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("d99cb62d-226e-45f7-8ab7-a67765ed64eb")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/DynamicType/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/DynamicType/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("DynamicType")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("DynamicType")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4912ad02-6315-4ee4-accc-30bf8a5b93a9")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Enums/DayOfWeek.cs: -------------------------------------------------------------------------------- 1 | public enum DayOfWeek 2 | { 3 | Mon, 4 | Tue, 5 | Wed, 6 | Thu, 7 | Fri, 8 | Sat, 9 | Sun 10 | } 11 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Enums/EnumsExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class EnumExample 4 | { 5 | static void Main() 6 | { 7 | DayOfWeek day = DayOfWeek.Wed; 8 | 9 | Console.WriteLine(day); // Wed 10 | 11 | Console.WriteLine((int)day); // 2 12 | 13 | day = DayOfWeek.Sat; 14 | Console.WriteLine(++day); // Sun 15 | Console.WriteLine(++day); // 7 16 | 17 | day = (DayOfWeek) Enum.Parse(typeof(DayOfWeek), "Mon"); 18 | Console.WriteLine(day); // Mon 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Generic-Classes/GenericList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class GenericList 4 | { 5 | const int DefaultCapacity = 4096; 6 | 7 | private T[] elements; 8 | private int count = 0; 9 | 10 | public GenericList(int capacity = DefaultCapacity) 11 | { 12 | this.elements = new T[capacity]; 13 | } 14 | 15 | public int Count 16 | { 17 | get 18 | { 19 | return this.count; 20 | } 21 | } 22 | 23 | public void Add(T element) 24 | { 25 | if (this.count >= this.elements.Length) 26 | { 27 | throw new IndexOutOfRangeException(String.Format( 28 | "The list capacity of {0} was exceeded.", this.elements.Length)); 29 | } 30 | this.elements[this.count] = element; 31 | this.count++; 32 | } 33 | 34 | public T this[int index] 35 | { 36 | get 37 | { 38 | if (index < 0 || index >= this.count) 39 | { 40 | throw new IndexOutOfRangeException(String.Format( 41 | "Invalid index: {0}.", index)); 42 | } 43 | 44 | T result = this.elements[index]; 45 | 46 | return result; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Generic-Classes/GenericListExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class GenericListExample 4 | { 5 | static void Main() 6 | { 7 | // Declare a list of type int 8 | GenericList intList = new GenericList(); 9 | intList.Add(1); 10 | intList.Add(2); 11 | intList.Add(3); 12 | Console.WriteLine("Number of elements: {0}", intList.Count); 13 | for (int i = 0; i < intList.Count; i++) 14 | { 15 | int element = intList[i]; 16 | Console.WriteLine(element); 17 | } 18 | 19 | Console.WriteLine(); 20 | 21 | // Declare a list of type string 22 | GenericList stringList = new GenericList(); 23 | stringList.Add("C#"); 24 | stringList.Add("Java"); 25 | stringList.Add("PHP"); 26 | stringList.Add("SQL"); 27 | Console.WriteLine("Number of elements: {0}", stringList.Count); 28 | for (int i = 0; i < stringList.Count; i++) 29 | { 30 | string element = stringList[i]; 31 | Console.WriteLine(element); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Generic-Methods/GenericMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class GenericMethod 4 | { 5 | static void Main() 6 | { 7 | int i = 5; 8 | int j = 7; 9 | int min = Min(i, j); // The same like Min(i, j); 10 | Console.WriteLine("Min({0}, {1}) = {2}", i, j, min); 11 | 12 | string firstString = "Rakiya"; 13 | string secondString = "Beer"; 14 | string minString = Min(firstString, secondString); 15 | Console.WriteLine("Min({0}, {1}) = {2}", firstString, secondString, minString); 16 | 17 | //Point p1 = new Point(); 18 | //Point p2 = new Point(); 19 | //Point minPoint = Min(p1, p2); // This will not compile 20 | 21 | string[] strings = CreateArray("hello", 5); 22 | Console.WriteLine(String.Join(", ", strings)); 23 | } 24 | 25 | public static T[] CreateArray(T value, int count) 26 | { 27 | T[] arr = new T[count]; 28 | for (int i = 0; i < count; i++) 29 | { 30 | arr[i] = value; 31 | } 32 | 33 | return arr; 34 | } 35 | 36 | public static T Min(T first, T second) 37 | where T : IComparable 38 | { 39 | if (first.CompareTo(second) <= 0) 40 | { 41 | return first; 42 | } 43 | else 44 | { 45 | return second; 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Generic-Methods/Point.cs: -------------------------------------------------------------------------------- 1 | struct Point 2 | { 3 | public int X { get; set; } 4 | public int Y { get; set; } 5 | } 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Other-Types-in-OOP-Demos.v12.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SoftUni/Object-Oriented-Programming/5f728c83f0542dbd113de7484f981f172306d9e9/06. OOP-Other-Types-in-OOP-Demos/Other-Types-in-OOP-Demos.v12.suo -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/StructVsClass/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/StructVsClass/ColorClass.cs: -------------------------------------------------------------------------------- 1 | namespace StructVsClass 2 | { 3 | public class ColorClass 4 | { 5 | public byte RedValue { get; set; } 6 | public byte GreenValue { get; set; } 7 | public byte BlueValue { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/StructVsClass/ColorStruct.cs: -------------------------------------------------------------------------------- 1 | namespace StructVsClass 2 | { 3 | public struct ColorStruct 4 | { 5 | public byte RedValue { get; set; } 6 | public byte GreenValue { get; set; } 7 | public byte BlueValue { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/StructVsClass/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("StructVsClass")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("StructVsClass")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("b80ee67d-7b6e-465c-bb0e-6427026d747c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/StructVsClass/StructClassComparison.cs: -------------------------------------------------------------------------------- 1 | namespace StructVsClass 2 | { 3 | using System; 4 | 5 | public class StructClassComparison 6 | { 7 | private const long PixelsCount = 1920 * 1080; 8 | 9 | static void Main() 10 | { 11 | long initialStructMem = GC.GetTotalMemory(true); 12 | var structColors = new ColorStruct[PixelsCount]; 13 | for (int i = 0; i < structColors.Length; i++) 14 | { 15 | structColors[i] = new ColorStruct(); 16 | } 17 | 18 | long postStructMem = GC.GetTotalMemory(true); 19 | Console.WriteLine("{0} instances of struct: {1}KB", 20 | PixelsCount, (postStructMem - initialStructMem) / 1000); 21 | 22 | long initialClassMem = GC.GetTotalMemory(true); 23 | var classColors = new ColorClass[PixelsCount]; 24 | for (int i = 0; i < classColors.Length; i++) 25 | { 26 | classColors[i] = new ColorClass(); 27 | } 28 | 29 | long postClassMem = GC.GetTotalMemory(true); 30 | Console.WriteLine("{0} instances of class: {1}KB", 31 | PixelsCount, (postClassMem - initialClassMem) / 1000); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Structures/Color.cs: -------------------------------------------------------------------------------- 1 | public class Color 2 | { 3 | public byte RedValue { get; set; } 4 | public byte GreenValue { get; set; } 5 | public byte BlueValue { get; set; } 6 | } 7 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Structures/Edges.cs: -------------------------------------------------------------------------------- 1 | public enum Edges 2 | { 3 | Straight, 4 | Rounded 5 | } 6 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Structures/Point.cs: -------------------------------------------------------------------------------- 1 | public struct Point 2 | { 3 | public int X { get; set; } 4 | public int Y { get; set; } 5 | 6 | public void Move(int x, int y) 7 | { 8 | this.X += x; 9 | this.Y += y; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Structures/Square.cs: -------------------------------------------------------------------------------- 1 | public struct Square 2 | { 3 | public Point Location { get; set; } 4 | public int Size { get; set; } 5 | public Color SurfaceColor { get; set; } 6 | public Color BorderColor { get; set; } 7 | public Edges Edges { get; set; } 8 | 9 | public Square(Point location, int size, Color surfaceColor, 10 | Color borderColor, Edges edges) : this() 11 | { 12 | this.Location = location; 13 | this.Size = size; 14 | this.SurfaceColor = surfaceColor; 15 | this.BorderColor = borderColor; 16 | this.Edges = edges; 17 | } 18 | 19 | public override string ToString() 20 | { 21 | string squareAsString = string.Format( 22 | "Square[" + "\n" + 23 | " location({0},{1})," + "\n" + 24 | " size({2})," + "\n" + 25 | " edges({3}), " + "\n" + 26 | " surface(#{4:X2}{5:X2}{6:X2})," + "\n" + 27 | " border(#{7:X2}{8:X2}{9:X2})" + "\n" + 28 | "]", 29 | this.Location.X, this.Location.Y, this.Size, this.Edges, 30 | this.SurfaceColor.RedValue, this.SurfaceColor.GreenValue, 31 | this.SurfaceColor.BlueValue, this.BorderColor.RedValue, 32 | this.BorderColor.GreenValue, this.BorderColor.BlueValue); 33 | return squareAsString; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /06. OOP-Other-Types-in-OOP-Demos/Structures/StructuresExample.cs: -------------------------------------------------------------------------------- 1 | public class StructuresExample 2 | { 3 | static void Main() 4 | { 5 | Color c = new Color() 6 | { 7 | RedValue = 1, 8 | GreenValue = 2, 9 | BlueValue = 3 10 | }; 11 | 12 | Square square = new Square( 13 | new Point() { X = 5, Y = -3 }, 14 | 7, 15 | new Color() { RedValue = 73, GreenValue = 158, BlueValue = 76 }, 16 | new Color() { RedValue = 0, GreenValue = 255, BlueValue = 133 }, 17 | Edges.Rounded 18 | ); 19 | System.Console.WriteLine(square); 20 | 21 | square.Edges = Edges.Straight; 22 | square.BorderColor = new Color() 23 | { 24 | RedValue = 0, 25 | GreenValue = 0, 26 | BlueValue = 0 27 | }; 28 | 29 | // Note: this will not compile (Point is value-type) 30 | // square.Location.X = square.Location.X + 10; 31 | 32 | Point location = square.Location; 33 | location.X += 10; 34 | square.Location = location; 35 | 36 | System.Console.WriteLine(square); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Action-and-Func/ActionAndFunc.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ActionAndFunc 4 | { 5 | static void Main() 6 | { 7 | Func intParseFunction = int.Parse; 8 | int num = intParseFunction("10"); 9 | 10 | Action printNumberAction = Console.WriteLine; 11 | printNumberAction(num); 12 | 13 | Execute(intParseFunction, printNumberAction, "-50"); 14 | Execute(double.Parse, Console.WriteLine, "3.14"); 15 | 16 | Action printNameAge = 17 | (name, age) => 18 | { 19 | Console.WriteLine("Name: " + name); 20 | Console.WriteLine("Age: " + age); 21 | }; 22 | 23 | printNameAge("Pesho", 5); 24 | 25 | Func magic = () => 26 | { 27 | return "Magic"; 28 | }; 29 | 30 | Console.WriteLine(magic); 31 | Console.WriteLine(magic()); 32 | } 33 | 34 | static void Execute(Func func, Action action, T1 value) 35 | { 36 | action(func(value)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/AnonymousMethods/AnonymousMethods.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows.Forms; 3 | 4 | class AnonymousMethods 5 | { 6 | static void Main() 7 | { 8 | // Method syntax 9 | Action action = ShowMsg; 10 | 11 | // Delegate syntax 12 | action = delegate(string msg) 13 | { 14 | MessageBox.Show(msg); 15 | }; 16 | 17 | // Lambda syntax 18 | action = (msg) => 19 | { 20 | MessageBox.Show(msg); 21 | }; 22 | 23 | action("Hello!"); 24 | } 25 | 26 | static void ShowMsg(string msg) 27 | { 28 | MessageBox.Show(msg); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/CustomLinqExtensions/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/CustomLinqExtensions/CustomLinqExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace CustomLinqExtensions 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | class CustomLinqExtensions 7 | { 8 | static void Main() 9 | { 10 | var elements = new List() 11 | { 12 | 40, 50, 60, 5, 10, 20, 30, 13 | }; 14 | 15 | var ordered = elements.SortBy(x => x); 16 | 17 | Console.WriteLine(string.Join(", ", ordered)); 18 | 19 | var students = new List() 20 | { 21 | new Student("Gosho", 20, 4.5), 22 | new Student("Pesho", 17, 2), 23 | new Student("Tanio", 23, 6.00), 24 | new Student("Ivan", 21, 5.5) 25 | }; 26 | 27 | var goodStudents = students 28 | .Filter(st => st.AverageGrade > 5) 29 | .SortBy(st => st.Age) 30 | .Project(st => st.Name); 31 | 32 | Console.WriteLine(string.Join("\n", goodStudents)); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/CustomLinqExtensions/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CustomLinqExtensions")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CustomLinqExtensions")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f7b45d06-942c-43b7-b1d2-d7ba13517e72")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/CustomLinqExtensions/Student.cs: -------------------------------------------------------------------------------- 1 | namespace CustomLinqExtensions 2 | { 3 | public class Student 4 | { 5 | public Student(string name, int age, double averageGrade) 6 | { 7 | this.Name = name; 8 | this.Age = age; 9 | this.AverageGrade = averageGrade; 10 | } 11 | 12 | public string Name { get; set; } 13 | 14 | public int Age { get; set; } 15 | 16 | public double AverageGrade { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Delegates-Example/DelegatesExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | // Declaration of a delegate 4 | public delegate void SimpleDelegate(string param); 5 | 6 | public class DelegatesExample 7 | { 8 | static void TestMethod(string param) 9 | { 10 | Console.WriteLine("I was called by a delegate."); 11 | Console.WriteLine("I got parameter: {0}.", param); 12 | } 13 | 14 | static void Main() 15 | { 16 | // Instantiate the delegate 17 | SimpleDelegate d = new SimpleDelegate(TestMethod); 18 | 19 | // The above can be written in a shorter way 20 | d = TestMethod; 21 | 22 | // Invocation of the method, pointed by delegate 23 | d("test"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventHandlers/8. EventHandler-Example.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {1C22DE0F-1AA3-48BF-AD24-2A480B84939F} 9 | Exe 10 | Properties 11 | _7.EventHandlerDemo 12 | _7.EventHandlerDemo 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | x86 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | x86 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 54 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventHandlers/Button.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Button 4 | { 5 | public event EventHandler Click; 6 | //public event EventHandler GotFocus; 7 | //public event EventHandler TextChanged; 8 | 9 | protected void OnClick() 10 | { 11 | if (this.Click != null) 12 | { 13 | this.Click(this, new EventArgs()); 14 | } 15 | } 16 | 17 | public void FireClick() 18 | { 19 | this.OnClick(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventHandlers/ButtonExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class ButtonExample 4 | { 5 | private static void Button_Click(object sender, EventArgs eventArgs) 6 | { 7 | Console.WriteLine("Button_Click() event called."); 8 | } 9 | 10 | public static void Main() 11 | { 12 | Button button = new Button(); 13 | button.Click += new EventHandler(Button_Click); 14 | button.FireClick(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Events/TimeChangedEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class TimeChangedEventArgs : EventArgs 4 | { 5 | private int ticksLeft; 6 | 7 | public TimeChangedEventArgs(int ticksLeft) 8 | { 9 | this.ticksLeft = ticksLeft; 10 | } 11 | 12 | public int TicksLeft 13 | { 14 | get { return this.ticksLeft; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Events/TimeChangedEventHandler.cs: -------------------------------------------------------------------------------- 1 | public delegate void TimeChangedEventHandler(object sender, TimeChangedEventArgs eventArgs); 2 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Events/Timer.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | 3 | public class Timer 4 | { 5 | private int tickCount; 6 | private int interval; 7 | 8 | public event TimeChangedEventHandler TimeChanged; 9 | 10 | public Timer(int tickCount, int interval) 11 | { 12 | this.tickCount = tickCount; 13 | this.interval = interval; 14 | } 15 | 16 | public int TickCount 17 | { 18 | get 19 | { 20 | return tickCount; 21 | } 22 | } 23 | 24 | public int Interval 25 | { 26 | get 27 | { 28 | return interval; 29 | } 30 | } 31 | 32 | protected void OnTimeChanged(int tick) 33 | { 34 | if (this.TimeChanged != null) 35 | { 36 | TimeChangedEventArgs args = new TimeChangedEventArgs(tick); 37 | this.TimeChanged(this, args); 38 | } 39 | } 40 | 41 | public void Run() 42 | { 43 | int tick = this.tickCount; 44 | while (tick > 0) 45 | { 46 | Thread.Sleep(this.interval); 47 | tick--; 48 | this.OnTimeChanged(tick); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Events/TimerDemo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class TimerDemo 4 | { 5 | public static void Main() 6 | { 7 | Timer timer = new Timer(10, 1000); 8 | timer.TimeChanged += new TimeChangedEventHandler(Timer_TimeChanged); 9 | 10 | Console.WriteLine("Timer started for 10 ticks at interval 1000 ms."); 11 | timer.Run(); 12 | } 13 | 14 | private static void Timer_TimeChanged(object sender, 15 | TimeChangedEventArgs eventArgs) 16 | { 17 | Console.WriteLine("Timer! Ticks left = {0}", eventArgs.TicksLeft); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventsAndInterfaces/Button.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public class Button : IClickable 4 | { 5 | private ClickEventHandler click; 6 | 7 | // Implement the event from the interface IClickable 8 | public event ClickEventHandler Click 9 | { 10 | add 11 | { 12 | click += value; 13 | Console.WriteLine("Subscribed to Button.Clicked event."); 14 | } 15 | remove 16 | { 17 | click -= value; 18 | Console.WriteLine("Unsubscribed to Button.Clicked event."); 19 | } 20 | } 21 | 22 | protected void OnClick() 23 | { 24 | if (click != null) 25 | { 26 | click(this, EventArgs.Empty); 27 | } 28 | } 29 | 30 | public void FireClick() 31 | { 32 | Console.WriteLine("Button.FireClick() called."); 33 | OnClick(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventsAndInterfaces/ButtonTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class ButtonTest 4 | { 5 | private static void Button_Click(object sender, EventArgs eventArgs) 6 | { 7 | Console.WriteLine("Button_Click() event called."); 8 | } 9 | 10 | static void Main() 11 | { 12 | Button button = new Button(); 13 | button.Click += new ClickEventHandler(Button_Click); 14 | button.FireClick(); 15 | button.Click -= new ClickEventHandler(Button_Click); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventsAndInterfaces/ClickEventHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | public delegate void ClickEventHandler(object sender, EventArgs eventArgs); 4 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/EventsAndInterfaces/IClickable.cs: -------------------------------------------------------------------------------- 1 | public interface IClickable 2 | { 3 | event ClickEventHandler Click; 4 | } -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/MulticastDelegates/MultiDelegates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | delegate int StringDelegate(string value); 4 | 5 | public class MultiDelegates 6 | { 7 | static int PrintString(string str) 8 | { 9 | Console.WriteLine("Str: {0}", str); 10 | return 1; 11 | } 12 | 13 | int PrintStringLength(string value) 14 | { 15 | Console.WriteLine("Length: {0}", value.Length); 16 | return 2; 17 | } 18 | 19 | public static void Main() 20 | { 21 | var del = new StringDelegate(PrintString); 22 | var md = new MultiDelegates(); 23 | del += md.PrintStringLength; 24 | 25 | int result = del("Pesho"); 26 | Console.WriteLine(result); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Predicates/5. Predicates-Example.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | x86 6 | 8.0.30703 7 | 2.0 8 | {817E4C16-6F2A-40D9-AE9B-A2FC15290DAE} 9 | Exe 10 | Properties 11 | _5.Predicates 12 | _5.Predicates 13 | v4.0 14 | Client 15 | 512 16 | 17 | 18 | x86 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | x86 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 53 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/Predicates/PredicatesExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | class PredicatesExample 6 | { 7 | static void Main() 8 | { 9 | var elements = new List() 10 | { 11 | 40, 50, 60, 5, 10, 20, 30, 12 | }; 13 | 14 | // Lambda expression with body 15 | var divisorsWithoutRemainder = elements 16 | .Where(delegate(int x) 17 | { 18 | return x % 3 == 0; 19 | }); 20 | 21 | // Inline lambda expression 22 | divisorsWithoutRemainder = elements 23 | .Where(x => x % 3 == 0); 24 | 25 | // Passing method that is Func 26 | divisorsWithoutRemainder = elements 27 | .Where(DividesByThree); 28 | 29 | // Passing Func delegate 30 | Func dividesByThreePredicate = DividesByThree; 31 | divisorsWithoutRemainder = elements 32 | .Where(dividesByThreePredicate); 33 | 34 | Console.WriteLine(string.Join(", ", divisorsWithoutRemainder)); 35 | } 36 | 37 | static bool DividesByThree(int num) 38 | { 39 | return num % 3 == 0; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/UIApplication/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/UIApplication/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/UIApplication/App.xaml.cs: -------------------------------------------------------------------------------- 1 | namespace UIApplication 2 | { 3 | using System.Windows; 4 | 5 | /// 6 | /// Interaction logic for App.xaml 7 | /// 8 | public partial class App : Application 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /07. OOP-Delegates-and-Events-Demos/UIApplication/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 |