The document discusses various programming language concepts and how they are implemented in C# compared to other languages like C++. It provides code examples of concepts like namespaces, operator overloading, optional arguments, structs, properties, and delegates in C# and compares them to their implementation in C++ or other languages. It emphasizes finding the similarities between languages but also pointing out differences or potential "false friends" where the implementation or semantics have drifted from the original.
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
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. @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. @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. @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
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. @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. @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
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
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
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. @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!