├── .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 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/UIApplication/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | namespace UIApplication
2 | {
3 | using System.Net;
4 | using System.Windows;
5 | using System.Windows.Controls;
6 | using System.Windows.Input;
7 |
8 | ///
9 | /// Interaction logic for MainWindow.xaml
10 | ///
11 | public partial class MainWindow : Window
12 | {
13 | public MainWindow()
14 | {
15 | this.InitializeComponent();
16 |
17 | this.MouseDown += this.MainWindow_MouseClick;
18 |
19 | var button = (Button) this.FindName("DownloadButton");
20 | button.Click += this.Button_Click;
21 | }
22 |
23 | private void MainWindow_MouseClick(object sender, MouseButtonEventArgs e)
24 | {
25 | MessageBox.Show(string.Format("Mouse clicked at ({0}, {1})",
26 | e.MouseDevice.GetPosition(this).X, e.MouseDevice.GetPosition(this).Y));
27 | }
28 |
29 | private async void Button_Click(object sender, RoutedEventArgs e)
30 | {
31 | var textBox = (TextBox) this.FindName("UrlBox");
32 | string url = textBox.Text;
33 |
34 | var progress = (Label) this.FindName("Progress");
35 |
36 | using (var client = new WebClient())
37 | {
38 | progress.Content = "Downloading...";
39 | await client.DownloadFileTaskAsync(url, "../../site.html");
40 |
41 | progress.Content = "Done!";
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/UIApplication/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("WpfApplication1")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("WpfApplication1")]
15 | [assembly: AssemblyCopyright("Copyright © 2015")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/UIApplication/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.34014
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace UIApplication.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/UIApplication/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/WallStreet/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/WallStreet/IBM.cs:
--------------------------------------------------------------------------------
1 | namespace WallStreet
2 | {
3 | ///
4 | /// The 'ConcreteSubject' class
5 | ///
6 | public class IBM : Stock
7 | {
8 | public IBM(string symbol, double price)
9 | : base(symbol, price)
10 | {
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/WallStreet/Program.cs:
--------------------------------------------------------------------------------
1 | namespace WallStreet
2 | {
3 | using System;
4 |
5 | public class Program
6 | {
7 | static void Main()
8 | {
9 | // Create IBM stock
10 | var ibm = new IBM("IBM", 120.00);
11 |
12 | // Attach event handler to StockChange event
13 | ibm.StockChange += (stock, args) =>
14 | {
15 | Console.WriteLine("Stock has changed from {0:F2} to {1:F2}",
16 | args.OldPrice, args.NewPrice);
17 | };
18 |
19 | // Fluctuating prices will fire the event
20 | ibm.Price = 120.10;
21 | ibm.Price = 121.00;
22 | ibm.Price = 120.50;
23 | ibm.Price = 120.75;
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/WallStreet/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("WallStreet")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("WallStreet")]
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("322851f3-84f2-474a-8508-dee537eb8514")]
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/WallStreet/Stock.cs:
--------------------------------------------------------------------------------
1 | namespace WallStreet
2 | {
3 | using System;
4 |
5 | public delegate void OnStockChangeEventHandler(Stock sender, StockChangedEventArgs args);
6 |
7 | ///
8 | /// The 'Subject' abstract class
9 | ///
10 | public abstract class Stock
11 | {
12 | public event OnStockChangeEventHandler StockChange;
13 |
14 | private readonly string symbol;
15 | private double price;
16 |
17 | protected Stock(string symbol, double price)
18 | {
19 | this.symbol = symbol;
20 | this.price = price;
21 | }
22 |
23 | public double Price
24 | {
25 | get
26 | {
27 | return this.price;
28 | }
29 |
30 | set
31 | {
32 | if (Math.Abs(this.price - value) > 0.001)
33 | {
34 | if (this.StockChange != null)
35 | {
36 | this.StockChange(this,
37 | new StockChangedEventArgs(this.price, value));
38 | }
39 |
40 | this.price = value;
41 | }
42 | }
43 | }
44 |
45 | public string Symbol
46 | {
47 | get { return this.symbol; }
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/07. OOP-Delegates-and-Events-Demos/WallStreet/StockChangedEventArgs.cs:
--------------------------------------------------------------------------------
1 | namespace WallStreet
2 | {
3 | using System;
4 |
5 | public class StockChangedEventArgs : EventArgs
6 | {
7 | public StockChangedEventArgs(double oldPrice, double newPrice)
8 | {
9 | this.OldPrice = oldPrice;
10 | this.NewPrice = newPrice;
11 | }
12 |
13 | public double OldPrice { get; set; }
14 |
15 | public double NewPrice { get; set; }
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/OOP-Workshop-2015-06-17.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.31101.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperRpgGame", "SuperRpgGame\SuperRpgGame.csproj", "{15E08928-D995-4869-B403-A54C9D55B7DB}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {15E08928-D995-4869-B403-A54C9D55B7DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {15E08928-D995-4869-B403-A54C9D55B7DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {15E08928-D995-4869-B403-A54C9D55B7DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {15E08928-D995-4869-B403-A54C9D55B7DB}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Attributes/EnemyAttribute.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Attributes
2 | {
3 | using System;
4 |
5 | [AttributeUsage(AttributeTargets.Class)]
6 | public class EnemyAttribute : Attribute
7 | {
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/Character.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public abstract class Character : GameObject, ICharacter
7 | {
8 | private string name;
9 |
10 | protected Character(Position position, char objectSymbol, string name, int damage, int health)
11 | : base(position, objectSymbol)
12 | {
13 | this.Damage = damage;
14 | this.Health = health;
15 | this.Name = name;
16 | }
17 |
18 | public int Damage { get; set; }
19 |
20 | public int Health { get; set; }
21 |
22 | public string Name
23 | {
24 | get
25 | {
26 | return this.name;
27 | }
28 |
29 | private set
30 | {
31 | if (string.IsNullOrWhiteSpace(value))
32 | {
33 | throw new ArgumentNullException("name", "Name cannot be null, empty or whitespace.");
34 | }
35 |
36 | this.name = value;
37 | }
38 | }
39 |
40 | public void Attack(ICharacter enemy)
41 | {
42 | enemy.Health -= this.Damage;
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/Fairy.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | using Attributes;
4 |
5 | [Enemy]
6 | public class Fairy : Character
7 | {
8 | private const int FairyDamage = 100;
9 | private const int FairyHealth = 60;
10 | private const char FairySymbol = 'F';
11 |
12 | public Fairy(Position position, string name)
13 | : base(position, FairySymbol, name, FairyDamage, FairyHealth)
14 | {
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/Ninja.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | using Attributes;
4 |
5 | [Enemy]
6 | public class Ninja : Character
7 | {
8 | private const int NinjaDamage = 150;
9 | private const int NinjaHealth = 300;
10 | private const char NinjaSymbol = 'N';
11 |
12 | public Ninja(Position position, string name)
13 | : base(position, NinjaSymbol, name, NinjaDamage, NinjaHealth)
14 | {
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/Ork.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | using Attributes;
4 |
5 | [Enemy]
6 | public class Ork : Character
7 | {
8 | private const int OrkDamage = 100;
9 | private const int OrkHealth = 150;
10 | private const char OrkSymbol = 'O';
11 |
12 | public Ork(Position position, string name)
13 | : base(position, OrkSymbol, name, OrkDamage, OrkHealth)
14 | {
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/PlayerRace.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | public enum PlayerRace
4 | {
5 | Elf = 1,
6 | Archangel = 2,
7 | Hulk = 3,
8 | Alcoholic = 4
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Characters/Troll.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Characters
2 | {
3 | using Attributes;
4 |
5 | [Enemy]
6 | public class Troll : Character
7 | {
8 | private const int TrollDamage = 75;
9 | private const int TrollHealth = 400;
10 | private const char TrollSymbol = 'T';
11 |
12 | public Troll(Position position, string name)
13 | : base(position, TrollSymbol, name, TrollDamage, TrollHealth)
14 | {
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Exceptions/NotEnoughBeerException.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Exceptions
2 | {
3 | using System;
4 |
5 | public class NotEnoughBeerException : Exception
6 | {
7 | public NotEnoughBeerException(string msg)
8 | : base(msg)
9 | {
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Exceptions/ObjectOutOfBoundsException.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Exceptions
2 | {
3 | using System;
4 |
5 | public class ObjectOutOfBoundsException : Exception
6 | {
7 | public ObjectOutOfBoundsException(string message)
8 | : base(message)
9 | {
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/GameObject.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame
2 | {
3 | using System;
4 | using Engine;
5 | using Exceptions;
6 |
7 | public abstract class GameObject
8 | {
9 | private Position position;
10 | private char objectSymbol;
11 |
12 | protected GameObject(Position position, char objectSymbol)
13 | {
14 | this.Position = position;
15 | this.ObjectSymbol = objectSymbol;
16 | }
17 |
18 | public Position Position
19 | {
20 | get
21 | {
22 | return this.position;
23 | }
24 |
25 | set
26 | {
27 | // Added upper-bound check
28 | if (value.X < 0
29 | || value.Y < 0
30 | || value.X >= SuperEngine.MapWidth
31 | || value.Y >= SuperEngine.MapHeight)
32 | {
33 | throw new ObjectOutOfBoundsException("Specified coordinates are outside map.");
34 | }
35 |
36 | this.position = value;
37 | }
38 | }
39 |
40 | public char ObjectSymbol
41 | {
42 | get
43 | {
44 | return this.objectSymbol;
45 | }
46 |
47 | set
48 | {
49 | if (!char.IsUpper(value))
50 | {
51 | throw new ArgumentOutOfRangeException(
52 | "objectSymbol",
53 | "Object symbol must be an upper-case letter.");
54 | }
55 |
56 | this.objectSymbol = value;
57 | }
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/HelpInfo.txt:
--------------------------------------------------------------------------------
1 | Game objective: Kill everyone!!!
2 | -help - displays basic game info and commands
3 | -map - displays the map
4 | -left, right, up, down - player movement
5 | -status - displays current player status and enemies left
6 | -enemies - displays info about enemies on map
7 | -clear - clears the screen
8 | -exit - run like a little girl
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IAttack.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IAttack
4 | {
5 | int Damage { get; set; }
6 |
7 | void Attack(ICharacter enemy);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/ICharacter.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface ICharacter : IAttack, IDestroyable
4 | {
5 | string Name { get; }
6 |
7 | Position Position { get; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/ICollect.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | using System.Collections.Generic;
4 | using Items;
5 |
6 | public interface ICollect
7 | {
8 | IEnumerable- Inventory { get; }
9 |
10 | void AddItemToInventory(Item item);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IDestroyable.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IDestroyable
4 | {
5 | int Health { get; set; }
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IHeal.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IHeal
4 | {
5 | void Heal();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IInputReader.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IInputReader
4 | {
5 | string ReadLine();
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IMoveable.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IMoveable
4 | {
5 | void Move(string direction);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IPlayer.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | using Characters;
4 |
5 | public interface IPlayer : ICharacter, IMoveable, ICollect, IHeal
6 | {
7 | PlayerRace Race { get; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Interfaces/IRenderer.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Interfaces
2 | {
3 | public interface IRenderer
4 | {
5 | void WriteLine(string message, params object[] paramaters);
6 |
7 | void Clear();
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Items/Beer.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Items
2 | {
3 | public class Beer : Item
4 | {
5 | private const char BeerSymbol = 'B';
6 |
7 | public Beer(Position position, BeerSize beerSize)
8 | : base(position, BeerSymbol)
9 | {
10 | this.BeerSize = beerSize;
11 | }
12 |
13 | public int HealthRestore
14 | {
15 | get
16 | {
17 | return (int)this.BeerSize;
18 | }
19 | }
20 |
21 | private BeerSize BeerSize { get; set; }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Items/BeerSize.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Items
2 | {
3 | public enum BeerSize
4 | {
5 | Small = 50,
6 | Medium = 75,
7 | Large = 100
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Items/Item.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Items
2 | {
3 | public abstract class Item : GameObject
4 | {
5 | protected Item(Position position, char itemSymbol)
6 | : base(position, itemSymbol)
7 | {
8 | this.ItemState = ItemState.Available;
9 | }
10 |
11 | public ItemState ItemState { get; set; }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Items/ItemState.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.Items
2 | {
3 | public enum ItemState
4 | {
5 | Available,
6 | Collected
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/Position.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame
2 | {
3 | public struct Position
4 | {
5 | public Position(int x, int y)
6 | : this()
7 | {
8 | this.X = x;
9 | this.Y = y;
10 | }
11 |
12 | public int X { get; set; }
13 |
14 | public int Y { get; set; }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/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("SuperRpgGame")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SuperRpgGame")]
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("4a48750a-5155-4541-a3fc-ddd951f6de31")]
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 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/SuperRpgGameApp.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame
2 | {
3 | using Engine;
4 | using Interfaces;
5 | using UI;
6 |
7 | public class SuperRpgGameApp
8 | {
9 | public static void Main()
10 | {
11 | IRenderer renderer = new ConsoleRenderer();
12 | IInputReader reader = new ConsoleInputReader();
13 |
14 | SuperEngine engine = new SuperEngine(reader, renderer);
15 |
16 | engine.Run();
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/UI/ConsoleInputReader.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.UI
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public class ConsoleInputReader : IInputReader
7 | {
8 | public string ReadLine()
9 | {
10 | return Console.ReadLine();
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/08. OOP-Workshop-RPG-Game/SuperRpgGame/UI/ConsoleRenderer.cs:
--------------------------------------------------------------------------------
1 | namespace SuperRpgGame.UI
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public class ConsoleRenderer : IRenderer
7 | {
8 | public void WriteLine(string message, params object[] paramaters)
9 | {
10 | Console.WriteLine(message, paramaters);
11 | }
12 |
13 | public void Clear()
14 | {
15 | Console.Clear();
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/LoggerTests/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/LoggerTests/JsonFormatter.cs:
--------------------------------------------------------------------------------
1 | using SOLIDLogger;
2 | namespace LoggerTests
3 | {
4 | using System;
5 | using System.Text;
6 | using SOLIDLogger.Interfaces;
7 |
8 | public class JsonFormatter : IFormatter
9 | {
10 | public int Limit { get; set; }
11 |
12 | public string Format(string msg, ReportLevel level, DateTime date)
13 | {
14 | StringBuilder output = new StringBuilder();
15 | output.AppendLine("{");
16 | output.AppendLine(string.Format(" \"text\" : \"{0}\"", msg));
17 | output.AppendLine(string.Format(" \"level\" : \"{0}\"", level));
18 | output.AppendLine(string.Format(" \"date\" : \"{0}\"", date));
19 | output.AppendLine("}");
20 |
21 | return output.ToString();
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/LoggerTests/Program.cs:
--------------------------------------------------------------------------------
1 | namespace LoggerTests
2 | {
3 | using SOLIDLogger;
4 | using SOLIDLogger.Appenders;
5 | using SOLIDLogger.Formatters;
6 |
7 | class Program
8 | {
9 | public static void Main()
10 | {
11 | var simpleFormatter = new SimpleFormatter();
12 | var xmlFormatter = new XmlFormatter();
13 | // Create own formatter
14 | var jsonFormatter = new JsonFormatter();
15 |
16 | var fileAppender = new FileAppender(jsonFormatter, "log.txt");
17 | var consoleAppender = new ConsoleAppender(xmlFormatter);
18 | // Create own appender
19 | var sqlAppender = new SqlAppender(jsonFormatter);
20 |
21 | Logger logger = new Logger(consoleAppender, fileAppender);
22 |
23 | logger.Critical("Out of memory");
24 | logger.Info("Unused local variable 'name'");
25 |
26 | fileAppender.Close();
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/LoggerTests/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("LoggerTests")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("LoggerTests")]
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("2b6220d5-9112-4966-baf5-aa760fc3e9ac")]
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 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/LoggerTests/SqlAppender.cs:
--------------------------------------------------------------------------------
1 | namespace LoggerTests
2 | {
3 | using System;
4 | using SOLIDLogger;
5 | using SOLIDLogger.Appenders;
6 | using SOLIDLogger.Interfaces;
7 |
8 | public class SqlAppender : Appender
9 | {
10 | public SqlAppender(IFormatter formatter)
11 | : base(formatter)
12 | {
13 | }
14 |
15 | public override void Append(string msg, ReportLevel level, DateTime date)
16 | {
17 | // TODO: ...
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Appenders/Appender.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Appenders
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public abstract class Appender : IAppender
7 | {
8 | protected Appender(IFormatter formatter)
9 | {
10 | this.Formatter = formatter;
11 | }
12 |
13 | public IFormatter Formatter { get; private set; }
14 |
15 | public abstract void Append(string msg, ReportLevel level, DateTime date);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Appenders/ConsoleAppender.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Appenders
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public class ConsoleAppender : Appender
7 | {
8 | public ConsoleAppender(IFormatter formatter)
9 | : base(formatter)
10 | {
11 | }
12 |
13 | public override void Append(string msg, ReportLevel level, DateTime date)
14 | {
15 | var output = this.Formatter.Format(msg, level, date);
16 | Console.WriteLine(output);
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Appenders/FileAppender.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Appenders
2 | {
3 | using System;
4 | using System.IO;
5 | using Interfaces;
6 |
7 | public class FileAppender : Appender
8 | {
9 | private readonly StreamWriter writer;
10 |
11 | public FileAppender(IFormatter formatter, string path)
12 | : base(formatter)
13 | {
14 | this.Path = path;
15 | this.writer = new StreamWriter(this.Path);
16 | }
17 |
18 | public string Path { get; private set; }
19 |
20 | public override void Append(string msg, ReportLevel level, DateTime date)
21 | {
22 | var output = this.Formatter.Format(msg, level, date);
23 |
24 | this.writer.WriteLine(output);
25 | this.writer.Flush();
26 | }
27 |
28 | public void Close()
29 | {
30 | this.writer.Close();
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Formatters/SimpleFormatter.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Formatters
2 | {
3 | using System;
4 | using Interfaces;
5 |
6 | public class SimpleFormatter : IFormatter
7 | {
8 | public string Format(string msg, ReportLevel level, DateTime date)
9 | {
10 | return string.Format("{0} - {1} - {2}", msg, level, date);
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Formatters/XmlFormatter.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Formatters
2 | {
3 | using System;
4 | using System.Text;
5 | using Interfaces;
6 |
7 | public class XmlFormatter : IFormatter
8 | {
9 | private static readonly string Indentation = new string(' ', 2);
10 |
11 | public string Format(string msg, ReportLevel level, DateTime date)
12 | {
13 | StringBuilder output = new StringBuilder();
14 | output.AppendLine("");
15 | output.AppendLine(string.Format("{1}{0}", msg, Indentation));
16 | output.AppendLine(string.Format("{1}{0}", level, Indentation));
17 | output.AppendLine(string.Format("{1}{0}", date, Indentation));
18 | output.AppendLine("");
19 |
20 | return output.ToString();
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Interfaces/IAppender.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Interfaces
2 | {
3 | using System;
4 |
5 | public interface IAppender
6 | {
7 | IFormatter Formatter { get; }
8 |
9 | void Append(string msg, ReportLevel level, DateTime date);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Interfaces/IFormatter.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger.Interfaces
2 | {
3 | using System;
4 |
5 | public interface IFormatter
6 | {
7 | string Format(string msg, ReportLevel level, DateTime date);
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/Logger.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger
2 | {
3 | using System;
4 | using System.Collections.Generic;
5 | using Interfaces;
6 |
7 | public class Logger
8 | {
9 | public Logger(params IAppender[] appenders)
10 | {
11 | this.Appenders = new List(appenders);
12 | }
13 |
14 | public List Appenders { get; set; }
15 |
16 | public void Info(string msg)
17 | {
18 | this.Log(msg, ReportLevel.Info);
19 | }
20 |
21 | public void Warn(string msg)
22 | {
23 | this.Log(msg, ReportLevel.Warn);
24 | }
25 |
26 | public void Error(string msg)
27 | {
28 | this.Log(msg, ReportLevel.Error);
29 | }
30 |
31 | public void Critical(string msg)
32 | {
33 | this.Log(msg, ReportLevel.Critical);
34 | }
35 |
36 | public void Fatal(string msg)
37 | {
38 | this.Log(msg, ReportLevel.Fatal);
39 | }
40 |
41 | private void Log(string msg, ReportLevel level)
42 | {
43 | var date = DateTime.Now;
44 |
45 | foreach (var appender in this.Appenders)
46 | {
47 | appender.Append(msg, level, date);
48 | }
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/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("SOLIDLogger")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SOLIDLogger")]
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("006894f3-33d2-47bd-a491-27e4eeb72c78")]
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 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/SOLIDLogger/ReportLevel.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger
2 | {
3 | public enum ReportLevel
4 | {
5 | Info = 0,
6 | Warn = 1,
7 | Error = 2,
8 | Critical = 3,
9 | Fatal = 4
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/App.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/App.xaml.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Configuration;
4 | using System.Data;
5 | using System.Linq;
6 | using System.Threading.Tasks;
7 | using System.Windows;
8 |
9 | namespace WpfApplication
10 | {
11 | ///
12 | /// Interaction logic for App.xaml
13 | ///
14 | public partial class App : Application
15 | {
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/ListBoxAppender.cs:
--------------------------------------------------------------------------------
1 | namespace WpfApplication
2 | {
3 | using System;
4 | using System.Windows.Controls;
5 | using SOLIDLogger;
6 | using SOLIDLogger.Appenders;
7 | using SOLIDLogger.Interfaces;
8 |
9 | public class ListBoxAppender : Appender
10 | {
11 | public ListBoxAppender(IFormatter formatter, ListBox listBox)
12 | : base(formatter)
13 | {
14 | this.ListBox = listBox;
15 | }
16 |
17 | public ListBox ListBox { get; private set; }
18 |
19 | public override void Append(string msg, ReportLevel level, DateTime date)
20 | {
21 | string output = this.Formatter.Format(msg, level, date);
22 |
23 | this.ListBox.Items.Add(output);
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/MainWindow.xaml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | namespace WpfApplication
2 | {
3 | using System.Windows;
4 | using System.Windows.Controls;
5 | using System.Windows.Input;
6 |
7 | using SOLIDLogger;
8 | using SOLIDLogger.Appenders;
9 | using SOLIDLogger.Formatters;
10 |
11 | ///
12 | /// Interaction logic for MainWindow.xaml
13 | ///
14 | public partial class MainWindow : Window
15 | {
16 | private Logger logger;
17 |
18 | public MainWindow()
19 | {
20 | this.InitializeComponent();
21 |
22 | var simpleFormatter = new SimpleFormatter();
23 |
24 | ListBox listBox = (ListBox) this.FindName("ListBox");
25 |
26 | var listBoxAppender = new ListBoxAppender(simpleFormatter, listBox);
27 | var fileAppender = new FileAppender(simpleFormatter, "simple.txt");
28 | this.logger = new Logger(fileAppender, listBoxAppender);
29 |
30 | this.KeyDown += this.MainWindow_KeyDown;
31 | }
32 |
33 | private void MainWindow_KeyDown(object sender, KeyEventArgs e)
34 | {
35 | this.logger.Info(e.Key.ToString());
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("WpfApplication")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("WpfApplication")]
15 | [assembly: AssemblyCopyright("Copyright © 2015")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.34014
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace WpfApplication.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/09. SolidLogger-Complete/WpfApplication/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/09. SolidLogger-Skeleton/SOLIDLogger/Logger.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger
2 | {
3 | using System;
4 |
5 | public class Logger
6 | {
7 | public void Info(string msg)
8 | {
9 | var date = DateTime.Now;
10 | Console.WriteLine("{0} - {1} - {2}", date, ReportLevel.Info, msg);
11 | }
12 |
13 | public void Warn(string msg)
14 | {
15 | var date = DateTime.Now;
16 | Console.WriteLine("{0} - {1} - {2}", date, ReportLevel.Info, msg);
17 | }
18 |
19 | public void Error(string msg)
20 | {
21 | var date = DateTime.Now;
22 | Console.WriteLine("{0} - {1} - {2}", date, ReportLevel.Info, msg);
23 | }
24 |
25 | public void Critical(string msg)
26 | {
27 | var date = DateTime.Now;
28 | Console.WriteLine("{0} - {1} - {2}", date, ReportLevel.Info, msg);
29 | }
30 |
31 | public void Fatal(string msg)
32 | {
33 | var date = DateTime.Now;
34 | Console.WriteLine("{0} - {1} - {2}", date, ReportLevel.Info, msg);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/09. SolidLogger-Skeleton/SOLIDLogger/Program.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger
2 | {
3 | class Program
4 | {
5 | public static void Main()
6 | {
7 | Logger logger = new Logger();
8 | logger.Critical("Out of memory");
9 | logger.Info("Unused local variable 'name'");
10 | }
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/09. SolidLogger-Skeleton/SOLIDLogger/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("SOLIDLogger")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SOLIDLogger")]
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("006894f3-33d2-47bd-a491-27e4eeb72c78")]
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 |
--------------------------------------------------------------------------------
/09. SolidLogger-Skeleton/SOLIDLogger/ReportLevel.cs:
--------------------------------------------------------------------------------
1 | namespace SOLIDLogger
2 | {
3 | public enum ReportLevel
4 | {
5 | Info = 0,
6 | Warn = 1,
7 | Error = 2,
8 | Critical = 3,
9 | Fatal = 4
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/09. SolidLogger-Skeleton/SolidLogger.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.31101.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOLIDLogger", "SOLIDLogger\SOLIDLogger.csproj", "{0A0B12FB-FF29-4BDA-B326-7A8C78C116EA}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {0A0B12FB-FF29-4BDA-B326-7A8C78C116EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {0A0B12FB-FF29-4BDA-B326-7A8C78C116EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {0A0B12FB-FF29-4BDA-B326-7A8C78C116EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {0A0B12FB-FF29-4BDA-B326-7A8C78C116EA}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | EndGlobal
23 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Boxing-Unboxing-Primitive-Types/07. Boxing-Unboxing-Primitive-Types.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.50727
7 | 2.0
8 | {7159805F-4940-45E6-A136-852503039C72}
9 | Exe
10 | Properties
11 | BoxingUnboxingPrimitiveTypes
12 | BoxingUnboxingPrimitiveTypes
13 | v2.0
14 |
15 |
16 |
17 |
18 | 2.0
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
56 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Boxing-Unboxing-Primitive-Types/BoxingUnboxing.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | class BoxingUnboxing
4 | {
5 | static void Main(string[] args)
6 | {
7 | int value1 = 1;
8 | object obj = value1; // boxing performed
9 |
10 | value1 = 12345; // only stack value is changed
11 |
12 | int value2 = (int)obj; // unboxing performed
13 | Console.WriteLine(value2); // prints 1
14 |
15 | long value3 = (long)(int)obj; // unboxing
16 | long value4 = (long)obj; // InvalidCastException
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Boxing-and-Unboxing/08. Boxing-and-Unboxing.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.50727
7 | 2.0
8 | {622B19A8-88C9-4269-91C5-35DF4A8BF853}
9 | Exe
10 | Properties
11 | BoxingAndUnboxing
12 | BoxingAndUnboxing
13 | v2.0
14 |
15 |
16 |
17 |
18 | 2.0
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 | x86
29 |
30 |
31 | pdbonly
32 | true
33 | bin\Release\
34 | TRACE
35 | prompt
36 | 4
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
57 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Boxing-and-Unboxing/BoxingAndUnboxing.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BoxingAndUnboxing
4 | {
5 | interface IMovable
6 | {
7 | void Move(int x, int y);
8 | }
9 |
10 | // Very bad practice! Structures should
11 | // contain no logic, but only data!
12 | struct Point : IMovable
13 | {
14 | public int x, y;
15 |
16 | public void Move(int x, int y)
17 | {
18 | this.x += x;
19 | this.y += y;
20 | }
21 |
22 | public override string ToString()
23 | {
24 | return String.Format("({0},{1})", x, y);
25 | }
26 | }
27 |
28 | class TestBoxingUnboxing
29 | {
30 | static void Main()
31 | {
32 | Point p1 = new Point();
33 | Console.WriteLine("p1={0}", p1); // p1=(0,0)
34 |
35 | IMovable p1mov = (IMovable)p1; // p1 si boxed
36 | IMovable p2mov = (IMovable)p1mov;
37 | // p1mov is not boxed second time,
38 | // because it is already boxed
39 |
40 | Point p2 = (Point)p2mov; // p2mov is unboxed
41 | p1.Move(-100, -100);
42 | p2.Move(100, 100);
43 | Console.WriteLine("p1 is moved to {0}", p1); // p1=(-100,-100)
44 | Console.WriteLine("p2 is moved to {0}", p2); // p2=(100,100)
45 |
46 | p1mov.Move(5, 5);
47 | Console.WriteLine("p1mov is moved to {0} ", p1mov); // p1mov=(5,5)
48 | Console.WriteLine("p2mov is also moved to {0}", p2mov); // p2mov=(5,5)
49 |
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Implementing-IComparable/Implementing IComparable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | class Point : IComparable
4 | {
5 | public int X { get; set; }
6 | public int Y { get; set; }
7 |
8 | public int CompareTo(Point otherPoint)
9 | {
10 | if (this.X != otherPoint.X)
11 | {
12 | return (this.X - otherPoint.X);
13 | }
14 | if (this.Y != otherPoint.Y)
15 | {
16 | return (this.Y - otherPoint.Y);
17 | }
18 | return 0;
19 | }
20 |
21 | static void Main()
22 | {
23 | Point firstPoint = new Point { X = 3, Y = 5 };
24 | Point secondPoint = new Point { X = 3, Y = 3 };
25 | Point thirdPoint = new Point { X = 7, Y = 6 };
26 |
27 | // Comparing points
28 | Console.WriteLine(
29 | firstPoint.CompareTo(secondPoint));
30 | Console.WriteLine(
31 | firstPoint.CompareTo(thirdPoint));
32 | Console.WriteLine(
33 | firstPoint.CompareTo(firstPoint));
34 |
35 | // Sorting points
36 | Point[] points = new Point[]
37 | {
38 | firstPoint, secondPoint, thirdPoint, firstPoint, firstPoint,
39 | secondPoint, thirdPoint, secondPoint, firstPoint, thirdPoint
40 | };
41 | Array.Sort(points);
42 | foreach (var p in points)
43 | {
44 | Console.WriteLine("({0}, {1})", p.X, p.Y);
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Implementing-IEnumerable/ImplementingIEnumerable.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | class ImplementingIEnumerable
5 | {
6 | static void Main()
7 | {
8 | LinkedList list =
9 | new LinkedList("1st element",
10 | new LinkedList("2nd element",
11 | new LinkedList("3rd element")));
12 |
13 | foreach (var item in list)
14 | {
15 | Console.WriteLine(item);
16 | }
17 |
18 | //IEnumerator enumerator = list.GetEnumerator();
19 | //while (enumerator.MoveNext())
20 | //{
21 | // Console.WriteLine(enumerator.Current);
22 | //}
23 |
24 | //enumerator.Reset();
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Implementing-IEnumerable/LinkedList.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | class LinkedList : IEnumerable
5 | {
6 | public T Value { get; set; }
7 | public LinkedList NextNode { get; private set; }
8 |
9 | public LinkedList(T value, LinkedList nextNode = null)
10 | {
11 | this.Value = value;
12 | this.NextNode = nextNode;
13 | }
14 |
15 | IEnumerator IEnumerable.GetEnumerator()
16 | {
17 | // Call the generic version of the method
18 | return this.GetEnumerator();
19 | }
20 |
21 | public IEnumerator GetEnumerator()
22 | {
23 | LinkedList currentNode = this;
24 | while (currentNode != null)
25 | {
26 | yield return currentNode.Value;
27 | currentNode = currentNode.NextNode;
28 | }
29 |
30 | // return new LinkedListEnumerator(this);
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Implementing-IEnumerable/LinkedListEnumerator.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 |
4 | class LinkedListEnumerator : IEnumerator
5 | {
6 | private LinkedList initialNode;
7 | private LinkedList currentNode;
8 | private bool isStarted = false;
9 |
10 | public LinkedListEnumerator(LinkedList firstNode)
11 | {
12 | this.currentNode = firstNode;
13 | this.initialNode = firstNode;
14 | }
15 |
16 | public T Current
17 | {
18 | get { return this.currentNode.Value; }
19 | }
20 |
21 | object IEnumerator.Current
22 | {
23 | get { return this.currentNode.Value; }
24 | }
25 |
26 | public bool MoveNext()
27 | {
28 | if (!this.isStarted)
29 | {
30 | this.isStarted = true;
31 | return true;
32 | }
33 |
34 | if (this.currentNode.NextNode != null)
35 | {
36 | this.currentNode = this.currentNode.NextNode;
37 | return true;
38 | }
39 |
40 | return false;
41 | }
42 |
43 | public void Reset()
44 | {
45 | this.isStarted = false;
46 | this.currentNode = this.initialNode;
47 | }
48 |
49 | public void Dispose()
50 | {
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Is-and-as-Operators/IsAndAsOperators.cs:
--------------------------------------------------------------------------------
1 | namespace IsAndAsOperators
2 | {
3 | using System;
4 |
5 | class Shape { }
6 |
7 | class Triangle : Shape { }
8 |
9 | class PalyWithOperatorsIsAndAs
10 | {
11 | static void Main(string[] args)
12 | {
13 | Object objBase = new Shape();
14 | if (objBase is Shape)
15 | Console.WriteLine("objBase is Shape");
16 | // Result: objBase is Shape
17 |
18 | if (!(objBase is Triangle))
19 | Console.WriteLine("objBase is not Triangle");
20 | // Result : objBase is not Triangle
21 |
22 | if (objBase is Object)
23 | Console.WriteLine("objBase is System.Object");
24 | // Result : objBase is System.Object
25 |
26 | // "dynamic" is internally System.Object
27 | // but with runtime binding of the operations
28 | if (objBase is dynamic)
29 | Console.WriteLine("objBase is dynamic");
30 | // Result: objBase is dynamic
31 |
32 | Shape b = objBase as Shape;
33 | Console.WriteLine("b = {0}", b);
34 | // Result: b = Shape
35 |
36 | Triangle d = objBase as Triangle;
37 | if (d == null)
38 | Console.WriteLine("d is null");
39 | // Result: d is null
40 |
41 | Object o = objBase as object;
42 | Console.WriteLine("o = {0}", o);
43 | // Result: o = Shape
44 |
45 | Triangle der = new Triangle();
46 | Shape bas = der as Shape;
47 | Console.WriteLine("bas = {0}", bas);
48 | // Result: bas = Triangle
49 |
50 | Object dyn = objBase as dynamic;
51 | Console.WriteLine("dyn = {0}", dyn);
52 | // Result: o = Shape
53 | }
54 | }
55 | }
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Object-Cloning/LinkedList.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 |
4 | class LinkedList : ICloneable
5 | {
6 | public T Value { get; set; }
7 | public LinkedList NextNode { get; private set; }
8 |
9 | public LinkedList(T value, LinkedList nextNode = null)
10 | {
11 | this.Value = value;
12 | this.NextNode = nextNode;
13 | }
14 |
15 | object ICloneable.Clone() // Implicit interface implementation
16 | {
17 | return this.Clone();
18 | }
19 |
20 | public LinkedList ShallowCopy()
21 | {
22 | return this;
23 | }
24 |
25 | public LinkedList MemberwiseCopy()
26 | {
27 | return (LinkedList)this.MemberwiseClone();
28 | }
29 |
30 | public LinkedList Clone() // Deep cloning (public method)
31 | {
32 | // Copy the first element
33 | LinkedList original = this;
34 | T valueOriginal = original.Value;
35 | LinkedList result = new LinkedList(this.Value);
36 | LinkedList copy = result;
37 | original = original.NextNode;
38 |
39 | // Copy the rest of the elements
40 | while (original != null)
41 | {
42 | valueOriginal = original.Value;
43 | copy.NextNode = new LinkedList(valueOriginal);
44 | original = original.NextNode;
45 | copy = copy.NextNode;
46 | }
47 |
48 | return result;
49 | }
50 |
51 | public override string ToString()
52 | {
53 | LinkedList currentNode = this;
54 | StringBuilder sb = new StringBuilder("(");
55 | while (currentNode != null)
56 | {
57 | sb.Append(currentNode.Value);
58 | currentNode = currentNode.NextNode;
59 | if (currentNode != null)
60 | {
61 | sb.Append(", ");
62 | }
63 | }
64 | sb.Append(")");
65 |
66 | return sb.ToString();
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Object-Cloning/ObjectCloning.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | class ObjectCloning
4 | {
5 | static void Main()
6 | {
7 | LinkedList initialList =
8 | new LinkedList("Old fellow John",
9 | new LinkedList("Granny Yaga",
10 | new LinkedList("King Kiro")));
11 |
12 | LinkedList deeplyClonedList = initialList.Clone();
13 | deeplyClonedList.Value = "1st changed";
14 | deeplyClonedList.NextNode.Value = "2nd changed";
15 |
16 | Console.WriteLine("initial list = {0}", initialList);
17 | Console.WriteLine("deeply cloned list = {0}", deeplyClonedList);
18 |
19 | Console.WriteLine();
20 |
21 | initialList =
22 | new LinkedList("Old fellow John",
23 | new LinkedList("Granny Yaga",
24 | new LinkedList("King Kiro")));
25 |
26 | LinkedList shallowCopy = initialList.ShallowCopy();
27 | shallowCopy.Value = "1st changed";
28 | shallowCopy.NextNode.Value = "2nd changed";
29 |
30 | Console.WriteLine("initial list = {0}", initialList);
31 | Console.WriteLine("shallow cloned list = {0}", shallowCopy);
32 |
33 | Console.WriteLine();
34 |
35 | initialList =
36 | new LinkedList("Old fellow John",
37 | new LinkedList("Granny Yaga",
38 | new LinkedList("King Kiro")));
39 |
40 | LinkedList memberwiseCopy = initialList.MemberwiseCopy();
41 | memberwiseCopy.Value = "1st changed";
42 | memberwiseCopy.NextNode.Value = "2nd changed";
43 |
44 | Console.WriteLine("initial list = {0}", initialList);
45 | Console.WriteLine("memberwise cloned list = {0}", memberwiseCopy);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Passing-Out-Parameters/10. PassingOutParameters.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.50727
7 | 2.0
8 | {CD010D99-B2A5-4F13-9BB8-3FA7B33FA7C7}
9 | Exe
10 | Properties
11 | PassingOutParameters
12 | PassingOutParameters
13 | v2.0
14 |
15 |
16 |
17 |
18 | 2.0
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
56 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Passing-Out-Parameters/PassingOutParameters.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace PassingOutParameters
4 | {
5 | public struct Point
6 | {
7 | public int x, y;
8 | public Point(int x, int y)
9 | {
10 | this.x = x;
11 | this.y = y;
12 | }
13 | }
14 |
15 | public struct Dimensions
16 | {
17 | public int width, height;
18 | public Dimensions(int width, int height)
19 | {
20 | this.width = width;
21 | this.height = height;
22 | }
23 | }
24 |
25 | public class Rectangle
26 | {
27 | private int x, y, width, height;
28 | public Rectangle(int x, int y, int width, int height)
29 | {
30 | this.x = x;
31 | this.y = y;
32 | this.width = width;
33 | this.height = height;
34 | }
35 |
36 | public void GetLocationAndDimensions(
37 | out Point location, out Dimensions dimensions)
38 | {
39 | location = new Point(x, y);
40 | dimensions = new Dimensions(width, height);
41 | }
42 |
43 | static void Main()
44 | {
45 | Rectangle rect = new Rectangle(5, 10, 12, 8);
46 |
47 | Point location = new Point(0, 0);
48 | Dimensions dimensions = new Dimensions(1, 2);
49 |
50 | // Location and dimension are not pre-initialized!
51 | rect.GetLocationAndDimensions(out location, out dimensions);
52 |
53 | Console.WriteLine("({0}, {1}, {2}, {3})",
54 | location.x, location.y,
55 | dimensions.width, dimensions.height);
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Passing-Ref-Parameters/09. Passing-Ref-Parameters.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.50727
7 | 2.0
8 | {E9F270F6-C75A-45F5-A3FC-D76F3CCE7B51}
9 | Exe
10 | Properties
11 | PassingRefParameters
12 | PassingRefParameters
13 | v2.0
14 |
15 |
16 |
17 |
18 | 2.0
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
56 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Passing-Ref-Parameters/PassingRefParameters.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace PassingRefParameters
4 | {
5 | public struct Point
6 | {
7 | internal int x;
8 | internal int y;
9 | }
10 |
11 | public static class MultiplyPoint
12 | {
13 | public static void IncorrectMultiplyBy2(Point point)
14 | {
15 | point.x *= 2;
16 | point.y *= 2;
17 | }
18 |
19 | public static void MultiplyBy2(ref Point point)
20 | {
21 | point.x *= 2;
22 | point.y *= 2;
23 | }
24 | }
25 |
26 | public class Student
27 | {
28 | public string name;
29 | public Student(string name)
30 | {
31 | this.name = name;
32 | }
33 |
34 | static void IncorrectModifyStudent(Student student)
35 | {
36 | student = new Student("Changed: " + student.name);
37 | }
38 |
39 | static void ModifyStudent(ref Student student)
40 | {
41 | student = new Student("Changed: " + student.name);
42 | }
43 |
44 | static void Main(string[] args)
45 | {
46 | Console.WriteLine("Test passing class (reference type) by reference:");
47 | Student s = new Student("Nakov");
48 | Console.WriteLine(s.name);
49 | IncorrectModifyStudent(s);
50 | Console.WriteLine(s.name);
51 | ModifyStudent(ref s);
52 | Console.WriteLine(s.name);
53 | Console.WriteLine();
54 |
55 | Console.WriteLine("Test passing struct (value type) by reference:");
56 | Point p = new Point() { x = 5, y = -8 };
57 | Console.WriteLine("p=({0},{1})", p.x, p.y);
58 | MultiplyPoint.IncorrectMultiplyBy2(p);
59 | Console.WriteLine("p=({0},{1})", p.x, p.y);
60 | MultiplyPoint.MultiplyBy2(ref p);
61 | Console.WriteLine("p=({0},{1})", p.x, p.y);
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/System.Object-Overloading/OverloadingSystemObject.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | class OverloadingSystemObject
4 | {
5 | static void Main()
6 | {
7 | Student firstStudent = new Student();
8 | firstStudent.Name = "Bai Ivan";
9 | firstStudent.Age = 68;
10 | Console.WriteLine(firstStudent); // called firstStudent.ToString()
11 |
12 | Student secondStudent = new Student();
13 | if (firstStudent != secondStudent) // it is true
14 | Console.WriteLine("{0} != {1}", firstStudent, secondStudent);
15 |
16 | secondStudent.Name = "Bai Ivan";
17 | secondStudent.Age = 68;
18 | if (firstStudent == secondStudent) // it is true
19 | Console.WriteLine("{0} == {1}", firstStudent, secondStudent);
20 |
21 | secondStudent.Age = 70;
22 | if (firstStudent != secondStudent) // it is true
23 | Console.WriteLine("{0} != {1}", firstStudent, secondStudent);
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/System.Object-Overloading/Student.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | public class Student
5 | {
6 | public string Name { get; set; }
7 | public int Age { get; set; }
8 | public List Grades { get; set; }
9 |
10 | public Student()
11 | {
12 | this.Grades = new List();
13 | }
14 |
15 | public override bool Equals(object param)
16 | {
17 | // If the cast is invalid, the result will be null
18 | Student student = param as Student;
19 |
20 | // Check if we have valid not null Student object
21 | if (student == null)
22 | {
23 | return false;
24 | }
25 |
26 | // Compare the reference type member fields
27 | if (!Object.Equals(this.Name, student.Name))
28 | {
29 | return false;
30 | }
31 |
32 | // Compare the value type member fields
33 | if (this.Age != student.Age)
34 | {
35 | return false;
36 | }
37 |
38 | return true;
39 | }
40 |
41 | public static bool operator ==(Student student1, Student student2)
42 | {
43 | return Student.Equals(student1, student2);
44 | }
45 |
46 | public static bool operator !=(Student student1, Student student2)
47 | {
48 | return !(Student.Equals(student1, student2));
49 | }
50 |
51 | public override int GetHashCode()
52 | {
53 | return this.Name.GetHashCode() ^ this.Age.GetHashCode();
54 | }
55 |
56 | public override string ToString()
57 | {
58 | return String.Format(
59 | "Student(Name: {0}, Age: {1})",
60 | this.Name ?? "", this.Age);
61 | }
62 |
63 | public int CompareTo(Student other)
64 | {
65 | if (other == null)
66 | {
67 | throw new ArgumentException("Student is null");
68 | }
69 |
70 | int ageComparison = this.Age.CompareTo(other.Age);
71 | if (ageComparison == 0)
72 | {
73 | return this.Name.CompareTo(other.Name);
74 | }
75 |
76 | return ageComparison;
77 | }
78 |
79 | public static bool operator <(Student a, Student b)
80 | {
81 | return a.CompareTo(b) < 0;
82 | }
83 |
84 | public static bool operator >(Student a, Student b)
85 | {
86 | return a.CompareTo(b) > 0;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Value-and-Reference-Types/06. Value-and-Reference-Types.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | 8.0.50727
7 | 2.0
8 | {954EA6D3-295F-424C-97AF-EC6D5987DB2F}
9 | Exe
10 | Properties
11 | Value_And_Reference_Types
12 | Value-And-Reference-Types
13 | v2.0
14 |
15 |
16 |
17 |
18 | 2.0
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
56 |
--------------------------------------------------------------------------------
/10. OOP-Common-Type-System-Demos/Value-and-Reference-Types/ValueAndReferenceTypes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | // Reference type
4 | class RefClass
5 | {
6 | private int value;
7 |
8 | public int Value
9 | {
10 | get { return this.value; }
11 | set { this.value = value; }
12 | }
13 | }
14 |
15 | // Value type
16 | struct ValStruct
17 | {
18 | private int value;
19 |
20 | public int Value
21 | {
22 | get { return this.value; }
23 | set { this.value = value; }
24 | }
25 | }
26 |
27 | class ValueAndReferenceTypes
28 | {
29 | static void Main(string[] args)
30 | {
31 | RefClass refExample = new RefClass();
32 | refExample.Value = 100;
33 | RefClass refExample2 = refExample;
34 | refExample2.Value = 200;
35 | Console.WriteLine(refExample.Value); // Prints 200
36 |
37 | ValStruct valExample = new ValStruct();
38 | valExample.Value = 100;
39 | ValStruct valExample2 = valExample;
40 | valExample2.Value = 200;
41 | Console.WriteLine(valExample.Value); // Prints 100
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Object-Oriented-Programming
2 | Repository for the OOP course @ SoftUni https://softuni.bg/courses/oop/
3 |
--------------------------------------------------------------------------------