6 prism_languages: [csharp]
8 A quick overview of C# 7
14 public void PrintCoordinates(Point p)
16 p.GetCoordinates(out int x, out int y);
17 WriteLine($"({x}, {y})");
21 `out` is used to declare a variable at the point where it is passed as an argument.
25 #### Is-expressions with patterns
28 public void PrintStars(object o)
30 if (o is null) return; // constant pattern "null"
31 if (!(o is int i)) return; // type pattern "int i"
32 WriteLine(new string('*', i));
36 #### Switch statements with patterns
42 WriteLine($"circle with radius {c.Radius}");
44 case Rectangle s when (s.Length == s.Height):
45 WriteLine($"{s.Length} x {s.Height} square");
48 WriteLine($"{r.Length} x {r.Height} rectangle");
51 WriteLine("<unknown shape>");
54 throw new ArgumentNullException(nameof(shape));
63 (string, string, string) LookupName(long id) // tuple return type
65 ... // retrieve first, middle and last from data storage
66 return (first, middle, last); // tuple literal
71 var names = LookupName(id);
72 WriteLine($"found {names.Item1} {names.Item3}.");
75 #### Tuple elements with name
78 (string first, string middle, string last) LookupName(long id) // tuple elements have names
82 var names = LookupName(id);
83 WriteLine($"found {names.first} {names.last}.");
89 return (first: first, middle: middle, last: last); // named tuple elements in a literal
92 #### Tuple Deconstruction
95 (var first, var middle, var last) = LookupName(id1);
96 WriteLine($"found {first} {last}.");
100 var (first, middle, last) = LookupName(id1); // var outside
104 (first, middle, last) = LookupName(id2); // assign onto existing variables
111 public int Fibonacci(int x)
113 if (x < 0) throw new ArgumentException("Less negativity please!", nameof(x));
114 return Fib(x).current;
116 (int current, int previous) Fib(int i)
118 if (i == 0) return (1, 0);
119 var (p, pp) = Fib(i - 1);
125 ### Literal Improvements
127 #### Digit Separator inside numbers literals
137 var b = 0b1010_1011_1100_1101_1110_1111;
140 ### Ref Returns and Locals
143 public ref int Find(int number, int[] numbers)
145 for (int i = 0; i < numbers.Length; i++)
147 if (numbers[i] == number)
149 return ref numbers[i]; // return the storage location, not the value
152 throw new IndexOutOfRangeException($"{nameof(number)} not found");
155 int[] array = { 1, 15, -39, 0, 7, 14, -12 };
156 ref int place = ref Find(7, array); // aliases 7's place in the array
157 place = 9; // replaces 7 with 9 in the array
158 WriteLine(array[4]); // prints 9
161 ### More Expression Bodied Members
163 C# 7.0 adds accessors, constructors and finalizers to the list of things that can have expression bodies:
168 private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>();
169 private int id = GetId();
171 public Person(string name) => names.TryAdd(id, name); // constructors
172 ~Person() => names.TryRemove(id, out *); // destructors
175 get => names[id]; // getters
176 set => names[id] = value; // setters
181 ### Throw Expressions
186 public string Name { get; }
187 public Person(string name) => Name = name ?? throw new ArgumentNullException(name);
188 public string GetFirstName()
190 var parts = Name.Split(" ");
191 return (parts.Length > 0) ? parts[0] : throw new InvalidOperationException("No name!");
193 public string GetLastName() => throw new NotImplementedException();