From 928b8404aa7d47dbc50351f48fdca7334bb32647 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 20:10:40 +0100 Subject: [CSharp/en]XML Comments can be used for external doc generation and for help within the IDE --- csharp.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 47dd9683..ad43e607 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -18,8 +18,10 @@ C# is an elegant and type-safe object-oriented language that enables developers Multi-line comments look like this */ /// -/// This is an XML documentation comment +/// This is an XML documentation comment which can be used to generate external +/// documentation or provide context help within an IDE /// +//public void MethodOrClassOrOtherWithParsableHelp() {} // Specify namespaces application will be using using System; -- cgit v1.2.3 From 937e6dad51446cf47c85156e4af0696efdcac43b Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 20:21:52 +0100 Subject: [CSharp/en]namespaces can have multiple levels with dots --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index ad43e607..4a238f25 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -35,7 +35,7 @@ using System.Threading.Tasks; using System.IO; // defines scope to organize code into "packages" -namespace Learning +namespace LearningXInYMinutes.CSharp { // Each .cs file should at least contain a class with the same name as the file // you're allowed to do otherwise, but shouldn't for sanity. -- cgit v1.2.3 From 012a4a8e901eda4fa89e151f398c67500af8acb9 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 20:42:27 +0100 Subject: [CSharp/en]Fixed compilation and ef errors --- csharp.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 4a238f25..d3738b39 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -26,7 +26,7 @@ Multi-line comments look like this // Specify namespaces application will be using using System; using System.Collections.Generic; -using System.Data.Entity; +using System.Data.Entity; // Add dll reference with NuGet: Install-Package EntityFramework using System.Dynamic; using System.Linq; using System.Linq.Expressions; @@ -684,7 +684,7 @@ on a new line! ""Wow!"", the masses cried"; // All though this is not entirely useful in this example, you // could do bicycle[0] which yields "chris" to get the first passenger or // bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle) - private string[] passengers = { "chris", "phil", "darren", "regina" } + private string[] passengers = { "chris", "phil", "darren", "regina" }; public string this[int i] { @@ -786,7 +786,7 @@ on a new line! ""Wow!"", the masses cried"; /// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional) /// http://msdn.microsoft.com/en-us/data/jj193542.aspx /// - public class BikeRepository : DbSet + public class BikeRepository : DbContext { public BikeRepository() : base() -- cgit v1.2.3 From 1d4bb253fd8b6053e5a38d206ffc15360d3bd6fd Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 20:56:56 +0100 Subject: [CSharp/en]Constants are TitleCase, not SCREAMING_CAPS in C# (as done everywhere in the .NET framework, encouraged by the MS .NET framework guidelines and StackOverflow: http://stackoverflow.com/a/242549/540352) --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index d3738b39..88c2db35 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -127,7 +127,7 @@ on a new line! ""Wow!"", the masses cried"; // Use const or read-only to make a variable immutable // const values are calculated at compile time - const int HOURS_I_WORK_PER_WEEK = 9001; + const int HoursWorkPerWeek = 9001; /////////////////////////////////////////////////// // Data Structures -- cgit v1.2.3 From 495e38612160ac373325543de3c5b88e0ef6ed35 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 21:40:21 +0100 Subject: [CSharp/en]break and continue in loops --- csharp.html.markdown | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 88c2db35..9f66b8ff 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -244,8 +244,15 @@ on a new line! ""Wow!"", the masses cried"; int fooDoWhile = 0; do { - //Iterated 100 times, fooDoWhile 0->99 + // Start iteration 100 times, fooDoWhile 0->99 + if (false) + continue; // skip the current iteration + fooDoWhile++; + + if (fooDoWhile == 50) + break; // breaks from the loop completely + } while (fooDoWhile < 100); //for loop structure => for(; ; ) @@ -303,7 +310,7 @@ on a new line! ""Wow!"", the masses cried"; // Converting data // Convert String To Integer - // this will throw an Exception on failure + // this will throw a Exception on failure int.Parse("123");//returns an integer version of "123" // try parse will default to type default on failure -- cgit v1.2.3 From 69e38974aec4d149944e551867c1412dfcf2bed7 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 21:41:46 +0100 Subject: [CSharp/en]added casting to converting --- csharp.html.markdown | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 9f66b8ff..fceda4ff 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -310,7 +310,7 @@ on a new line! ""Wow!"", the masses cried"; // Converting data // Convert String To Integer - // this will throw a Exception on failure + // this will throw a FormatException on failure int.Parse("123");//returns an integer version of "123" // try parse will default to type default on failure @@ -324,6 +324,11 @@ on a new line! ""Wow!"", the masses cried"; Convert.ToString(123); // or tryInt.ToString(); + + // Casting + // Cast decimal 15 to a int + // and then implicitly cast to long + long x = (int) 15M; } /////////////////////////////////////// -- cgit v1.2.3 From 04c4433433f313884fe38f126b24ff52b59f3af1 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 21:45:13 +0100 Subject: [CSharp/en]Added ref and out parameters --- csharp.html.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index fceda4ff..8eda5356 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -380,6 +380,15 @@ on a new line! ""Wow!"", the masses cried"; return -1; } + // Methods can have the same name, as long as the signature is unique + // A method that differs only in return type is not unique + public static void MethodSignatures( + ref int maxCount, // Pass by reference + out int count) + { + count = 15; // out param must be assigned before control leaves the method + } + // Methods can have the same name, as long as the signature is unique public static void MethodSignatures(string maxCount) { @@ -414,6 +423,10 @@ on a new line! ""Wow!"", the masses cried"; MethodSignatures(3, 1, 3, "Some", "Extra", "Strings"); MethodSignatures(3, another: 3); // explicity set a parameter, skipping optional ones + // BY REF AND OUT PARAMETERS + int maxCount = 0, count; // ref params must have value + MethodSignatures(ref maxCount, out count); + // EXTENSION METHODS int i = 3; i.Print(); // Defined below -- cgit v1.2.3 From 4f8b16f81745c4082945b49e631aadd204564626 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 22:05:11 +0100 Subject: [CSharp/en]Added static property --- csharp.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 8eda5356..61db6a8a 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -625,7 +625,7 @@ on a new line! ""Wow!"", the masses cried"; // Static members belong to the type itself rather then specific object. // You can access them without a reference to any object: // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); - static public int BicyclesCreated = 0; + public static int BicyclesCreated { get; set; } // readonly values are set at run time // they can only be assigned upon declaration or in a constructor @@ -827,7 +827,6 @@ on a new line! ""Wow!"", the masses cried"; * Flags * Attributes - * Static properties * Exceptions, Abstraction * ASP.NET (Web Forms/MVC/WebMatrix) * Winforms -- cgit v1.2.3 From dc34af2b5de78fb7e7f04e982e145d0bd67a3728 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 22:34:42 +0100 Subject: [CSharp/en]Added [Flags] --- csharp.html.markdown | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 61db6a8a..4258a0c6 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -622,6 +622,22 @@ on a new line! ""Wow!"", the masses cried"; public BikeBrand Brand; // After declaring an enum type, we can declare the field of this type + // Decorate an enum with the FlagsAttribute to indicate that multiple values can be switched on + [Flags] // Any class derived from Attribute can be used to decorate types, methods, parameters etc + public enum BikeAccessories + { + None = 0, + Bell = 1, + MudGuards = 2, // need to set the values manually! + Racks = 4, + Lights = 8, + Full = Bell | MudGuards | Racks | Lights + } + + // Usage: aBike.Accessories.HasFlag(Bicycle.BikeAccessories.Bell) + // Before .NET 4: (aBike.Accessories & Bicycle.BikeAccessories.Bell) == Bicycle.BikeAccessories.Bell + public BikeAccessories Accessories { get; set; } + // Static members belong to the type itself rather then specific object. // You can access them without a reference to any object: // Console.WriteLine("Bicycles created: " + Bicycle.bicyclesCreated); @@ -825,7 +841,6 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered - * Flags * Attributes * Exceptions, Abstraction * ASP.NET (Web Forms/MVC/WebMatrix) -- cgit v1.2.3 From 710f9fa03b5cea3d60906edb977133e37a19ebf6 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 23:14:58 +0100 Subject: [CSharp/en]Added exception handling --- csharp.html.markdown | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 4258a0c6..df265d8c 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -462,6 +462,31 @@ on a new line! ""Wow!"", the masses cried"; Func square = (x) => x * x; // Last T item is the return value Console.WriteLine(square(3)); // 9 + // ERROR HANDLING - coping with an uncertain world + try + { + var funBike = PennyFarthing.CreateWithGears(6); + + // will no longer execute because CreateWithGears throws an exception + string some = ""; + if (true) some = null; + some.ToLower(); // throws a NullReferenceException + } + catch (NotSupportedException) + { + Console.WriteLine("Not so much fun now!"); + } + catch (Exception ex) // catch all other exceptions + { + throw new ApplicationException("It hit the fan", ex); + // throw; // A rethrow that preserves the callstack + } + // catch { } // catch-all without capturing the Exception + finally + { + // executes after try or catch + } + // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily. // Most of objects that access unmanaged resources (file handle, device contexts, etc.) // implement the IDisposable interface. The using statement takes care of @@ -780,10 +805,17 @@ on a new line! ""Wow!"", the masses cried"; } set { - throw new ArgumentException("You can't change gears on a PennyFarthing"); + throw new InvalidOperationException("You can't change gears on a PennyFarthing"); } } + public static PennyFarthing CreateWithGears(int gears) + { + var penny = new PennyFarthing(1, 1); + penny.Gear = gears; // Oops, can't do this! + return penny; + } + public override string Info() { string result = "PennyFarthing bicycle "; @@ -842,7 +874,7 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered * Attributes - * Exceptions, Abstraction + * Abstraction * ASP.NET (Web Forms/MVC/WebMatrix) * Winforms * Windows Presentation Foundation (WPF) -- cgit v1.2.3 From 90e8dac0b8f5ad4cb0e9218cc05457da48dd4e98 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sat, 31 Jan 2015 23:39:41 +0100 Subject: [CSharp/en]updated topics not covered + added myself :) --- csharp.html.markdown | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index df265d8c..fc7588f8 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -5,6 +5,7 @@ contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Melvyn Laïly", "http://x2a.yt"] - ["Shaun McCarthy", "http://www.shaunmccarthy.com"] + - ["Wouter Van Schandevijl", "http://github.com/laoujin"] filename: LearnCSharp.cs --- @@ -874,10 +875,14 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered * Attributes - * Abstraction - * ASP.NET (Web Forms/MVC/WebMatrix) - * Winforms - * Windows Presentation Foundation (WPF) + * async/await, yield, pragma directives + * Web Development + * ASP.NET MVC & WebApi (new) + * ASP.NET Web Forms (old) + * WebMatrix (tool) + * Desktop Development + * Windows Presentation Foundation (WPF) (new) + * Winforms (old) ## Further Reading @@ -890,7 +895,4 @@ on a new line! ""Wow!"", the masses cried"; * [ASP.NET Web Matrix Tutorials](http://www.asp.net/web-pages/tutorials) * [ASP.NET Web Forms Tutorials](http://www.asp.net/web-forms/tutorials) * [Windows Forms Programming in C#](http://www.amazon.com/Windows-Forms-Programming-Chris-Sells/dp/0321116208) - - - -[C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx) + * [C# Coding Conventions](http://msdn.microsoft.com/en-us/library/vstudio/ff926074.aspx) -- cgit v1.2.3 From f5d0208178d9903911249dad482cec37f5523627 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sun, 1 Feb 2015 00:04:10 +0100 Subject: [CSharp/en]Fixes for my own changes --- csharp.html.markdown | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index fc7588f8..58d5fa11 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -326,7 +326,7 @@ on a new line! ""Wow!"", the masses cried"; // or tryInt.ToString(); - // Casting + // Casting // Cast decimal 15 to a int // and then implicitly cast to long long x = (int) 15M; @@ -390,11 +390,6 @@ on a new line! ""Wow!"", the masses cried"; count = 15; // out param must be assigned before control leaves the method } - // Methods can have the same name, as long as the signature is unique - public static void MethodSignatures(string maxCount) - { - } - // GENERICS // The classes for TKey and TValue is specified by the user calling this function. // This method emulates the SetDefault of Python @@ -657,7 +652,7 @@ on a new line! ""Wow!"", the masses cried"; MudGuards = 2, // need to set the values manually! Racks = 4, Lights = 8, - Full = Bell | MudGuards | Racks | Lights + FullPackage = Bell | MudGuards | Racks | Lights } // Usage: aBike.Accessories.HasFlag(Bicycle.BikeAccessories.Bell) -- cgit v1.2.3 From 8911f368de43ab33d0d5ac251551de1ee765727c Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sun, 1 Feb 2015 06:01:01 +0100 Subject: [CSharp/en]extra details for using EntityFramework and namespaces --- csharp.html.markdown | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 58d5fa11..87a70200 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -24,19 +24,24 @@ Multi-line comments look like this /// //public void MethodOrClassOrOtherWithParsableHelp() {} -// Specify namespaces application will be using +// Specify the namespaces this source code will be using +// The namespaces below are all part of the standard .NET Framework Class Libary using System; using System.Collections.Generic; -using System.Data.Entity; // Add dll reference with NuGet: Install-Package EntityFramework using System.Dynamic; using System.Linq; -using System.Linq.Expressions; using System.Net; using System.Threading.Tasks; using System.IO; -// defines scope to organize code into "packages" -namespace LearningXInYMinutes.CSharp +// But this is one is not: +using System.Data.Entity; +// In order to be able to use it, you need to add a dll reference +// This can be done with the NuGet package manager: `Install-Package EntityFramework` + +// Namespaces define scope to organize code into "packages" or "modules" +// Using this code from another source file: using Learning.CSharp; +namespace Learning.CSharp { // Each .cs file should at least contain a class with the same name as the file // you're allowed to do otherwise, but shouldn't for sanity. -- cgit v1.2.3 From 031637455c6fef9ea51dbe1073129c4871ad10d9 Mon Sep 17 00:00:00 2001 From: Laoujin Date: Sun, 1 Feb 2015 19:01:47 +0100 Subject: [CSharp/en]Fixed typo --- csharp.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 87a70200..479b7f01 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -34,7 +34,7 @@ using System.Net; using System.Threading.Tasks; using System.IO; -// But this is one is not: +// But this one is not: using System.Data.Entity; // In order to be able to use it, you need to add a dll reference // This can be done with the NuGet package manager: `Install-Package EntityFramework` -- cgit v1.2.3 From f1a90a4e636616ce91a271504eee3f374bfd7094 Mon Sep 17 00:00:00 2001 From: Guntbert Reiter Date: Sun, 4 Oct 2015 16:44:24 +0200 Subject: Put demonstrative condition into ternary expression It should be made clear that the part before the ternary operator is indeed a condition, most often created as some comparison expression. --- csharp.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 479b7f01..222ba0d2 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -236,7 +236,8 @@ on a new line! ""Wow!"", the masses cried"; // Ternary operators // A simple if/else can be written as follows // ? : - string isTrue = (true) ? "True" : "False"; + int toCompare = 17; + string isTrue = toCompare == 17 ? "True" : "False"; // While loop int fooWhile = 0; -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- csharp.html.markdown | 56 ++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 222ba0d2..02650038 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -25,7 +25,7 @@ Multi-line comments look like this //public void MethodOrClassOrOtherWithParsableHelp() {} // Specify the namespaces this source code will be using -// The namespaces below are all part of the standard .NET Framework Class Libary +// The namespaces below are all part of the standard .NET Framework Class Libary using System; using System.Collections.Generic; using System.Dynamic; @@ -48,7 +48,7 @@ namespace Learning.CSharp public class LearnCSharp { // BASIC SYNTAX - skip to INTERESTING FEATURES if you have used Java or C++ before - public static void Syntax() + public static void Syntax() { // Use Console.WriteLine to print lines Console.WriteLine("Hello World"); @@ -371,11 +371,11 @@ on a new line! ""Wow!"", the masses cried"; // // INTERESTING FEATURES // - + // DEFAULT METHOD SIGNATURES public // Visibility - static // Allows for direct call on class without object + static // Allows for direct call on class without object int // Return Type, MethodSignatures( int maxCount, // First variable, expects an int @@ -383,7 +383,7 @@ on a new line! ""Wow!"", the masses cried"; int another = 3, params string[] otherParams // captures all other parameters passed to method ) - { + { return -1; } @@ -400,8 +400,8 @@ on a new line! ""Wow!"", the masses cried"; // The classes for TKey and TValue is specified by the user calling this function. // This method emulates the SetDefault of Python public static TValue SetDefault( - IDictionary dictionary, - TKey key, + IDictionary dictionary, + TKey key, TValue defaultItem) { TValue result; @@ -410,7 +410,7 @@ on a new line! ""Wow!"", the masses cried"; return result; } - // You can narrow down the objects that are passed in + // You can narrow down the objects that are passed in public static void IterateAndPrint(T toPrint) where T: IEnumerable { // We can iterate, since T is a IEnumerable @@ -450,13 +450,13 @@ on a new line! ""Wow!"", the masses cried"; // GENERICS // - var phonebook = new Dictionary() { + var phonebook = new Dictionary() { {"Sarah", "212 555 5555"} // Add some entries to the phone book }; // Calling SETDEFAULT defined as a generic above Console.WriteLine(SetDefault(phonebook, "Shaun", "No Phone")); // No Phone - // nb, you don't need to specify the TKey and TValue since they can be + // nb, you don't need to specify the TKey and TValue since they can be // derived implicitly Console.WriteLine(SetDefault(phonebook, "Sarah", "No Phone")); // 212 555 5555 @@ -491,26 +491,26 @@ on a new line! ""Wow!"", the masses cried"; // DISPOSABLE RESOURCES MANAGEMENT - let you handle unmanaged resources easily. // Most of objects that access unmanaged resources (file handle, device contexts, etc.) - // implement the IDisposable interface. The using statement takes care of + // implement the IDisposable interface. The using statement takes care of // cleaning those IDisposable objects for you. using (StreamWriter writer = new StreamWriter("log.txt")) { writer.WriteLine("Nothing suspicious here"); // At the end of scope, resources will be released. // Even if an exception is thrown. - } + } // PARALLEL FRAMEWORK // http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx - var websites = new string[] { - "http://www.google.com", "http://www.reddit.com", + var websites = new string[] { + "http://www.google.com", "http://www.reddit.com", "http://www.shaunmccarthy.com" }; var responses = new Dictionary(); - + // Will spin up separate threads for each request, and join on them // before going to the next step! - Parallel.ForEach(websites, + Parallel.ForEach(websites, new ParallelOptions() {MaxDegreeOfParallelism = 3}, // max of 3 threads website => { @@ -534,7 +534,7 @@ on a new line! ""Wow!"", the masses cried"; (introduceTo) => string.Format("Hey {0}, this is {1}", student.FirstName, introduceTo)); Console.WriteLine(student.Introduce("Beth")); - // IQUERYABLE - almost all collections implement this, which gives you a lot of + // IQUERYABLE - almost all collections implement this, which gives you a lot of // very useful Map / Filter / Reduce style methods var bikes = new List(); bikes.Sort(); // Sorts the array @@ -556,8 +556,8 @@ on a new line! ""Wow!"", the masses cried"; // ASPARALLEL // And this is where things get wicked - combines linq and parallel operations var threeWheelers = bikes.AsParallel().Where(b => b.Wheels == 3).Select(b => b.Name); - // this will happen in parallel! Threads will automagically be spun up and the - // results divvied amongst them! Amazing for large datasets when you have lots of + // this will happen in parallel! Threads will automagically be spun up and the + // results divvied amongst them! Amazing for large datasets when you have lots of // cores // LINQ - maps a store to IQueryable objects, with delayed execution @@ -575,9 +575,9 @@ on a new line! ""Wow!"", the masses cried"; .Select(b => b.Name); // still no query run // Now the query runs, but opens a reader, so only populates are you iterate through - foreach (string bike in query) + foreach (string bike in query) Console.WriteLine(result); - + } @@ -629,7 +629,7 @@ on a new line! ""Wow!"", the masses cried"; private set; // You can set modifiers on the get/set methods } - int _speed; // Everything is private by default: Only accessible from within this class. + int _speed; // Everything is private by default: Only accessible from within this class. // can also use keyword private public string Name { get; set; } @@ -676,7 +676,7 @@ on a new line! ""Wow!"", the masses cried"; // Constructors are a way of creating classes // This is a default constructor - public Bicycle() + public Bicycle() { this.Gear = 1; // you can access members of the object with the keyword this Cadence = 50; // but you don't always need it @@ -688,13 +688,13 @@ on a new line! ""Wow!"", the masses cried"; // This is a specified constructor (it contains arguments) public Bicycle(int startCadence, int startSpeed, int startGear, - string name, bool hasCardsInSpokes, BikeBrand brand) + string name, bool hasCardsInSpokes, BikeBrand brand) : base() // calls base first { - Gear = startGear; + Gear = startGear; Cadence = startCadence; _speed = startSpeed; - Name = name; + Name = name; _hasCardsInSpokes = hasCardsInSpokes; Brand = brand; } @@ -857,7 +857,7 @@ on a new line! ""Wow!"", the masses cried"; } /// - /// Used to connect to DB for LinqToSql example. + /// Used to connect to DB for LinqToSql example. /// EntityFramework Code First is awesome (similar to Ruby's ActiveRecord, but bidirectional) /// http://msdn.microsoft.com/en-us/data/jj193542.aspx /// @@ -882,7 +882,7 @@ on a new line! ""Wow!"", the masses cried"; * ASP.NET Web Forms (old) * WebMatrix (tool) * Desktop Development - * Windows Presentation Foundation (WPF) (new) + * Windows Presentation Foundation (WPF) (new) * Winforms (old) ## Further Reading -- cgit v1.2.3 From d97eaa51d279f24dddbc820aab10ec3cb2e90e69 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 10 Oct 2015 20:52:59 -0700 Subject: Added detail on C# single inheritance Clarified that the base class name must be the first thing listed --- csharp.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 02650038..a4dff948 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -837,7 +837,8 @@ on a new line! ""Wow!"", the masses cried"; 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 + // Class can inherit only one other class, but can implement any amount of interfaces, however + // the base class name must be the first in the list and all interfaces follow class MountainBike : Bicycle, IJumpable, IBreakable { int damage = 0; -- cgit v1.2.3 From 5bbd8419b232bff0baae40506351f733e0e30462 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 10 Oct 2015 22:25:39 -0700 Subject: Added info C# null-propagation Another tool I found useful when learning C# --- csharp.html.markdown | 3 +++ 1 file changed, 3 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 02650038..14ffe4e0 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -443,6 +443,9 @@ on a new line! ""Wow!"", the masses cried"; // ?? is syntactic sugar for specifying default value (coalesce) // in case variable is null int notNullable = nullable ?? 0; // 0 + + // ?. is an operator for null-propogation - a shorthand way of checking for null + nullable?.Print(); // Use the Print() extension method if nullable isn't null // IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is: var magic = "magic is a string, at compile time, so you still get type safety"; -- cgit v1.2.3 From 51f7c4f6a015666794dc46ec080c80813d4ac57e Mon Sep 17 00:00:00 2001 From: Jo Pearce Date: Mon, 12 Oct 2015 14:07:56 +0100 Subject: Fixed the custom indexer example (setter return type is void) --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 479b7f01..72ee3731 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -749,7 +749,7 @@ on a new line! ""Wow!"", the masses cried"; // It's also possible to define custom Indexers on objects. // All though this is not entirely useful in this example, you - // could do bicycle[0] which yields "chris" to get the first passenger or + // could do bicycle[0] which returns "chris" to get the first passenger or // bicycle[1] = "lisa" to set the passenger. (of this apparent quattrocycle) private string[] passengers = { "chris", "phil", "darren", "regina" }; @@ -760,7 +760,7 @@ on a new line! ""Wow!"", the masses cried"; } set { - return passengers[i] = value; + passengers[i] = value; } } -- cgit v1.2.3 From 0cea1d25d5d103eb9774cf3f3e1362a11b2e684d Mon Sep 17 00:00:00 2001 From: Jo Pearce Date: Mon, 12 Oct 2015 15:15:06 +0100 Subject: Added some simple yield examples --- csharp.html.markdown | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 72ee3731..8babb90a 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Melvyn Laïly", "http://x2a.yt"] - ["Shaun McCarthy", "http://www.shaunmccarthy.com"] - ["Wouter Van Schandevijl", "http://github.com/laoujin"] + - ["Jo Pearce", "http://github.com/jdpearce"] filename: LearnCSharp.cs --- @@ -417,6 +418,42 @@ on a new line! ""Wow!"", the masses cried"; // Item is an int Console.WriteLine(item.ToString()); } + + // YIELD + // Usage of the "yield" keyword indicates that the method it appears in is an Iterator + // (this means you can use it in a foreach loop) + public static IEnumerable YieldCounter(int limit = 10) + { + for (var i = 0; i < limit; i++) + yield return i; + } + + // which you would call like this : + public static void PrintYieldCounterToConsole() + { + foreach (var counter in YieldCounter()) + Console.WriteLine(counter); + } + + // you can use more than one "yield return" in a method + public static IEnumerable ManyYieldCounter() + { + yield return 0; + yield return 1; + yield return 2; + yield return 3; + } + + // you can also use "yield break" to stop the Iterator + // this method would only return half of the values from 0 to limit. + public static IEnumerable YieldCounterWithBreak(int limit = 10) + { + for (var i = 0; i < limit; i++) + { + if (i > limit/2) yield break; + yield return i; + } + } public static void OtherInterestingFeatures() { @@ -875,7 +912,7 @@ on a new line! ""Wow!"", the masses cried"; ## Topics Not Covered * Attributes - * async/await, yield, pragma directives + * async/await, pragma directives * Web Development * ASP.NET MVC & WebApi (new) * ASP.NET Web Forms (old) -- cgit v1.2.3 From ad5b0a615b6dd6857de6d2055c1a2743bd340d83 Mon Sep 17 00:00:00 2001 From: Cameron Wood Date: Tue, 13 Oct 2015 17:03:26 -0400 Subject: [csharp/en] Fix for a few spelling errors Fixed a few typos and spelling errors in the C# documentation --- csharp.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 28da9fe5..7aca2c6f 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -160,7 +160,7 @@ on a new line! ""Wow!"", the masses cried"; // List = new List(); List intList = new List(); List stringList = new List(); - List z = new List { 9000, 1000, 1337 }; // intialize + List z = new List { 9000, 1000, 1337 }; // initialize // The <> are for generics - Check out the cool stuff section // Lists don't default to a value; @@ -460,7 +460,7 @@ on a new line! ""Wow!"", the masses cried"; { // OPTIONAL PARAMETERS MethodSignatures(3, 1, 3, "Some", "Extra", "Strings"); - MethodSignatures(3, another: 3); // explicity set a parameter, skipping optional ones + MethodSignatures(3, another: 3); // explicitly set a parameter, skipping optional ones // BY REF AND OUT PARAMETERS int maxCount = 0, count; // ref params must have value @@ -481,7 +481,7 @@ on a new line! ""Wow!"", the masses cried"; // in case variable is null int notNullable = nullable ?? 0; // 0 - // ?. is an operator for null-propogation - a shorthand way of checking for null + // ?. is an operator for null-propagation - a shorthand way of checking for null nullable?.Print(); // Use the Print() extension method if nullable isn't null // IMPLICITLY TYPED VARIABLES - you can let the compiler work out what the type is: @@ -650,7 +650,7 @@ on a new line! ""Wow!"", the masses cried"; { return _cadence; } - set // set - define a method to set a proprety + set // set - define a method to set a property { _cadence = value; // Value is the value passed in to the setter } -- cgit v1.2.3 From 4e0db77a0c3cc4483b3b5f4f81d306869eef656a Mon Sep 17 00:00:00 2001 From: "chris@chriszimmerman.net" Date: Thu, 15 Oct 2015 20:37:29 -0400 Subject: Added information about out parameters in C#. --- csharp.html.markdown | 2 ++ 1 file changed, 2 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 7aca2c6f..c32fb9f3 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Shaun McCarthy", "http://www.shaunmccarthy.com"] - ["Wouter Van Schandevijl", "http://github.com/laoujin"] - ["Jo Pearce", "http://github.com/jdpearce"] + - ["Chris Zimmerman", "https://github.com/chriszimmerman"] filename: LearnCSharp.cs --- @@ -394,6 +395,7 @@ on a new line! ""Wow!"", the masses cried"; ref int maxCount, // Pass by reference out int count) { + //the argument passed in as 'count' will hold the value of 15 outside of this function count = 15; // out param must be assigned before control leaves the method } -- cgit v1.2.3 From 6a173cb28d6b2565e63fd3e0c05e6627043ba48b Mon Sep 17 00:00:00 2001 From: "chris@chriszimmerman.net" Date: Thu, 15 Oct 2015 20:39:37 -0400 Subject: Fixed spacing. --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index c32fb9f3..26d4dffc 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -395,8 +395,8 @@ on a new line! ""Wow!"", the masses cried"; ref int maxCount, // Pass by reference out int count) { - //the argument passed in as 'count' will hold the value of 15 outside of this function - count = 15; // out param must be assigned before control leaves the method + //the argument passed in as 'count' will hold the value of 15 outside of this function + count = 15; // out param must be assigned before control leaves the method } // GENERICS -- cgit v1.2.3 From 0671147741c186d27ae055fdceea71b06ec94f20 Mon Sep 17 00:00:00 2001 From: "chris@chriszimmerman.net" Date: Thu, 15 Oct 2015 20:41:22 -0400 Subject: Fixed more spacing issues. --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 26d4dffc..59f3e42b 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -395,8 +395,8 @@ on a new line! ""Wow!"", the masses cried"; ref int maxCount, // Pass by reference out int count) { - //the argument passed in as 'count' will hold the value of 15 outside of this function - count = 15; // out param must be assigned before control leaves the method + //the argument passed in as 'count' will hold the value of 15 outside of this function + count = 15; // out param must be assigned before control leaves the method } // GENERICS -- cgit v1.2.3 From d65e738fc339c7b1481dc88cb1c39d1ec8777b6d Mon Sep 17 00:00:00 2001 From: Andrew Backes Date: Thu, 15 Oct 2015 23:26:57 -0500 Subject: [csharp/eng] Partial Classes example --- csharp.html.markdown | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 7aca2c6f..d62dadec 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -911,6 +911,35 @@ on a new line! ""Wow!"", the masses cried"; public DbSet Bikes { get; set; } } + + // Classes can be split across multiple .cs files + // A1.cs + public partial class A + { + public static void A1() + { + Console.WriteLine("Method A1 in class A"); + } + } + + // A2.cs + public partial class A + { + public static void A2() + { + Console.WriteLine("Method A2 in class A"); + } + } + + // Program using the partial class "A" + public class Program + { + static void Main() + { + A.A1(); + A.A2(); + } + } } // End Namespace ``` -- cgit v1.2.3 From 9f510f3044138429ce616c390e42d8e0b6ceb2df Mon Sep 17 00:00:00 2001 From: "chris@chriszimmerman.net" Date: Sat, 17 Oct 2015 19:50:09 -0400 Subject: Fixed indentation in csharp file. --- csharp.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 59f3e42b..31c0417e 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -395,8 +395,8 @@ on a new line! ""Wow!"", the masses cried"; ref int maxCount, // Pass by reference out int count) { - //the argument passed in as 'count' will hold the value of 15 outside of this function - count = 15; // out param must be assigned before control leaves the method + //the argument passed in as 'count' will hold the value of 15 outside of this function + count = 15; // out param must be assigned before control leaves the method } // GENERICS -- cgit v1.2.3