O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Trying to learn C# (NDC Oslo 2019)

181 visualizações

Publicada em

Learning a new language is often colored by the language you come from.

As a programmer coming from C++ and Java, with some functional programming background, how did I navigate trying to get a grasp of C#? Should be fun for C# developers, but also educational: How do we teach a new language to folks that already know how to program?

Publicada em: Software
  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Trying to learn C# (NDC Oslo 2019)

  1. 1. @pati_gallardo TurtleSec @pati_gallardo Turtle Sec
  2. 2. @pati_gallardo TurtleSec 2 How do you learn a new programming language? Submit a talk on learning it and get it accepted?
  3. 3. @pati_gallardo TurtleSec 3 Where to start? Write some code Get environmentTake notes Videos / BooksFreak out Procrastinate Ask on TwitterMake flow diagram
  4. 4. @pati_gallardo Turtle Sec Trying to learn C# Patricia Aas NDC Oslo 2019
  5. 5. @pati_gallardo Turtle Sec Patricia Aas - Trainer & Consultant C++ Programmer, Application Security Currently : TurtleSec Previously : Vivaldi, Cisco Systems, Knowit, Opera Software Master in Computer Science - main language Java Pronouns: she/her
  6. 6. @pati_gallardo TurtleSec @pati_gallardo The Genealogy of programming languages
  7. 7. @pati_gallardo TurtleSec 7@pati_gallardo Cross Pollination Good? Bad? Both?
  8. 8. @pati_gallardo TurtleSec 1. #define DEBUG 1 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. }; 17. } @pati_gallardo 8 1. #define DEBUG 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. } 17. } Which Programming Language is this? What about this?
  9. 9. @pati_gallardo TurtleSec 1. #define DEBUG 1 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. }; 17. } @pati_gallardo 9 1. #define DEBUG 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. } 17. } telephone.cpp Telephone.cs C++ C#
  10. 10. @pati_gallardo TurtleSec 10@pati_gallardo Evil Twin
  11. 11. @pati_gallardo TurtleSec @pati_gallardo How do you learn a language?
  12. 12. @pati_gallardo TurtleSec 12 Telephone Teléfono? English Spanish Cognates Same Origin, Form and Meaning
  13. 13. @pati_gallardo TurtleSec 13 Much Mucho? English SpanishSame Form and Meaning False Cognates Not Same Origin!
  14. 14. @pati_gallardo TurtleSec 14@pati_gallardo Cognates or False Cognates? Who cares? Your mental shortcuts are working!
  15. 15. @pati_gallardo TurtleSec C# : An Imperative C-like language? 1. var list = new List<int>(){ 40, 5, 9, 39, 19 }; 2. 3. for (int i = 0; i < list.Count; i++) { 4. Console.WriteLine(list[i]); 5. } 6. 7. foreach (var item in list) { 8. Console.WriteLine(item); 9. } @pati_gallardo 15 No Surprises
  16. 16. @pati_gallardo TurtleSec C# : A Functional Lisp-like language? 1. var list = new List<int>(){ 40, 5, 9, 39, 19 }; 2. 3. list 4. .Where(s => s < 10) 5. .ToList() 6. .ForEach(s => Console.WriteLine(s)); @pati_gallardo 16 No Surprises?
  17. 17. @pati_gallardo TurtleSec 17 Pigg Pig? Norwegian English False Friends “I had bad pigs in my tires” Same Form Stud / Nail Not Same Meaning!
  18. 18. @pati_gallardo TurtleSec 18@pati_gallardo False Friends Bad Assumptions Your mental shortcuts are hurting you Drift in Meaning Special case of False Friends
  19. 19. @pati_gallardo TurtleSec 19 Frokost Frokost? Norwegian Danish LunchDrift in Meaning Same Origin and Form Breakfast Not Same Meaning!
  20. 20. @pati_gallardo TurtleSec 20 List List? Java C# ArrayList/ Vector Not Same Meaning! Same Origin and Form Linked List Drift in Meaning
  21. 21. @pati_gallardo TurtleSec 21 FormMeaning Origin Cognates False Friends False Cognates Drift in MeaningDrift in Form
  22. 22. @pati_gallardo TurtleSec @pati_gallardo How you learn depends on what you know?
  23. 23. @pati_gallardo TurtleSec 23@pati_gallardo First language Scheme (Lisp) I know Java - university I know C++ and C I have coded in probably 10-15 other languages. Examples: Perl, Python, JavaScript Programming for almost 20 years So what do I know?
  24. 24. @pati_gallardo TurtleSec @pati_gallardo You can’t start from scratch
  25. 25. @pati_gallardo TurtleSec 25 What to skip? Languages inherit from each other What is normal? What is different? Danger: False Friends Danger: Unknown Unknowns
  26. 26. @pati_gallardo TurtleSec 26 Plan Skip over Cognates For loop, break, continue, if, else… If it’s “normal”, skip it
  27. 27. @pati_gallardo TurtleSec @pati_gallardo How do you know that you’re not missing things? Skim?
  28. 28. @pati_gallardo TurtleSec 28 Learn the basics Find surprises Too much repetition Cognates
  29. 29. @pati_gallardo TurtleSec 29@pati_gallardo Namespaces Defines Drift in Meaning
  30. 30. @pati_gallardo TurtleSec 30 1. namespace PackageName { 2. public class ClassName { 3. // Class definition 4. } 5. } [C++] Namespaces Recognition Language C#More like C++ modules really? Drift in Meaning
  31. 31. @pati_gallardo TurtleSec [C & C++] C# has defines! 1. #define HERE_I_AM 2. using NUnit.Framework; 3. 4. public class Preprocessor { 5. [Test] 6. public void TestPreprocessor() { 7. #if HERE_I_AM 8. Assert.Pass(); 9. #else 10. Assert.Fail(); 11. #endif 12. } 13. } @pati_gallardo 31 N really though. No preprocessor Drift in Meaning
  32. 32. @pati_gallardo TurtleSec 32@pati_gallardo Operator Overloading Optional Arguments Named Parameters Struct Value Type Cognates
  33. 33. @pati_gallardo TurtleSec [C++] Operator Overloading Use 1. Box first = new Box(new Point(1, 1), 3, 2); 2. Box second = new Box(new Point(3, 4), 3, 3); 3. Box boundingBox = first + second; @pati_gallardo 33 Some constructs are quite elegant with operator overloading
  34. 34. @pati_gallardo TurtleSec 34 Bounding box x = 1 y = 1 width = 5 height = 6 (1,1) (3,4) Box boundingBox = first + second; first second
  35. 35. @pati_gallardo TurtleSec [C++] Operator Overloading Definition 1. public static Box operator +(Box left, Box right) { 2. Point top = 3. new Point(Math.Min(left.top().x, right.top().x), 4. Math.Min(left.top().y, right.top().y)); 5. Point bottom = 6. new Point(Math.Max(left.bottom().x, right.bottom().x), 7. Math.Max(left.bottom().y, right.bottom().y)); 8. int width = bottom.x - top.x; 9. int height = bottom.y - top.y; 10. return new Box(top, width, height); 11. } @pati_gallardo 35
  36. 36. @pati_gallardo TurtleSec [C++] Optional Arguments 1. public class OptionalArguments { 2. private static int increase(int x, int y = 2) { 3. return x + y; 4. } 5. 6. [Test] 7. public void TestNamedParameters() { 8. Assert.AreEqual(5, increase(3)); 9. } 10. } @pati_gallardo 36
  37. 37. @pati_gallardo TurtleSec [Objective-C] Named Parameters 1. public class NamedParameters { 2. private static int named(int x, int y) { 3. return x + y; 4. } 5. 6. [Test] 7. public void TestNamedParameters() { 8. Assert.AreEqual(5, named(y: 3, x: 2)); 9. } 10. } @pati_gallardo 37
  38. 38. @pati_gallardo TurtleSec [C++] Struct - value type 1. struct Point { 2. public int x; 3. public int y; 4. } @pati_gallardo 38 Similar in meaning to C++Drift in Form In C++ structs are classes with default public access - in C# structs are classes that don’t allow inheritance In C++ all types are value types Cognates
  39. 39. @pati_gallardo TurtleSec 39@pati_gallardo Properties Out Parameters Drift in Form
  40. 40. @pati_gallardo TurtleSec [Qt?] Properties 1. public class Properties { 2. public string ReadWriteProperty { get; set; } = "Hello"; 3. public string ReadOnlyProperty { get; } 4. // Auto-implemented properties must have get accessors. 5. // public string WriteOnlyProperty { set; } 6. public string PrivateReadWriteProperty { private get; set; } 7. public string ReadPrivateWriteProperty { get; private set; } 8. 9. private string PrivateReadWriteField; 10. public string WrapField { 11. set => PrivateReadWriteField = value; 12. get => PrivateReadWriteField; 13. } 14. } @pati_gallardo 40 Magic “value”
  41. 41. @pati_gallardo TurtleSec [C & C++] (Inline) Out Parameters 1. private static int outParam(int x, int y, out int mod) { 2. mod = x % y; 3. return x / y; 4. } 5. 6. [Test] 7. public void TestOutParameters() { 8. int quotient = outParam(25, 5, out var remainder); 9. Assert.AreEqual(0, remainder); 10. Assert.AreEqual(5, quotient); 11. } @pati_gallardo 41 Passing int by ref
  42. 42. @pati_gallardo TurtleSec [] Null-conditional operators ?. and ?[] 1. int GetCoffee(List<Shop> coffeeShops, int index) { 2. // If there are any coffeeShops and 3. // if there is a shop at that index, 4. // please get me that coffee, 5. // if not, just get me 0 6. return coffeeShops?[index]?.Coffee ?? 0; 7. } @pati_gallardo 42
  43. 43. @pati_gallardo TurtleSec 43@pati_gallardo Destructor / Finalize False Friends
  44. 44. @pati_gallardo TurtleSec 44 Point out false friends C# finalizers (also called destructors!) look like C++ destructors but act like Java’s finalize()
  45. 45. @pati_gallardo TurtleSec [C++ & Java] Destructor / Finalize 1. public class DestroyMe { 2. ~DestroyMe() { 3. // Might be called by the 4. // Garbage Collector 5. } 6. } @pati_gallardo 45 More like Java finalize Drift in Meaning
  46. 46. @pati_gallardo TurtleSec @pati_gallardo What are the semantics?
  47. 47. @pati_gallardo TurtleSec 47 Learn the middle What’s the middle? Beyond basic syntax?
  48. 48. @pati_gallardo TurtleSec [Java & C++] IDisposable 1. public class DisposeMe : IDisposable { 2. private FileStream fileStream; 3. 4. public void Dispose() { 5. fileStream?.Dispose(); 6. } 7. } @pati_gallardo 48 Similar to Javas Closeable Drift in Form Same semantics Different syntax
  49. 49. @pati_gallardo TurtleSec [Python] yield return (Generators) 1. public IEnumerable<int> YieldReturn() { 2. yield return 3; 3. yield return 4; 4. yield return 5; 5. } 6. [Test] 7. public void TestingYieldReturn() { 8. using var it = YieldReturn().GetEnumerator(); 9. it.MoveNext(); 10. Assert.AreEqual(3, it.Current); 11. it.MoveNext(); 12. Assert.AreEqual(4, it.Current); 13. it.MoveNext(); 14. Assert.AreEqual(5, it.Current); 15. } @pati_gallardo 49
  50. 50. @pati_gallardo TurtleSec [Python, Java, C++] IEnumerable (Lazy Iterators) 1. // C# In Depth, Jon Skeet 2. // Lazy Evaluation 3. static IEnumerable<int> Fibonacci() { 4. int current = 0; 5. int next = 1; 6. while (true) { 7. yield return current; 8. int oldCurrent = current; 9. current = next; 10. next = next + oldCurrent; 11. } 12. } @pati_gallardo 50
  51. 51. @pati_gallardo TurtleSec [?] Extension Methods (Extend 3-party Classes) 1. public class Target {} 2. public static class Extension { 3. public static void PrintHello(this Target t) { 4. Console.WriteLine("Hello World"); 5. } 6. } 7. public class Test { 8. [Test] 9. public void TestExtension() { 10. Target target = new Target(); 11. target.PrintHello(); 12. } 13. } @pati_gallardo 51
  52. 52. @pati_gallardo TurtleSec @pati_gallardo How do we teach?
  53. 53. @pati_gallardo TurtleSec 53 Use Terms People Recognize Explain how this is different But gives folks a frame of reference Delegate -> Fancy Function Pointer
  54. 54. @pati_gallardo TurtleSec [C & C++] Delegates (Fancy Function Pointers) 1. public class DelegateTest { 2. public delegate void printMe(); 3. [Test] 4. public void TestExtension() { 5. printMe printHello = 6. delegate { 7. Console.WriteLine("Hello World!"); 8. }; 9. printHello(); 10. } 11. } @pati_gallardo 54 Function Pointer Type Function DefinitionUsing Function Pointer
  55. 55. @pati_gallardo TurtleSec [C & C++] Delegates (Fancy Function Pointers) 1. printMe first = delegate { Console.WriteLine("Hello"); }; 2. printMe second = delegate { Console.WriteLine("World"); }; 3. 4. var helloWorld = first + second; 5. helloWorld(); 55 Composable Function Pointers! Operator + defined on “function pointers” @pati_gallardo
  56. 56. @pati_gallardo TurtleSec [C++ Qt] Events (Event Handlers) 1. EventHandler handler = 2. delegate { 3. Console.WriteLine("Hello Event!"); 4. }; 5. handler?.Invoke(this, EventArgs.Empty); @pati_gallardo 56
  57. 57. @pati_gallardo TurtleSec 57@pati_gallardo Decimal Non Zero Based Array Nullable Types What about surprises? 128 bit floating point type with 28-29 significant digit precision What else have I missed?
  58. 58. @pati_gallardo TurtleSec Non Zero Based Array 1. Array array = Array.CreateInstance(typeof(int), 2. new[] {2}, 3. new[] {42}); 4. array.SetValue(5, 42); 5. array.SetValue(6, 43); 6. Assert.AreEqual(2, array.GetLength(0)); 7. Assert.AreEqual(42, array.GetLowerBound(0)); 8. Assert.AreEqual(43, array.GetUpperBound(0)); 9. Assert.AreEqual(5, array.GetValue(42)); 10. Assert.AreEqual(6, array.GetValue(43)); @pati_gallardo 58 I mean…. Wat?
  59. 59. @pati_gallardo TurtleSec Nullable Types 1. bool? maybe = null; 2. 3. if (maybe ?? true) 4. Console.WriteLine("Print Me!"); 5. 6. maybe = false; 7. 8. if (!maybe ?? false) 9. Console.WriteLine("Print me too!"); @pati_gallardo 59 Maybe??
  60. 60. @pati_gallardo TurtleSec 60@pati_gallardo Anonymous Types ExpandoObject JavaScript Love
  61. 61. @pati_gallardo TurtleSec [JavaScript] Anonymous Types 1. var gilmoreGirls = 2. new[] { 3. new { Name = "Loralai", Age = "36" }, 4. new { Name = "Rory", Age = "20" } 5. }; @pati_gallardo 61 If you squint it looks a bit like JS objects
  62. 62. @pati_gallardo TurtleSec [JavaScript] ExpandoObject 1. public delegate void printMe(); 2. 3. [Test] 4. public void Test() { 5. dynamic expando = new ExpandoObject(); 6. // Add a field 7. expando.GilmoreGirl = "Rory"; 8. Assert.AreEqual("Rory", expando.GilmoreGirl); 9. // Add a function 10. printMe order = delegate { Console.WriteLine("Coffee!"); }; 11. expando.getCoffee = order; 12. expando.getCoffee(); 13. } 14. @pati_gallardo 62 Full on JS!
  63. 63. @pati_gallardo TurtleSec 63 Killer features Why C#? N just Nice To Have
  64. 64. @pati_gallardo TurtleSec 64 Why would I choose C#? Platform Integration LINQ
  65. 65. @pati_gallardo TurtleSec @pati_gallardo How do we learn?
  66. 66. @pati_gallardo TurtleSec 66 Rewrite an application you have written already Focus on the language and n the application What are the Unknown Unknowns? Hard to get confident
  67. 67. @pati_gallardo TurtleSec 67 FormMeaning Origin Cognates False Friends False Cognates Drift in MeaningDrift in Form Unknown Unknowns! How do you find out what you don’t know?
  68. 68. @pati_gallardo TurtleSec 68 The best way to learn a programming language is to make something interesting next to someone who’s knowledgeable and kind
  69. 69. @pati_gallardo TurtleSec 1. #define DEBUG 1 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. }; 17. } @pati_gallardo 69 1. #define DEBUG 2. 3. namespace Communication { 4. class Telephone { 5. bool log = false; 6. 7. Telephone() { 8. #if DEBUG 9. log = true; 10. #endif 11. } 12. 13. ~Telephone() { 14. log = false; 15. } 16. } 17. } telephone.cpp Telephone.cs C++ C# You can have an accent in code! Or “not idiomatic”
  70. 70. @pati_gallardo TurtleSec 70 Where to start? Procrastinate Anything that gets you out of this is good Maybe even submitting a talk to a conference?
  71. 71. @pati_gallardo TurtleSec @pati_gallardo
  72. 72. @pati_gallardo Turtle Sec Questions? Photos from pixabay.com Patricia Aas, TurtleSec
  73. 73. @pati_gallardo TurtleSec @pati_gallardo Turtle Sec

×