From fceaa4a7cf0bcca54306ce6994f273f6b3710290 Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:05:14 -0400 Subject: added Decimal type example for C# --- csharp.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index d3adbd01..bb36d6ce 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -95,8 +95,12 @@ namespace Learning // Double - Double-precision 64-bit IEEE 754 Floating Point // Precision: 15-16 digits double fooDouble = 123.4; + + // Decimal - a 128-bits data type, with more precision than other floating-point types, + // suited for financial and monetary calculations + decimal fooDecimal = 150.3m; - // Bool - true & false + // Boolean - true & false bool fooBoolean = true; bool barBoolean = false; -- cgit v1.2.3 From 81c0480780a600ef27071285e002e8ea78dc727c Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:23:24 -0400 Subject: corrected errors about the nullable types for C# --- csharp.html.markdown | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index bb36d6ce..16e3749e 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -107,7 +107,8 @@ namespace Learning // Char - A single 16-bit Unicode character char fooChar = 'A'; - // Strings + // Strings -- unlike the previous base types which are all value types, + // a string is a reference type. That is, you can set it to null string fooString = "My string is here!"; Console.WriteLine(fooString); @@ -142,14 +143,21 @@ namespace Learning const int HOURS_I_WORK_PER_WEEK = 9001; // Nullable types - // any type can be made nullable by suffixing a ? + // any value type (i.e. not a class) can be made nullable by suffixing a ? // ? = int? nullable = null; Console.WriteLine("Nullable variable: " + nullable); - // In order to use nullable's value, you have to use Value property or to explicitly cast it - string? nullableString = "not null"; - Console.WriteLine("Nullable value is: " + nullableString.Value + " or: " + (string) nullableString ); + // In order to use nullable's value, you have to use Value property + // or to explicitly cast it + DateTime? nullableDate = null; + // The previous line would not have compiled without the '?' + // because DateTime is a value type + // ? is equivalent to writing Nullable + Nullable otherNullableDate = nullableDate; + + nullableDate = DateTime.Now; + Console.WriteLine("Nullable value is: " + nullableDate.Value + " or: " + (DateTime) nullableDate ); // ?? is syntactic sugar for specifying default value // in case variable is null -- cgit v1.2.3 From a2b14fcedfaa01d6dea7d7682b82fc5ddae9b84d Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:27:55 -0400 Subject: mentioned strings immutability and added details about var keyword (C#) --- csharp.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 16e3749e..c6394974 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -111,6 +111,9 @@ namespace Learning // a string is a reference type. That is, you can set it to null string fooString = "My string is here!"; Console.WriteLine(fooString); + // You can access each character of the string with an indexer: + char charFromString = fooString[1]; // 'y' + // Strings are immutable: you can't do fooString[1] = 'X'; // formatting string fooFs = string.Format("Check Check, {0} {1}, {0} {1:0.0}", 1, 2); @@ -165,6 +168,8 @@ namespace Learning Console.WriteLine("Not nullable variable: " + notNullable); // Var - compiler will choose the most appropriate type based on value + // Please not that this does not remove type safety. + // In this case, the type of fooImplicit is known to be a bool at compile time var fooImplicit = true; /////////////////////////////////////////////////// -- cgit v1.2.3 From dbfaa5ba52758cc826e710024f0be22404817e5c Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:29:03 -0400 Subject: typo (not instead of note) for C# doc --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index c6394974..2854a49b 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -168,7 +168,7 @@ namespace Learning Console.WriteLine("Not nullable variable: " + notNullable); // Var - compiler will choose the most appropriate type based on value - // Please not that this does not remove type safety. + // Please note that this does not remove type safety. // In this case, the type of fooImplicit is known to be a bool at compile time var fooImplicit = true; -- cgit v1.2.3 From 41cf74fa53b1d315e2909e1ea62f4874727b578a Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:33:11 -0400 Subject: corrections for C# doc --- csharp.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 2854a49b..7239ca52 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -218,7 +218,7 @@ namespace Learning // Others data structures to check out: // // Stack/Queue - // Dictionary + // Dictionary (an implementation of a hash map) // Read-only Collections // Tuple (.Net 4+) @@ -252,7 +252,6 @@ namespace Learning ~ Unary bitwise complement << Signed left shift >> Signed right shift - >>> Unsigned right shift & Bitwise AND ^ Bitwise exclusive OR | Bitwise inclusive OR -- cgit v1.2.3 From 67b9af44925a66ac0dc278050bcaaa80fd50f21c Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:39:59 -0400 Subject: added details for the switch statement for C# --- csharp.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 7239ca52..dcbb30d1 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -343,6 +343,14 @@ namespace Learning case 3: monthString = "March"; break; + // You can assign more than one case to an action + // But you can't add an action without a break before another case + // (if you want to do this, you would have to explicitly add a goto case x + case 6: + case 7: + case 8: + monthString = "Summer time!!"; + break; default: monthString = "Some other month"; break; -- cgit v1.2.3 From 3b5e4837bda104ca6e23b65327f819953d1c1b2e Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 15:49:20 -0400 Subject: more details for the enums (C#) --- csharp.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index dcbb30d1..76af24af 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -359,7 +359,7 @@ namespace Learning /////////////////////////////////////// - // Converting Data Types And Typcasting + // Converting Data Types And Typecasting /////////////////////////////////////// // Converting data @@ -413,7 +413,7 @@ namespace Learning // Class Declaration Syntax: - // class { + // class { // //data fields, constructors, functions all inside. // //functions are called as methods in Java. // } @@ -428,11 +428,14 @@ namespace Learning 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 + // It is really just mapping a name to a value (an int, unless specified otherwise). + // The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong. + // An enum can't contain the same value twice. public enum Brand { AIST, BMC, - Electra, + Electra=42, //you can explicitly set a value to a name Gitane } // We defined this type inside a Bicycle class, so it is a nested type -- cgit v1.2.3 From 10bb7dbce413be31916c02b340b15f1a4e4a6a23 Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 16:00:08 -0400 Subject: various typos + added an example to declare an auto property (C#) --- csharp.html.markdown | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index 76af24af..0c459bdb 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -441,7 +441,7 @@ namespace Learning // 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 + public Brand brand; // After declaring 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; @@ -486,7 +486,7 @@ namespace Learning // () // classes can implement getters and setters for their fields - // or they can implement properties + // or they can implement properties (this is the preferred way in C#) // Method declaration syntax: // () @@ -501,13 +501,13 @@ namespace Learning cadence = newValue; } - // virtual keyword indicates this method can be overridden + // virtual keyword indicates this method can be overridden in a derived class public virtual void SetGear(int newValue) { gear = newValue; } - // Method parameters can have defaut values. In this case, methods can be called with these parameters omitted + // Method parameters can have default values. In this case, methods can be called with these parameters omitted public void SpeedUp(int increment = 1) { _speed += increment; @@ -527,6 +527,11 @@ namespace Learning get { return _hasTassles; } set { _hasTassles = value; } } + + // You can also define an automatic property in one line + // this syntax will create a backing field automatically. + // You can set an access modifier on either the getter or the setter (or both) to restrict its access: + public bool IsBroken { get; private set; } // Properties can be auto-implemented public int FrameSize @@ -552,7 +557,7 @@ namespace Learning // 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 + // Within a static method, we only can reference static class members return bicyclesCreated > 9000; } // If your class only needs static members, consider marking the class itself as static. @@ -591,7 +596,7 @@ namespace Learning interface IBreakable { - bool Broken { get; } // interfaces can contain properties as well as methods, fields & events + bool Broken { get; } // interfaces can contain properties as well as methods & events } // Class can inherit only one other class, but can implement any amount of interfaces -- cgit v1.2.3 From 758cd94b33dcfce89ab308c67725513184fb3c4b Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 16:07:43 -0400 Subject: added an example for the foreach loop in C# --- csharp.html.markdown | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 0c459bdb..e6a174c3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -324,6 +324,17 @@ namespace Learning //Iterated 10 times, fooFor 0->9 } Console.WriteLine("fooFor Value: " + fooFor); + + // For Each Loop + // foreach loop structure => foreach( in ) + // The foreach loop loops over any object implementing IEnumerable or IEnumerable + // All the collection types (Array, List, Dictionary...) in the .Net framework implement one or both of these interfaces + // (The ToCharArray() could be removed, because a string also implements IEnumerable) + foreach (char character in "Hello World".ToCharArray()) + { + //Console.WriteLine(character); + //Iterated over all the characters in the string + } // Switch Case // A switch works with the byte, short, char, and int data types. -- cgit v1.2.3 From 71a62e642d2f31dcdc62aee9fd61ca6fa18d1c57 Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 16:11:45 -0400 Subject: breaking some lines (C#) --- csharp.html.markdown | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/csharp.html.markdown b/csharp.html.markdown index e6a174c3..8b293534 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -328,7 +328,8 @@ namespace Learning // For Each Loop // foreach loop structure => foreach( in ) // The foreach loop loops over any object implementing IEnumerable or IEnumerable - // All the collection types (Array, List, Dictionary...) in the .Net framework implement one or both of these interfaces + // All the collection types (Array, List, Dictionary...) in the .Net framework + // implement one or both of these interfaces. // (The ToCharArray() could be removed, because a string also implements IEnumerable) foreach (char character in "Hello World".ToCharArray()) { @@ -518,7 +519,8 @@ namespace Learning gear = newValue; } - // Method parameters can have default values. In this case, methods can be called with these parameters omitted + // Method parameters can have default values. + // In this case, methods can be called with these parameters omitted public void SpeedUp(int increment = 1) { _speed += increment; @@ -541,7 +543,8 @@ namespace Learning // You can also define an automatic property in one line // this syntax will create a backing field automatically. - // You can set an access modifier on either the getter or the setter (or both) to restrict its access: + // You can set an access modifier on either the getter or the setter (or both) + // to restrict its access: public bool IsBroken { get; private set; } // Properties can be auto-implemented -- cgit v1.2.3 From 6d5509e94b7e752e11285a86a2fe8f7f72e9f600 Mon Sep 17 00:00:00 2001 From: Melvyn Date: Sat, 21 Sep 2013 16:16:04 -0400 Subject: added myself to the contributors (C#) --- csharp.html.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/csharp.html.markdown b/csharp.html.markdown index 8b293534..1471b833 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"] + - ["Melvyn Laïly", "http://x2a.yt"] filename: LearnCSharp.cs --- -- cgit v1.2.3