From b01b53a46dbb4d2a35c44ad2caff521f442b2741 Mon Sep 17 00:00:00 2001 From: ksami Date: Mon, 23 Oct 2017 17:36:18 +0800 Subject: clarify use case for verbatim strings added example for using @ symbol with strings --- csharp.html.markdown | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 963f38f4..bb8bf6e5 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -132,6 +132,12 @@ namespace Learning.CSharp DateTime fooDate = DateTime.Now; Console.WriteLine(fooDate.ToString("hh:mm, dd MMM yyyy")); + // Verbatim String + // You can use the @ symbol before a string literal to escape all characters in the string + string path = "C:\\Users\\User\\Desktop"; + string verbatimPath = "C:\Users\User\Desktop"; + Console.WriteLine(path == verbatimPath); // => true + // You can split a string over two lines with the @ symbol. To escape " use "" string bazString = @"Here's some stuff on a new line! ""Wow!"", the masses cried"; -- cgit v1.2.3 From 3e072363d9b454973f116e39d4690ed73662f452 Mon Sep 17 00:00:00 2001 From: ksami Date: Mon, 23 Oct 2017 17:41:57 +0800 Subject: missed out the @ symbol --- 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 bb8bf6e5..77737182 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -135,7 +135,7 @@ namespace Learning.CSharp // Verbatim String // You can use the @ symbol before a string literal to escape all characters in the string string path = "C:\\Users\\User\\Desktop"; - string verbatimPath = "C:\Users\User\Desktop"; + string verbatimPath = @"C:\Users\User\Desktop"; Console.WriteLine(path == verbatimPath); // => true // You can split a string over two lines with the @ symbol. To escape " use "" -- cgit v1.2.3 From 467f143b9cc3c267d87b295e1ca84ef9308873a2 Mon Sep 17 00:00:00 2001 From: ksami Date: Mon, 23 Oct 2017 17:50:07 +0800 Subject: add example for verbatim interpolated string --- csharp.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 77737182..c98a7da9 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -955,6 +955,7 @@ on a new line! ""Wow!"", the masses cried"; // String interpolation by prefixing the string with $ // and wrapping the expression you want to interpolate with { braces } + // You can also combine both interpolated and verbatim strings with $@ public class Rectangle { public int Length { get; set; } @@ -967,6 +968,9 @@ on a new line! ""Wow!"", the masses cried"; { Rectangle rect = new Rectangle { Length = 5, Width = 3 }; Console.WriteLine($"The length is {rect.Length} and the width is {rect.Width}"); + + string username = "User"; + Console.WriteLine($@"C:\Users\{username}\Desktop"); } } -- cgit v1.2.3 From 95b5f9a4c4fa17b1d4854185a08e2a3434944de0 Mon Sep 17 00:00:00 2001 From: ksami Date: Tue, 24 Oct 2017 13:29:11 +0800 Subject: #2314 add delegates and events to csharp/en --- csharp.html.markdown | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index c98a7da9..24ce803e 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -640,6 +640,54 @@ on a new line! ""Wow!"", the masses cried"; } } + + // DELEGATES AND EVENTS + public class DelegateTest + { + public static int count = 0; + public static int Increment() + { + // increment count then return it + return ++count; + } + + // A delegate is a reference to a method + // To reference the Increment method, + // first declare a delegate with the same signature + // ie. takes no arguments and returns an int + public delegate int IncrementDelegate(); + + // An event can also be used to trigger delegates + // Create an event with the delegate type + public static event IncrementDelegate MyEvent; + + static void Main(string[] args) + { + // Refer to the Increment method by instantiating the delegate + // and passing the method itself in as an argument + IncrementDelegate inc = new IncrementDelegate(Increment); + Console.WriteLine(inc()); // => 1 + + // Delegates can be composed with the + operator + IncrementDelegate composedInc = inc; + composedInc += inc; + composedInc += inc; + + // composedInc will run Increment 3 times + Console.WriteLine(composedInc()); // => 4 + + + // Subscribe to the event with the delegate + MyEvent += new IncrementDelegate(Increment); + MyEvent += new IncrementDelegate(Increment); + + // Trigger the event + // ie. run all delegates subscribed to this event + Console.WriteLine(MyEvent()); // => 6 + } + } + + // Class Declaration Syntax: // class { // //data fields, constructors, functions all inside. -- cgit v1.2.3 From 4eb33f98b47dfbf6ae0c7c2148faadab310f53c5 Mon Sep 17 00:00:00 2001 From: ksami Date: Tue, 24 Oct 2017 15:20:20 +0800 Subject: add topics for tuples to csharp/en Added explanation and examples for tuples, deconstruction of tuples and other objects, and discarding unwanted fields during deconstruction --- csharp.html.markdown | 72 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 16 deletions(-) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index c98a7da9..54d5ea09 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -1106,25 +1106,65 @@ namespace Learning.More.CSharp } } +//New C# 7 Feature +//Install Microsoft.Net.Compilers Latest from Nuget +//Install System.ValueTuple Latest from Nuget using System; namespace Csharp7 { - //New C# 7 Feature - //Install Microsoft.Net.Compilers Latest from Nuget - //Install System.ValueTuple Latest from Nuget - class Program - { - static void Main(string[] args) - { - //Type 1 Declaration - (string FirstName, string LastName) names1 = ("Peter", "Parker"); - Console.WriteLine(names1.FirstName); - - //Type 2 Declaration - var names2 = (First:"Peter", Last:"Parker"); - Console.WriteLine(names2.Last); - } - } + // TUPLES, DECONSTRUCTION AND DISCARDS + class TuplesTest + { + public (string, string) GetName() + { + // Fields in tuples are by default named Item1, Item2... + var names1 = ("Peter", "Parker"); + Console.WriteLine(names1.Item2); // => Parker + + // Fields can instead be explicitly named + // Type 1 Declaration + (string FirstName, string LastName) names2 = ("Peter", "Parker"); + + // Type 2 Declaration + var names3 = (First:"Peter", Last:"Parker"); + + Console.WriteLine(names2.FirstName); // => Peter + Console.WriteLine(names3.Last); // => Parker + + return names3; + } + + public string GetLastName() { + var fullName = GetName(); + + // Tuples can be deconstructed + (string firstName, string lastName) = fullName; + + // Fields in a deconstructed tuple can be discarded by using _ + var (_, last) = fullName; + return last; + } + + // Any type can be deconstructed in the same way by + // specifying a Deconstruct method + public int randomNumber = 4; + public int anotherRandomNumber = 10; + + public void Deconstruct(out int randomNumber, out int anotherRandomNumber) + { + randomNumber = this.randomNumber; + anotherRandomNumber = this.anotherRandomNumber; + } + + static void Main(string[] args) + { + var tt = new TuplesTest(); + (int num1, int num2) = tt; + Console.WriteLine($"num1: {num1}, num2: {num2}"); // => num1: 4, num2: 10 + + Console.WriteLine(tt.GetLastName()); + } + } } ``` -- cgit v1.2.3 From 6d836a9fda6ea007d3393799c83f3f12df91affb Mon Sep 17 00:00:00 2001 From: ksami Date: Fri, 27 Oct 2017 14:27:14 +0800 Subject: Add more c#7 features Added pattern matching, reference locals and local functions under c#7 features --- csharp.html.markdown | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'csharp.html.markdown') diff --git a/csharp.html.markdown b/csharp.html.markdown index 3790747c..f27adf18 100644 --- a/csharp.html.markdown +++ b/csharp.html.markdown @@ -1213,6 +1213,85 @@ namespace Csharp7 Console.WriteLine(tt.GetLastName()); } } + + // PATTERN MATCHING + class PatternMatchingTest + { + public static (string, int)? CreateLogMessage(object data) + { + switch(data) + { + // Additional filtering using when + case System.Net.Http.HttpRequestException h when h.Message.Contains("404"): + return (h.Message, 404); + case System.Net.Http.HttpRequestException h when h.Message.Contains("400"): + return (h.Message, 400); + case Exception e: + return (e.Message, 500); + case string s: + return (s, s.Contains("Error") ? 500 : 200); + case null: + return null; + default: + return (data.ToString(), 500); + } + } + } + + // REFERENCE LOCALS + // Allow you to return a reference to an object instead of just its value + class RefLocalsTest + { + // note ref in return + public static ref string FindItem(string[] arr, string el) + { + for(int i=0; i apple + } + } + + // LOCAL FUNCTIONS + class LocalFunctionTest + { + private static int _id = 0; + public int id; + public LocalFunctionTest() + { + id = generateId(); + + // This local function can only be accessed in this scope + int generateId() + { + return _id++; + } + } + + public static void AnotherMethod() + { + var lf1 = new LocalFunctionTest(); + var lf2 = new LocalFunctionTest(); + Console.WriteLine($"{lf1.id}, {lf2.id}"); // => 0, 1 + + int id = generateId(); + // error CS0103: The name 'generateId' does not exist in the current context + } + } } ``` -- cgit v1.2.3