Great Scott! C# 7 is almost out!
Time to hop into the DeLorean with Doc Brown. If my calculations are correct, when this baby hits 88 miles per hour, we'll be traveling back to the future with C# 7. Switches? Where we're going, we don't need Switches we got Pattern Matching. Just hold on for one second. Let's get this straight, in the future Tuples will be usable! This will beg the questions. Where are we? When are we? We are in the future and we now have Local Functions and Records.
You'll walk away with a sense of where C# is going and how you can learn about its new features today by looking back to the future. This is heavy.
12. “[Sparta] they would never
reduce to slavery a city
which was itself an integral
portion of Hellas, and had
performed a great and
noble service to Hellas in
the most perilous of
emergencies.”
Xenophon, Hellenica
Book 2, 2.20
trans. H. G. Dakyns
15. “[Writing offers readers] the
appearance of wisdom, not true
wisdom, for they will read many
things without instruction and
will therefore seem to know
many things, when they are for
the most part ignorant and hard
to get along with, since they are
not wise, but only appear wise.”
Plato, Phaedrus
275a-b
trans. Harold N. Fowler
18. “World history is
the record of the
spirit's efforts to
attain knowledge of
what it is in itself.”
Hegel, Lectures on the
Philosophy of World History
Introduction
trans. Johannes Hoffmeister
26. Local Functions
{Algol 60}
begin
comment classic recursive procedure;
integer nn, nf;
integer procedure factorial(n); value n; integer n;
begin
if n <= 1 then factorial := 1
else factorial := n * factorial(n-1)
end;
nn := 5;
nf := factorial(nn);
outinteger ( 1 , nf)
end
taken from: http://algol60.org/lego/procedure11.a60Algol 60
27. Local Functions
{Algol 60}
integer procedure factorial(n); value n; integer n;
begin
if n <= 1 then factorial := 1
else factorial := n * factorial(n-1)
end;
nn := 5;
nf := factorial(nn);
taken from: http://algol60.org/lego/procedure11.a60Algol 60
40. Functions
{real world}
public int Hello(string name)
{
var time = DateTime.Now;
Console.Write($"Hello {name} it is now {time}");
return name.Length;
}
C# 6
45. Local Functions
{really “realistic”}
public IEnumerable<int> RealistCode(IEnumerable<int> values)
{
if (values == null)
throw new ArgumentException("values cannot be null");
if (!values.Any())
throw new ArgumentException(
"must have at least one element in values");
IEnumerable <int> Rules()
{
return values
.Where(x => x > 2)
.Where(x => x%2 == 1);
}
return Rules();
}
C# 7
46. Local Functions
{really “realistic”}
public IEnumerable<int> RealistCode(IEnumerable<int> values)
{
Precondition();
return values
.Where(x => x > 2)
.Where(x => x%2 == 1);
void Precondition()
{
if (values == null)
throw new ArgumentException("values cannot be null");
if (!values.Any())
throw new ArgumentException("must have at least one element in values");
}
}
C# 7
49. Local Functions
{design by contract}
note
description : "example of factorial"
class
APPLICATION
create
make
feature -- Initialization
make
local
n: NATURAL
do
n := 3
print ("%NFactorial of " + n.out + " = ")
print (recursive_factorial (n))
end
feature -- Access
recursive_factorial (n: NATURAL): NATURAL
require
n >= 0
do
if n = 0 then
Result := 1
else
Result := n * recursive_factorial (n - 1)
end
end
end
Eiffel
50. Local Functions
{design by contract}
feature -- Access
recursive_factorial (n: NATURAL): NATURAL
require
n >= 0
do
if n = 0 then
Result := 1
else
Result := n * recursive_factorial (n - 1)
end
end
end
Eiffel
79. Local Functions
{tail call optimization}
F#
module Factorial =
let factorial x =
let rec aux m x =
match x with
| 0 -> m
| _ -> aux (m*x) (x-1)
aux 1 x
95. Pattern Matching
{types}
static void Main(string[] args)
{
var values = new List<object>
{1, (short) 2,
(Int32) 3, null,
new {}, "no", 1.2d};
foreach (var value in values)
{
var x = value as int;
if (x != null)
Console.WriteLine("got “ + x);
}
}
C# 4.0
110. Pattern Matching
{types}
static void Main(string[] args)
{
var values = new List<object>
{1, (short) 2,
(Int32) 3, null,
new {}, "no", 1.2d};
foreach (var value in values)
{
t = match(x)
{
case int _: "int";
case short _: "short";
case object _: "object";
case string _: "string";
default: "something";
}
Console.WriteLine("got “ + t);
}
}
C# 8
111. Pattern Matching
{types}
foreach (var value in values)
{
t = match(x)
{
case int _: "int";
case short _: "short";
case object _: "object";
case string _: "string";
default: "something";
}
Console.WriteLine("got “ + t);
}
C# 8