all right thank you everybody for coming
uh welcome to what's new in C sharp 11.
my name's Dave I work on the Roslin and
Razer team
and I like C sharp and I like talking
about it so I'm gonna
um quick show of hands we'll see if we
can do this very quickly who is on C
sharp six
or above actually let's do it this way
six or above hopefully everybody thank
you right put your hands down if it's
only six so keep hands up of seven or
above okay keep your hands up there's
eight or above
nine
ten
okay so most of you seem to be on nine
maybe ten that's cool perfect
um
I
who's heard of parameter null checking
um there was a bit of controversy about
this feature and so I had to include it
mainly because so I did this talk in
June in NDC Melbourne which is where I'm
from and
I submitted the talk when all of the
drama was happening and I thought this
is great I'm going to get some good
mileage out of this and then they can
the future and um well the Talk's boring
now but I wanted to mention it because
just a reminder in case you didn't know
C sharp is open source
uh you can follow along on
github.com.net C sharp Lang if you
believe that any one particular feature
is the beginning of the apocalypse and
the downfall of humanity as we know it
and the end of the C sharp language
um I don't think it is
I quite like the parameter null chain
feature too but that's a whole other
thing
um but the fact that the team took the
feedback on board and removed the
feature Etc I think that's a good sign
so
if anything I show you today makes you
go uh
maybe you know joining the conversation
so what I'm going to do is I'm just
going to go through every single feature
of C sharp 11. this is actually the
easiest talk I've ever done because it's
the first time I'm talking about
something after it's been released and
so I know what's in C sharp 11 in June I
had to guess
um and so if you have any questions
yell out at the time or at the end
whatever that's fine hopefully I'll
remember to repeat the question for the
recording I'm sure someone down the
front will yell at me if I don't but
let's go
rostering literals these are in no
particular order however this happens to
be my favorite
so in C sharp ten and Below there are
two ways to Define string literals the
one at the top is a normal string
literal I guess it's just called a
string literal in the middle we have a
Verbatim string
a Verbatim string says you don't have to
escape
new lines tabs
and
because you escape new lines with Slash
n and because you don't have to escape
new lines therefore you don't have to
escape slashes
I'm sure you all know this you do have
to escape quotes though because
otherwise how does the compiler know
when the string is finished
and that's okay but you know it could be
better so raw string literals the the
promise of the feature is you never have
to escape anything
what you see is exactly what's in the
string in this raw form and so as you
can see there
in the first one we have to escape new
lines and the second one we only have to
escape quotes and then the third one
nothing is escaped
let's jump across to visual studio
and write a string so firstly with raw
string literals
they can be on one line
um that's just yeah they can just in so
you know or they can be on multiple
lines there's a few little quirks to
them though so if they're on multiple
lines there has to be nothing after the
first three quotes
and the last three quotes have to be on
a line by themselves as well so the
delimiters have to be essentially alone
your content is between the delimiters
not like non-inclusive and then three
quotes is how we how we delimit them
and that's kind of weird and some people
think it's a bit ugly and I'm kind of
surprised that people complained about
two exclamation marks but they don't
complain about three quotes but again
I'm not getting into that
so you don't have to escape anything
right I can type in quotes this is fine
I can put in tabs new lines slash
characters no worries
um
but of course that's just a spelling
mistake don't worry
um
of course
how can I promise that I don't have to
escape anything right what if I want to
put in two talking marks well that's
fine
the obvious question is what if I want
to put in three talking box
now we've broken things
so how does this work
the answer is that roster and literals
the Syntax for them is at least three
talking marks
so if you want to include three talking
marks in your string and if we ignore
the editor trying to help me
oh God okay he tries to help and anyway
so if I want to include three talking
Marks in My string then I use four as my
delimiter right you can include n
talking marks by delimiting them with n
plus one
now the reason I'm surprised people
don't complain about this is because it
means this is perfectly valency Sharp
and you can keep going like this and see
this is visual shooter is now helping me
um
I dare you to go and just change every
string in your code base so that just
please we can do this obviously
hopefully we don't but we can so they're
a little bit weird but they're really
really good
the reason I love them is so working on
the Ross and Razer team a lot of what I
do I write a lot of tests because I'm a
good boy but my tests often involve
here's a chunk of code
go do something you know a code fix or
an analyzer or whatever and then here's
what I want my code to look like and so
I can express my test data in its raw
form that's why they're called raw
stream literals it's really good for
that particularly with C sharp it's got
braces in it it's got new lines it's got
all these complex characters that
usually cause problems other things this
is good for is if you're testing uh web
apis or Json serialization if you've got
Blobs of Json the fact that you can grab
the contents of a string and assuming it
was Json this would be a better example
um well let's type some Json quickly so
I can I can do this I can copy that I
can paste it into Postman press send and
see what happens or I can get some
result out of Postman copy it and paste
it into my expected output I don't have
to fight with it I don't have to fight
with the compiler so they're really good
now of course we can also do so there's
nothing there's no such thing as a
Verbatim raw string because that doesn't
make sense but there is interpolated raw
strings
so interpolated strings with the dollar
sign same thing dollar sign turbulated
raw strings of course we now have a
compile error because we have braces in
our raw string so in a normal
interpolated string we escape the braces
by putting in two braces
but we don't have to escape anything in
raw strength so how do we do it well
if you want to include up to one brace
in your raw string
I'm glad you're all with it this is good
it's lovely to see so this is perfectly
validation up and now if I want to make
this a proper interpolated string I use
two braces whoops to indicate
that this is an interpolation hole and
obviously I haven't defined name so
ignore the error
so again it's like this can look a bit
weird and yes this means that this can
be perfectly valid C sharp as well
anyway hopefully again hopefully no one
will do this they are really cool
so that's
that's the fun part let me make this
sensible
a fight with the editor again
and one two three okay
there is one more trick to Raw string
literals oh actually there's kind of two
it's kind of one anyway I'm just going
to make another I'm going to make a
Verbatim string here and I'm going to do
the same thing
in fact why don't I copy and paste it'll
be easier oops
okay
so these two strings are nominally the
same right
but they're actually not so the the one
of the tricks that Royal stringless
tools does by requiring the delimiters
to be on the line by themselves they
also ignore the first new line so this
uh verbatim string y actually starts
with a new line character and depending
on what you're doing this can throw you
off right if if you've got a really
fussy Json serializer maybe that's an
error and so you end up doing that to
get rid of that new line but now the
code looks really ugly so the so the
other trick they do is By ignoring that
new line it allows you to format your
string data really nicely
and this becomes even more prevalent if
I do
this and add some indentation to the
world
so this verbatim string now looks really
ugly but this is what it has to look
like to get well-formatted string data
into my program
with my raw string I can actually do
this
and so a raw string literal will ignore
any white space that comes before
whatever column the end delimiter is at
now that sounds a bit weird and
confusing and there's probably a better
way to explain it but if you look that
vertical line that Visual Studio is
drawing there that is telling you where
that string where each line of that
string starts so you can not only get
the string that you want like the data
that you want in your program with no
escaping you also get to make it look
nice which again is really handy
um
it's this is the actual reason I really
love this feature is because as I'm
scrolling down to test class seeing the
code is you know indented I think I mean
it all goes back to line zero for the
strings and as I'm scrolling quickly my
brain just goes oh that must be the
start of a method and it's not it never
is
um so
this like some little tricks it does
but that's it that's rostring literals
they're really good they're my favorite
feature question thank you
yes good question if I don't indent one
of these lines it's a syntax error the
compiler knows also if I have mixed
indentation like if I had two tabs and
the other line had four spaces or
whatever it was that's all sort of
syntax error so the compiler checks that
what you're doing makes sense
um and then you know you can either fix
it by doing that or fix it by putting
name forward again
and I think Ryder also shows the
vertical line as well but I don't know
any other questions on roasting literals
before I go on to the next feature
it's really quite a weird tool because
it's basically like a reference yes
question
uh is a syntax highlighting this
question yes but that's not rush English
that's just Roslyn being fancy
um Roslyn will detect Jason and regex's
and we'll syntax highlight them
um in any string so this works in
verbatim strings as well and in fact you
can if it's not detecting it you can
tell it it's like I can tell this is a
Rejects and it'll syntax highlight it
incorrectly but
um yeah so that's just Visual Studio
being fancy I think writer does the same
as well
um
we rosin doesn't support SQL but uh the
hope within the rosin team would be that
the SQL team will do that in.net 8 James
Newton King has worked on doing it for
routes in asp.net so routes
automatically highlight with the route
syntax
yeah
Russell already supports C sharp nvb and
Jason and regex that's enough languages
I think
right list patterns
who likes patterns because if you don't
like patterns you're not going to like
list patterns either
um
patents are something that's been
building up in C sharp since seven point
something or other and every new version
there's another little thing a little
thing and this patterns is the newest
one to add
it's good to know about patterns in
general to talk about list patterns
because patterns really build on each
other
so let me
let's do this
so I can say
if args is a string or string array
this is a pattern
it's not a very interesting one but it
is a pattern this is a type pattern
because I'm checking the type of
something and if I go
and do this this is a property pattern
so I've got a property pattern and a
type pattern now obviously this type
pattern is kind of stupid because it's
clearly defined as a string array so I
can get rid of that now I've just got a
property pattern but so patterns they
they build on each other and so list
patterns is the newest one on the list
list yep
a list pattern the way to think about it
is you're essentially writing into your
code
what the collection of data should look
like so in this case this looks like an
empty array declaration well this is
saying is args an empty array
so if Alex was null then this wouldn't
pass because it's clearly like an empty
array is a thing
I can say well it's strings so I can say
one two three so this is checking is
args does it have three but and the
compiler will just literally do this
does it have three elements in it if it
does the first one one is the second one
two right
that's okay that's that's you know not
that interesting but where the where
patterns really get
um interesting is when you then build up
onto them so I can go in here and I can
use my length again
and I can say link four so this is
saying is arg's got three elements in it
first element is one last elements three
and the second element
is at least well no not at least is four
characters long I can do is least at
least by saying
that one anyway maths is hard
um but again that so this is a
relational pattern inside a property
pattern inside a list pattern so
patterns
you know this pattern's great but they
can be a bit weird to understand
personally for me and I've been using C
sharp 11 like since C sharp 10 was
released because we're on nightly builds
I'm at the point now where even still I
don't write list patterns from the
get-go
but I write a bit of code and then I
read it and go oh that could be a list
pattern and and that's sort of taken me
a while to get to it used to be that I
would only notice it a week later that I
could convert to a paddle it takes time
to sort of build in because I think
we've just got lots of muscle memory
about writing if statements
um the other thing you can do with list
patterns is
use the range
operator I guess and so what this says
is
does List have at least two elements
first one is one last one is three and
any number from zero to n in the middle
we can also capture things from them
with patterns
um I'm hoping that just says it's not
used yes good oops so this is saying are
there three elements is the first one is
the last one three and no matter what
the second one is put it in the variable
s and then I can go and use this I've
used this sort of thing for like path
parsing so you go string dot split
and then a list pattern to say well is
it program files Microsoft whatever
and then what I'm looking for you know
um and then the other thing oh and so
discards also work I should say
um and again that's just another pattern
um let me jump over to Sharp lab and I
will show you what's going on here
so if I write this code here
um
let's go with one dot dot
and three
so if you haven't seen it this is sharp
lab.io what this does is it compiles
your code with the latest Roslyn and
then it tells IL spy to decompile your
code as though it was written in C sharp
one so it basically unwinds all the
syntactic sugar and so this is why I
love patterns because all of that code I
didn't have to write so you can see we
get the length we do the length check
check the first element check the second
the last element
um
this is what the compiler is doing now
the compiler doing this is not just
saving you time it's also doing
the compiler understands your patterns
so if this was a switch expression it's
doing exhaustiveness checks so let's go
uh I don't even matter
uh switch
typing is hard
um true little Lou that's a thing
okay what the what have I done
to the compile no what have I done
I can't read my writing type or Nemesis
definition what
oh I've missed a Mr brace haven't I
too many braces
keeping you on your toes I knew what I
was doing all right
so this is the same pattern I had before
but in the switch expression but so now
if I go one dot dot
foreign
so this is cool but the compiler knows
that we haven't handled all possible
cases so that's potentially useful if I
move this up one
if I'm understanding my own code
properly
uh did sharper just die
anyway what should happen there is it
should flag the second one as being
unreachable
there we go the patterns are so shut up
died for a bit never mind
um so now it's saying this pattern is
unreachable so it knows it understands
what you're doing
because
uh well because it's compiler the
downside of patterns is they have to be
compile time uh they have to be
evaluated at compile time so that the
compiler can do this that can be a like
a downside sometimes because if I had
like let's say I had another argument
here
um I can't oh I guess that would have to
be a string
I can't do this
oops
so this doesn't work because number is
not known at compile time
so that can sometimes be annoying but
it's because of the Extra Protection the
compiler is giving you by knowing about
what you're doing and whether what
you're doing is redundant Etc so list
patterns is just another sort of arrow
in the quiver of patterns but they are
really useful the other thing is like
this is a string array but
I could easily make this a list
um
I could make this a collection you know
like it the compiler knows about the
standard sort of collection-ish
things which is good
the other thing
one other interesting thing I can do is
I can say
that
um
no
I think wait a minute
this is where I have forgot what I was
going to show and I've got it wrong
oh let's get sex right
no unexpected character what
oh maybe
sometimes sharp lab also gets confused
and thinks I've got characters I don't
have
there we go that's it was it so so now
what this is doing is this is saying uh
at least one element because dot.com
match zero
and then any number after that but
capture that into another sub array
so that can be really useful to pull
things out and you can you can pull
things out in the middle as well so I
could you know I can do this
so it can be really powerful you can
only have one range operator in a
pattern in the list pattern so you can't
put two of them in and like it doesn't
do like wild card matching but this can
be really useful the trick with this or
the thing to watch out for with this is
if I change this back to a list
um yeah so this now is not compiling
because it doesn't know how to pull out
the middle of a list into something
um arrays it knows spans it knows but
lists it essentially doesn't know it
does know it just doesn't want to
allocate memory to do it
um that's list patterns any questions on
list patterns
that's cool if you think one later no
worries 20 minutes
all right to utf-8 string the tools I'm
gonna have to speed up a bit here
um this one is relatively Niche if you
need this then it's great and if you
don't need it then you'll never need it
essentially if you have any code
in your uh
applications in your programs that deal
with byte arrays
or that deal with utf-8 strings this is
useful for you so what this is it says
utf-8 string literals but I don't like
to think about it this way because it's
not really a string
if I go over RX equals ABC so the u8
thing on the end is how you denote this
as being a utf-8 string just like you do
M for decimal and you know f for float
but what this is if I hold my mouse over
VAR which well you might be able to see
it you can't zoom in it's not a string
it's a read-only span of bytes
um and that's what makes this a little
bit Niche and if I go to array then it's
a byte array so it's not a string you
can't do string operations on it this is
a handy way of writing byte arrays
so
if you do lots of processing of utf-8
strings utf-8 files and you process them
as byte arrays and if you've got checks
for you know is this equal this if you
do a normal string check on it then it's
not as performant as it could be because
strings in.net are utf-16
so if you want performance you compare
them against byte arrays but if you
compare them against byte arrays you
then have magic numbers in your code
that don't look good so utf-8 strings
string literals are a way to avoid that
but as I say if you don't do that you're
probably never use them
required members okay this one I will
take a few more minutes with let me
write a person class because that's what
you have to do demos with
I'm going to write a first name
property
let's go in no let's go yeah
said it was good
and I will go
middle name and last name so here's my
person class it's the same person class
we all make that we definitely all have
and we have warnings we have null
ability warnings because I haven't
provided a value I've said
these properties are strings not
nullable strings and therefore I have to
provide a value this is really annoying
because I don't want to provide a value
I'm just creating a class I want whoever
creates my person to provide a value and
so I can do that
by saying that this is read only what
this is doing is this is telling the
compiler
first name middle name last name they
have to have a value they can't be null
and they have to have that value set
when the person class is created this is
presumably complaining
uh
read only what did I say required thank
you
ah dear there we go
so now if I go up here and I go
to create a new person
now the compiler is complaining but it's
not compiling at me it's complaining to
whoever is creating my person class
which is exactly what I want because I'm
just creating a dto
and I can come here and I can say oops I
can specify a first name and then you
know obviously this is a terrible uh
a terrible person API we don't want
middle names to be required I don't have
a middle name and not everybody has a
last name either
so now everyone's happy I've defined my
detail how I want
except for the semicolon and my
consumers know what I expect
I think about this feature in terms of
fixing this whole inull ability but it's
actually strictly
unrelated so if I make last name
required again
because
no not everybody has a last name but for
my target audience of mainly Western
people most of them do
I still want the consumer of this class
to think about the last name
so it's still nullable
I don't have to set it to a value or I
mean I do it's just that null is a valid
value
so it fills a gap in nullability but
don't conflate them necessarily that's
just me
um
what this is really an Adolf is the the
style of c-sharp programming that is not
using Constructors so object
initializers like I have here they're
getting a bit more popular now we have
records with with syntax and Anonymous
types and all these things there's other
ways to construct things and so this is
a way to say
you know in the old days or the current
days you create a Constructor and you
put on the parameters that are required
to be set and you can't construct the
class without calling the Constructor
this is a way to say well you can be
more flexible in how people create the
class but you still can denote what is
required what isn't
speaking of which if I create a
Constructor
let's create one that takes it first
name
and we need an empty one so that my code
above doesn't error so this is cool and
if I create a new person another person
we have a problem
and the problem is this says you haven't
said any required properties
but in fact I have in fact if I make if
I take required off last name I've said
all required properties
so that this problem is one that
sometimes emerges from the compiler in
general the compiler doesn't
inspect the code inside a method to see
what it's doing
and the reason for that is it gets very
complicated because your method could
call other methods it could hit a
database it could call web service the
whole thing problem is a thing so
generally speaking with null ability
with required lots of features it
doesn't look inside the method so the
way we sort of get around this
is there's an attribute called sets
required members and what this says is
if someone calls this Constructor don't
complain about required members of them
now you'll note that that Constructor
does not set the required members so
that's me telling the compiler hey don't
worry I've got this if I don't have this
that's on me
but it does mean that both Styles can
live together in the same code you can
have Constructors and you can have empty
Constructors but you can still denote
what you want to be nullable what has to
be required what's settable like we've
got init properties to say they can only
be set once all these things so it's
just about more expressiveness in your
details
any questions
static abstract interface members
this one's a little weird because static
members on interfaces exist already
but they don't work this way so this
abstract as a keyword to denote which
one you want but abstract is kind of
redundant on interfaces
but anyway what this does is it lets you
define a method on an interface
and say that it's static and then just
like every other method on an interface
normally
anyone implementing that interface has
to implement that member
and they have to implement it as a
static member so in this case we have a
property called can walk on eye animal
so anything that implements eye animal
has to have a static property called can
walk
that's the compiler feature it's not
super exciting
where this comes in where this is useful
is with generics
because in my class down the bottom here
I've got a generic class and I know that
t has to be something of this interface
but I don't know what that's the whole
point of generics that's why I'm using
it so I can't call dog.can walk because
I don't know it's a dog and if I could
call dog.com I didn't need the interface
so this lets me call a static method or
property on a generic type
knowing that it implements interface
so where this is really cool
there's two there's two things this is
really cool for so number one I'm going
to create an interface called I async
factory
and I'm going to make it generic
and I'm going to put a static abstract
task of T self create
async method
so what this means is
I can spell I swear
who decided to spell check when vs was a
good idea I'm going to create a class
called Google because I like the word
goo
it's oops I async factory of myself
I implemented a geostatic method so this
is yeah this isn't very exciting like
the generics maybe look confusing but
this is just a static method that
implements an interface member but what
this means is
yeah the nicest thing is icing
Constructors in sejap I just made them
if I have some kind of method that I
want to be able to construct in implant
instances of things then I can make that
method
take in a t and I can say where T is an
ice icing Factory and now I know that
there's a create method I can call that
will asynchronously whatever that means
create an instance of that class what it
actually means it's up to like I don't
know wait to stop delay that's that's
there you go
um I think this is really cool for this
sort of thing because this is something
we couldn't really do in C sharp for you
couldn't you could say a generic type
had to be uh had to have an empty
Constructor so I had to be able to
create it but only empty right not with
um
well it could be icing couldn't have any
properties whatever
the other thing you can do here is is uh
just allow like have parameters so maybe
I say that I want everything in my whole
system to be constructed you have to
take an eye logger because I want
logging to be really important so yeah
have this static method uh here have a
parameter so then everything in points
it has to have that parameter so this is
a way to
well in the past I would have done this
by having this interface and I would
have had this create be a normal method
not a static method and so you'd pass in
an instance of the thing and it would
call the method and it really should
have been static but it just couldn't
this now allows for that the other place
this is interesting that you possibly
have heard of is generic math so this is
a new feature of the.net runtime that
takes advantage of this feature so I can
make what are we doing we'll do the
classic
ad method whoops uh I forgot
I'm gonna make an Adder no wait no I'm
not I'm gonna make a method
um
so add we're going to add two integers
together
great
no worries I can call add
whoops apparently I can't
okay
that's not very exciting but of course
this only works for integers if I want
to work for other types that's where
generics come in
but whoops
but with generics I can't add them
together because
what does that mean
so it turns out in C sharp what is what
you write as a plus operator is actually
a static method and it's a static method
called op underscore addition and so if
I say where T is an i number
of t
now my crochet bars the reason for this
is I number is an interface that says
You must have a static method called op
underscore addition and the integer
class in 32 class implements that
and in fact if we look in I number
um
it's actually
not implementing much
weird oh right eye number base
apparently
there we go there's the list uh there's
a lot of interfaces I Edition operators
is the one I want
and this here it is here's the operator
so the n32 class implements our number
which in turn implements this method
which means the compiler is now happy
for me to say a plus b on this generic
type of T which I have no idea what it
is but I know that it at least has to
support I number
where this comes in more useful is
things like
where is this
lifespan possible probably yep so I
parsable is an interesting one for me
this is a new interface in.net and
various types will implement this and
what this says is whatever type it is
has to be able to par be parsed from a
string
and I can think of a number of times in
the past where I would have wanted this
behavior in some generic method like a
deserialization method is a perfect
example digitalize this string as this
type well if that type has a parse
method you don't have to write any code
then right whoever wrote the types
written for you so there's a couple of
aspects to this you can write your own
interfaces and you can now have static
methods on them that's good
you can take advantage of I number and
you can do generic math and if you've
got math heavy code then that'll be
really useful but also there's lots of
other interfaces in.net that you can
just explore and a lot of the inbuilt
types will support them so that I
formatable well iPhone edible existed
but there's now an iPhone edible that
actually has static methods that mean
things
any questions on static abstract members
or generic math not that I'm very good
at math
cool
generic attributes there's not much to
demo about this one it's pretty much
what you see on the screen
in the past you would always have to use
type of attributes and then you had a
type and then you had to do something
with that type and that can be annoying
and now attributes support generics so
now in my second attribute there the
generic attribute instead of having a
type that I have to play with I have a t
and
because of all of the other things that
generics allowed but for example static
abstract members might be one of them I
can possibly do things with that tea
without having to go you know activated
or create instance or using reflection
to get properties Etc
um there's not as far as I know there
aren't any generic attributes in the BCL
yet
this is just for
things to take advantage of
Auto default structs who does stuff with
structs anyone much
just you okay I don't
um I've managed to avoid drugs but
exactly why I like this feature because
this
I don't use drugs enough that I forget
the rules and this feature as well as
one from C sharp 10 makes
makes them makes the rules more like uh
the same rules as having a class
so
oops order default trucks um
in c-sharp nine
having this empty Constructor would have
been a compiler
you weren't allowed to create a
parameterless Constructor for structs
the compiler creates one for you and
that's it in C sharp 10 that restriction
was relaxed and now you can create one
but
in C sharp 10 if I create
a field in stitch up 10
when I call this
no sorry in Cinema 10 this would have
been a compiler right now it's not a
compile error it's very confusing
because what happens is if we scroll
down here we can see what the compiler
is doing the compiler will now
synthesize a Setter essentially so
setting the field setting the property
whatever it is it'll synthesize that
into your code into your Constructors to
initialize any field or property to its
default values so this is exactly how
classes work the difference is that in a
class the compiler doesn't actually have
to do the work because when the class is
allocated on the Heap the runtime clears
out the memory for you right that's how
classes work structs get allocated on
the stack they don't clear memory so the
compiler has to actually write the code
that says x equals zero but yeah so what
this means is that structs just act
look essentially it's going to act like
classes but the rules for creating them
and writing them are now much more
aligned which is good because I can
never remember them
what is worth noting here if you do use
structs is the difference between
empty Constructor versus defaults versus
a real Constructor and you know if I
delete this you know
you can you can sort of see what's going
on here so without an empty Constructor
the first structure we create which
calls the object structure is the same
as defaults
if I put my Constructor in it now does
something different so this again this
came in in C sharp 10 but it's worth
calling out that there could be
difference between like I mean if I just
go like that so there is now a
difference between an empty Constructor
and a default of this struct and that
could be a trap but again it's kind of
what the author of destruct intended so
it's maybe not a trap you know but worth
noting
all right we're going okay for time
this is the last full slide but I didn't
set the number of features in C sharp
11. so the talk only goes for as long as
they did work
um refills and refscoped
in the past I've said this is a niche
feature and then people complained that
it's not Niche because they use it all
the time that's fair I don't use this
and I don't know if anyone in this room
will
it's the kind of feature where if you
need it you probably already know about
it and don't need to come to this talk
but I'm gonna try my best to explain it
um structs are weird that's my
explanation so structs get allocated On
The Stack
and
there's a certain type of struct called
a ref struct which gets allocated on the
stack but it also can never be on the
Heap it's a rule that's what the ref
means and span is one of those so I
can't have this span field on my struct
even though my structure is going to get
allocated on the stack and a span has to
live on the strap stack I can't have the
field there because what if my strut
gets boxed and gets put on the Heap or
as part of an async method and gets
shoved off to the side and wakes up
three seconds later or whatever else so
the rule says you can't have uh what's
called a ref struct which is what span
is you can't have a ref struct on a
struct you can have a ref struct on a
ref struct so I can say that my struct
is now a restaurant so it can only live
on the Heap the stack whoops
and therefore it can contain things that
kind of live on the stack and so the
compiler can verify all of this by me
declaring what the rules are for my
thing that implies the rules for the
things I hold but now we have another
error up here which we didn't have
before I can't pass this parameter to
this method on a ref struct because that
could allow that parameter which is
owned by the caller method could allow
it to leak out
right this truck could do something with
it that would be bad and we can't have
things on the stack leaking because when
the method that called it with the
method itself or the method of course it
goes out of scope that memory is used
for something else so we can't have
anything leaking so the new feature here
is that I can say this is scoped and now
my code compiles and I can tell from
your faces you're just as excited about
this as I am
what this is doing is essentially
there's new ways well the scoped keyword
is a new
another another way to tell the compiler
essentially what you're doing but it's
it's you promising to the compile that
you won't do bad things but it also
allows the compiler to verify that you
won't do bad things so scoped this this
parameter is scoped so I can I can use
it in this method if I tried to pass it
to another method that would be a
compiler unless that method was scoped
and so it's got to be scoped all the way
down it's going to be ref structs all
the way up right so the rules are kind
of straightforward but by building on
top of that rule system the compiler
essentially can trust that you're doing
the right thing
so again if you know about ref
constructs and structs and Scopes and
whatever great and if you don't even
better
um
because I don't either
you can even you can do read only ref or
ref read only or read only refereed only
and they all mean different things and
I'm so glad I don't talk about it all
right so this is the last slide so if
you have any questions if you've been
holding back
this is the grab bag
um of features over 42 minutes which is
fine I don't mind I can finish early
these are just small things and in fact
I'm going to go from the bottom up just
to make it quicker so numeric ink
pointer is not very interesting there
are data types in.net called n int and n
u int actually I'm going to type because
this one's funny sorry so there's mint
right which is native integer and then
there's native unsigned int and every
single time I type it I type n unit and
it really confuses me
in C sharp 10 and below
these were weird things that would be
represented by the int pointer class so
the int interpreter and uint pointer
these would be represented by this class
but the compiler allowed you to do a
little bit more with them it was a bit
weird so now in C sharp 11 they're just
aliases in exactly the same way that int
is in 32 and string is string those
things again if you don't use them then
you don't thing
um
matching span of char is useful if you
use spans so
um if you're not don't know what a span
is or span or a read-only span is a way
of
essentially saying that you want to work
on the memory that represents a bunch of
characters but doesn't have to be a
string
right it could be a file it could be
something else or if it is a string it's
just a it's a way of dealing with it
without
essentially saving memory allocations
that's what spans for so span of char
specifically is very commonly used to
point to Strings but you couldn't do if
statements so if you had a span of chars
you say well I don't care what the
backing memory is for this string
essentially
I couldn't even check if it equals
something else and so you now can we
know how to do the pattern matching on
spans of jars
fire local types are
maybe interesting
um
so this is being able to put the file
keyword in front of a class and so this
let me go
I don't know
there's a method so I can go C dot
Google
but if I create a new file oh God
let me do this again and
if I create a new file
C dot oop C doesn't work
um so a farther class is exactly what it
sounds like that class C that I created
there is only valid in this file and in
fact I can create another one over here
and it'll work in this file
oh
whoops
because it does
what have I done oh that's it helps to
be in a method honestly because I used
to writing shorthand code I forget you
need you need to put things in anyway so
I can have multiple classes with the
same name in different files and their
Skype differently and in fact if I jump
across the sharp lab
oops
you can see
Java Diagon
push-up
I've seen it demoed in at least three
talks today so it could just be come on
shuffle
okay uh yeah check that class name out
so a little
pile of magic to make it uh unique it's
actually a hash of the file name so it's
not random it's deterministic and things
work
um they're mainly useful for Source
generators if you're into Source
generators because you don't have to
worry about class names clashing if you
say this is only valid in this file
you'll never have a problem
name of parameter in attributes that's
pretty
simple so X and I can say
encryption and tablet string name of x
hello
Talking Max
and that should be Embrace that should
be bracket there we go
um this didn't used to compile
in C sharp 10 this would say I don't
know what x is and obviously what you
meant was the parameter and so now we
allow that that is very useful and comes
up reasonably often but
probably not that you can name right now
new lines interpolation holes is
fantastic I can do that that's the
entire feature I can put new lines in
it's actually very useful it's just kind
of you know didn't deserve a whole slide
um
all right and last one 45 minutes
it's all on you if this is going to take
an out it's all up to you got to ask
questions
um
this one's good so this one is uh
a perf Improvement that lots of people
do
but it's a readability
uh detriment and so in C sharp 11 we can
now get the Perth benefit without the
readability judgment so what this is
about is about
capturing method groups so I have these
two lambdas here
one I have written as a full Lambda and
one I have just written as the method
name because the method signature
already matches the Lambda
excuse me
and if I scroll down to what this
compiles down to to these two lines and
it's a bit hard to read because the C
sharp compiler generates these types
with crazy characters
but what this is doing
so for the full Lambda syntax the first
one this says
there's a type called C which is this
class helper class and there's a field
on it called 900 square underscore zero
this one and that is that field holds a
reference to the the Lambda and so this
says use that field
it's a static field
if it's not null and if it is null then
we initialize it to a new Lambda
that's caching in order to avoid an
allocation
right so we only create this Lambda in
memory once
the second line where we just use the
method name is just saying yeah it's a
new action this is allocating memory
every single time this is called and for
the wrong bit of code in the wrong spot
this could be a problem and so you go
through and you make all of these things
actual lambdas and it makes your code a
little bit more ugly it's not a huge
difference when I'm like creating Fields
but if you're passing lambdas into
methods it's really nice to just see the
name of the method that's going to do
the work than a Lambda so what we
what what needed to change here
interestingly enough was the c-sharp
spec which had a line in it that said
um
something about you know
you can't cache Lambda delegates and now
it says you sometimes can like that's
that was that was a change but so if I
make this static we get this new
compiler behavior and there's all sorts
of reasons why it doesn't do it for
non-statics and I'm not going to go into
but if I made that method static we can
now say these two lines essentially do
the same thing the names are different
it's a bit quirky but this is now only
allocating once and so your performance
is better
but it's also still more readable
and that
is the end of the talk because that's
all they'd even see sharp 11 and
um unless you want me to talk 10 minutes
about what's in C sharp 12 but I don't
know yet because it's really early
is there any questions about anything
or you can go get a coffee
or you can go get dinner not dinner I
don't know it's 2 am where I'm from
hello question
um
uh
you declared a
hurricane
uh okay so what are the implications if
you have different implementations of
the static method with different
instances of the interface
yeah so this is this is maybe one of the
weird things about static
so if I have an interface
called this and I say static void
that
versus saying static abstract
oops
void I guess I should probably give it a
different name
um so interfaces could have static
methods before and you called them by
just doing igoo Dot
because they were just static methods on
a type an interface is a type class is a
type of structure is a type
um
and what you could then do so I could go
class
an animal I'm mixing my metaphors now
I can do this and I can implement
this interface
and I can say animal dot Google whoops
maybe I can't
okay I can't
um
you can do that with base classes and so
maybe I thought that's maybe where
you're going
um
so the answer to the question though is
is kind of
there isn't a problem because if there
was it would already exist with
interfaces so if I have a variable
if I have a variable of type animal
then I can't call that static method on
it
like it's a static method you have to
call it on the type
so
if you're worried about getting a cat
but calling the method on dog you would
have to literally type in dog dot goo
and in that case well you're calling it
on dog there's no ambiguity so it's a
bit weird and I get like I understand
why the question because this seems odd
at first but
um
they really are only useful with
generics you wouldn't normally you
wouldn't expect to have a strongly typed
uh
variable or local that you would use
these with because if it was strongly
typed you wouldn't worry about the
interface
I don't know if that makes any sense I
got confused halfway through
any of
them
um okay well cool let's yeah we've got
seven minutes let's see what I have an
objects
string so string G that
why do people hate this I love this I
did a PR into so I work on the Razer
editor mainly and I did a PR into that
that removed about 2000 lines of code
just by putting in talking exclamation
marks
um
and I then had to remove it which was
really annoying
uh because I couldn't just get revert
because we've done a bunch of work
anyway don't be an early doctor people
didn't like this for a couple of reasons
um
number one reason they just didn't like
the syntax they just thought it looks a
bit angry I don't know I I'm I don't
mean to make fun because they had valid
valid concerns I just don't know
um
there's a lot of people who wanted it on
the type
so they kind of you know if you think
about nullable is on the type the
question mark would be on the type so it
makes sense to say not null is on the
type but that kind of falls down in a
few scenarios
um
where it doesn't make sense
um
people
I think were
well I quite like that you know if you
want
um
the null
the the exclamation mark is the null
suppression thing right where if you've
got a nullable thing and you say uh yeah
you go null it's like yeah it's not now
trust me
um I kind of like that it was an
extension of that whereas like one
exclamation mark tells the compiler to
shut up about nulls two exclamation mark
tells the run time like I kind of like
that but it's also a bit different
because one tells the compiler to stop
complaining and the other one tells the
runtime to throw an exception which is
actively complaining so I kind of those
sorts of reasons there's a very long
thread on C Sharp Lane and I promise you
and again I don't mean to make fun but I
promise you every single thought you're
having now about why this is good or bad
has been discussed in that thread
um which is a good thing it's very
thorough there's lots of Engagement with
the community
um at the end of the day the biggest
problem problem with C sharp with this
sort of thing is that C sharp is 20
years old and it is 100 backwards
compatible and so any new syntax that
goes in now
it's there it's there forever it has to
be supported forever and you can't use
that Syntax for other things so let's
say null suppression maybe get some new
way to work maybe the logical thing
would be to put two exclamation marks on
we can't because that means something so
it's very hard to introduce new syntax
it's very I guess it's it's sort of
risky in a way because you are really
committing yourself so if the versioning
of C sharp was a bit more lacks and we
weren't so backwards compatible and we
weren't really tied to this forever I
think we might have seen a different
results but it is hard to put in new
syntax that isn't
um you know it's not a slam dunk which
actually brings up another thing I
wonder if I can demo this
is a another interesting this is kind of
a compiler feature but it's not a
language feature so I didn't include it
but it's relevant here
um one thing that was going to be in C
sharp 11 was the field keyword
but
you could have a typical field or a
field called field and that could cause
conflicts conflicts is something the
c-sharp compiler team the language team
has to deal with all the time if you
want a new keyword in the language like
record or like scoped or whatever you've
got to think about well what's going to
happen if someone has a type called
record or a type called Script or a
field call or a parameter or whatever
what are the rules what's going to win
what's going to what are we going to
resolve this to and so one thing that is
new in C sharp 11 the compiler is that
it will now warn if you have any classes
defined with lowercase names
because the problem of trying to work
out new syntax to add is getting so
difficult that we kind of need to stop
people from taking all of the good words
so
so things things like that is why it's
it's a big ask to put in new syntax and
so it's not just that people didn't like
the syntax it's that if we put it in it
would be there forever and we'd have to
support it forever and we couldn't use
it for anything else
and if there's no more questions then I
will let you escape the room
thank you very much