From 0312c061b292985a5d849253480b0a8e5eafb332 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:35:59 +0200 Subject: C#: Added myself to contributors list --- csharp.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0cef8f05..b113b6d3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -3,6 +3,7 @@ language: c# contributors: - ["Irfan Charania", "https://github.com/irfancharania"] + - ["Max Yankov", "https://github.com/golergka"] filename: LearnCSharp.cs --- -- cgit v1.2.3 From b488745fc646b16df4ef87ddb746af596ea6588f Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:45:01 +0200 Subject: Static field --- csharp.html.markdown | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index b113b6d3..3de1eb66 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -403,7 +403,12 @@ namespace Learning private int _speed; // Private: Only accessible from within the class protected int gear; // Protected: Accessible from the class and subclasses internal int wheels; // Internal: Accessible from within the assembly - string name; // default: Only accessible from within this class + string name; // Everything is private by default: Only accessible from within this class + + // Static members belong to the type itself rather then specific object. + static public int bicyclesCreated = 0; + // You can access them without a reference to any object: + // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); // readonly values are set at run time // they can only be assigned upon declaration or in a constructor @@ -417,6 +422,7 @@ namespace Learning cadence = 50; _speed = 5; name = "Bontrager"; + bicyclesCreated++; } // This is a specified constructor (it contains arguments) -- cgit v1.2.3 From 964476a6e859959a3c1ae6a005ba82ba0d6b234e Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:45:58 +0200 Subject: C#: Lacking static members --- csharp.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 3de1eb66..584d40fd 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -535,6 +535,7 @@ namespace Learning * Enums, Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions + * Static properties, methods and classes * Exceptions, Interfaces, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3 From bb1f4eb93a70882f959acf6e849a9663ad74deca Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 22:49:36 +0200 Subject: C#: edited example code according to Microsoft's naming conventions --- csharp.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 584d40fd..0740c3ed 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -450,29 +450,29 @@ namespace Learning // Method declaration syntax: // () - public int getCadence() + public int GetCadence() { return cadence; } // void methods require no return statement - public void setCadence(int newValue) + public void SetCadence(int newValue) { cadence = newValue; } // virtual keyword indicates this method can be overridden - public virtual void setGear(int newValue) + public virtual void SetGear(int newValue) { gear = newValue; } - public void speedUp(int increment) + public void SpeedUp(int increment) { _speed += increment; } - public void slowDown(int decrement) + public void SlowDown(int decrement) { _speed -= decrement; } @@ -521,7 +521,7 @@ namespace Learning { } - public override void setGear(int gear) + public override void SetGear(int gear) { gear = 0; } -- cgit v1.2.3 From ccdc21900d090a88ca02e25007f4e3c197b8f50e Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:08:24 +0200 Subject: C#: enums --- csharp.html.markdown | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0740c3ed..fefcb0bb 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -405,6 +405,19 @@ namespace Learning internal int wheels; // Internal: Accessible from within the assembly string name; // Everything is private by default: Only accessible from within this class + // Enum is a value type that consists of a set of named constants + public enum Brand + { + AIST, + BMC, + Electra, + Gitane + } + // We defined this type inside a Bicycle class, so it is a nested type + // Code outside of this class should reference this type as Bicycle.Brand + + public Brand brand; // After declaing an enum type, we can declare the field of this type + // Static members belong to the type itself rather then specific object. static public int bicyclesCreated = 0; // You can access them without a reference to any object: @@ -416,28 +429,30 @@ namespace Learning // Constructors are a way of creating classes // This is a default constructor - public Bicycle() + private Bicycle() { gear = 1; cadence = 50; _speed = 5; name = "Bontrager"; + brand = Brand.AIST; bicyclesCreated++; } // This is a specified constructor (it contains arguments) public Bicycle(int startCadence, int startSpeed, int startGear, - string name, bool hasCardsInSpokes) + string name, bool hasCardsInSpokes, Brand brand) { this.gear = startGear; // "this" keyword denotes the current object this.cadence = startCadence; this._speed = startSpeed; this.name = name; // it can be useful when there's a name conflict this.hasCardsInSpokes = hasCardsInSpokes; + this.brand = brand; } // Constructors can be chained - public Bicycle(int startCadence, int startSpeed) : + public Bicycle(int startCadence, int startSpeed, Brand brand) : this(startCadence, startSpeed, 0, "big wheels", true) { } @@ -532,7 +547,7 @@ namespace Learning ## Topics Not Covered - * Enums, Flags + * Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions * Static properties, methods and classes -- cgit v1.2.3 From dd1a6193605909f3d6abf7795cd18261b27696b9 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:09:07 +0200 Subject: C#: default parameter values --- csharp.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index fefcb0bb..599aaca3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -482,12 +482,13 @@ namespace Learning gear = newValue; } - public void SpeedUp(int increment) + // Method parameters can have defaut values. In this case, methods can be called with these parameters omitted + public void SpeedUp(int increment = 1) { _speed += increment; } - public void SlowDown(int decrement) + public void SlowDown(int decrement = 1) { _speed -= decrement; } -- cgit v1.2.3 From 41ec7af8e5ac7e4a7ab8b8fc008466be98b0cdc4 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:09:21 +0200 Subject: Static methods --- csharp.html.markdown | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 599aaca3..22fb58e1 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -523,6 +523,14 @@ namespace Learning "\n------------------------------\n" ; } + + // Methods can also be static. It can be useful for helper methods + public static bool DidWeCreateEnoughBycles() + { + // Within a static method, we only can reference static class memebers + return bicyclesCreated > 9000; + } // If your class only needs static members, consider marking the class itself as static. + } // end class Bicycle // PennyFarthing is a subclass of Bicycle @@ -551,7 +559,7 @@ namespace Learning * Flags * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions - * Static properties, methods and classes + * Static properties * Exceptions, Interfaces, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3 From cae70d430e40478e2c916489e53ffcb24f8fe024 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:28:21 +0200 Subject: C#: calling base method --- csharp.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 22fb58e1..0012fcb3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -549,6 +549,13 @@ namespace Learning { gear = 0; } + + public override string ToString() + { + string result = "PennyFarthing bicycle "; + result += base.ToString(); // Calling the base version of the method + return reuslt; + } } } // End Namespace -- cgit v1.2.3 From a225349acb165084a9da95f2da631a7525c513d3 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sat, 17 Aug 2013 23:28:55 +0200 Subject: C#: interfaces --- csharp.html.markdown | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0012fcb3..55de415d 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -557,6 +557,36 @@ namespace Learning return reuslt; } } + + // Interfaces only contain signatures of the members, without the implementation. + interface IJumpable + { + void Jump(int meters); // all interface members are implicitly public + } + + interface IBreakable + { + bool Broken { get; } // interfaces can contain properties as well as methods, fields & events + } + + // Class can inherit only one other class, but can implement any amount of interfaces + class MountainBike : Bicycle, IJumpable, IBreakable + { + int damage = 0; + + public void Jump(int meters) + { + damage += meters; + } + + public void Broken + { + get + { + return damage > 100; + } + } + } } // End Namespace ``` @@ -567,7 +597,7 @@ namespace Learning * Attributes * Generics (T), Delegates, Func, Actions, lambda expressions * Static properties - * Exceptions, Interfaces, Abstraction + * Exceptions, Abstraction * LINQ * ASP.NET (Web Forms/MVC/WebMatrix) * Winforms -- cgit v1.2.3