Abusing C# - Jon Skeet

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Pretty awesome stuff, thanks for the share!

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/ThatsFudge πŸ“…οΈŽ︎ May 08 2017 πŸ—«︎ replies

Damn. And he didn't even cover half the content he had prepared.

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/Khao8 πŸ“…οΈŽ︎ May 09 2017 πŸ—«︎ replies

Wow, tough crowd, eh? (I mean the audience in the video)

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/TikiiTorch πŸ“…οΈŽ︎ May 08 2017 πŸ—«︎ replies

Neat, but that's pretty useless stuff lol

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/VOX_Studios πŸ“…οΈŽ︎ May 09 2017 πŸ—«︎ replies
Captions
um I just I made the mistake of checking to before starting and I retreat saying top conference tip don't use the first person in in your talk instead of saying I will teach say you will learn etc I've already violated that and I just done it again and again and I'm afraid I'm probably going to keep doing it this is in some ways quite a personal Fork and the good thing is if I keep saying I then it means you don't have to feel any association with the codes that you're going to see which may be for the best um if you don't me don't know me I'm John skeet I work for Google I've worked there for about eight and a half years for the last year and a half I've been working on the C sharp support for Google cloud platform it's awesome go check out our booth um I've also been abusing c-sharp for a long long time I used to do this because I wasn't writing any production c-sharp so it was fine to write awful awful codes that was kind of fun knowing it would never end up on a production server now I need to be slightly more careful that what I write will you that I separate the abuse part from the regular code the idea is to look at interesting bits of the c-sharp language which are right for being misused and misuse them ideally in a way that isn't just well this is crazy code no one would ever want to do this but here what that actually looks quite appealing but no no no definitely not um I would like this to be a collaborative effort so in particular you know Kathleen knows the language at least as well as I do and I'm sure some of you do as well so if you suddenly spot other ways that we could abuse stuff you're related to what I'm doing do shelf out likewise if you're just come up on Sage and start typing um I've done it to other people no reason people shouldn't do it to me likewise if you don't understand what's going on then it's possible you may actually learn something in this session and there are at least a couple of bits of code that might be considered a bad idea or might be considered a good idea and I might ask for a show of hands but at least you need to think carefully before using but they really are appealing there are some more chairs around on this slide by the way so folks are standing it doesn't bother me if you walk in front of me it's fine okay all the code is up on github I have a blog that's almost entirely about code I have a non code blog that's currently almost entirely about feminism and we have I have learnt that this talk is better if I work out what I'm going to teach you or show you beforehand out of interest this is called abusing c-sharp more because I've done abusing c-sharp as a talk with some of the older stuff several times how many of you have already seen an abusing c-sharp talk by me okay not very many so I might revisit some of the most fun bits of that just for a laugh and if not there's far far more code than I could possibly go over so it's really sort of picking some some things we're going to start with the Mongolian vowel separator which is my favorite unicode character how many of you were bill whiteners talk about language design yesterday a few we talked a little bit about breaking changes and things that the language specification refers to and I mentioned Unicode and Bill was saying well that doesn't change much allow me to differ so we have a bit of code here most of this code I will show you some code we'll run it and see what it does basically so what do you think this code is going to do sorry you know what I missed something sorry they were I do want to call yeah it's annoying I got a different version that had the Schofield call but not the assignment right okay so now more usefully so I've got a little framework that is just going to call me it's going to do the obvious thing a thoughts it's going to write in main okay I will prove that you're actually correct and then I'll show you why you might not have been yes it prints in main if I have run this and someday I would love to try to find a version of Visual Studio 2010 but with a rose in preview running on running on a version of Windows from about 2010 and because then I think it would print in initializer and those of you who are very close to the front how many of you can read the line column and character saying on the on the status bar how many of you can see okay enough to see the numbers so normally focus on the coal and chair as I just scroll around so if I go to the left right as I go I'm pressing pressing pressing pressing so I had to press twice to go from the G to the X and that's because the thing in the middle there is the Mongolian valve separator it doesn't exist here this just goes 28 28 29 29 30 30 no it's all it's all that so we have a different sequence of Unicode characters here and here but that's okay because the language specification says that if there are any Unicode formatting characters within an identifier that's fine and they'll sort of be normalized the way in fact it doesn't say that you'll normalize them ill say when you compare when you try to find an identifier ignore them and in fact the way that the obvious way to do that is just by normalizing them away and so if you ask by reflection for string you will get the version without without the Mongolian valve separator so why does this mean in 2010 it would have printed something differently any Unicode excuse me and trivia fans out there well let me show you the history of the Mongolian valve separator it was introduced in Unicode 3.0 where it was introduced as a formatting character and then in 4.0 it became a whitespace character and it's played a whitespace character until 2014 so if we've been running on something Unicode using Unicode 6 it would be whitespace now let's just replace the Unicode that the mongolian vel separator with a regular white space now that's still valid code but it's going to print in initializer yeah so as fine I don't know of any other characters that have gone from one Unicode category to another and then back again and particularly to stay valid in identifies but mean different things or rather either separate identifies all or be part of one so you might be wondering what the specification itself itself says and for every published version of the c-sharp specification it's gone too it's specified that it uses Unicode 3 and the the old CF feed Aleksey the native one used to carry alongside it within it a copy of Unicode 3x ma there's an ECMO standard for c-sharp the third edition covers c-sharp to the fourth edition will cover c-sharp 5 when hopefully we get that out this this half I really really hope and then the idea is that we can between ECMO and Microsoft we can get the C sharp 6 specification and standard out as sort of one thing so you'll have seen that there isn't a c-sharp six spec publicly available other than a draft version in a github repository belonging to Lucian Bishop who's no longer even in Microsoft but so ACMA said it has before so if you tell the c-sharp compiler to run in ISO so ECMO is one standards body ISO is a different standards body but the ACMA standard was then fast-tracked to the ISO standard so ISO to is equivalent to ECMO third edition is somewhat equivalent to C sharp to and I think this is still what's he going to print if I can actually run the thing come on controller five so builders oh okay yeah half of the rest of the code in the project doesn't work under c-sharp - let's see if i pull up come on prompt I can write it the nice thing about compiling the code with main methods like this is I can compile just a single file so it's in odds and ends and they all remember the command line that you use the specifies of the line version so if I do line version ISO - I'm gonna involve separator so it showed in main still basically this shows that the the compiler is not really obeying the language choice that we've said because that should mean use Unicode for as of Rawson rather than just uses whatever version of Unicode Ness is using because that's the only thing that really makes sense to keep going with so in the fifth version of the unicode spec sorry the fourth edition which is C sharp 5 the upcoming one we're just waving our hands kind of saying it uses a version of Unicode and let's hope it doesn't matter let's stick with strings a bit more and tighten it use name of so I'm going to add a class notion of added before so name of intro how many of you are using C sharp 6 good and you're you're aware of name of hopefully and I'll just rename into to intro which is what I meant so the next thing is if you come into that ever so common scenario where your Shift key is broken or possibly you're working on a keyboard which is in a different language and you don't know where the double quotes are so you're unable to type of string literal and this is a problem we often monstering literals and suppose you're in a slightly better situation that vo the string literals that you want to write doesn't contain any spaces you've got problems well you can use name of to help you so you could declare a method so suppose we want to print hello world all is from one word okay we can do console dot write line name of hello world okay so without typing any double quotes we can now run that and it will print hello world but you know that kind of sucks because you've got to introduce a whole extra method and that's not fun and you could have you could do a variable called hello world instead and then it's still just a bit nasty and then you think of dynamic so one of the things you can do with name of is refer to instance or static methods members via a reference so normally if I do string x equals whatever even know and I can't type X dots give me a static method on string is null or empty I can't use X dot is null or empty because it's a static method and c-sharp made the very good decision unlike Java that you shouldn't be able to call static methods on variables on any kind of reference as if it's an instance method what I can do is refer to the static method via an instance I think no it's the other way around you can refer to an instance yes sorry you can refer to an instance method and I can do exports plate I hope no it's because I'm lame old si I hope that you would be on your toes yeah and that now works good whew right so even though is null or empty you can't call as X dot is no empty you can use X dot to just say what type we're interested in great so now that means I can write hello world without ever introducing anything in my program text that says hello world introducing any kind of members because this syntax also works for dynamic so we can write dynamically is null and then do name of d dot hello world or d mongolian Val separator and as you can imagine D Mongolian Val separator has the Mongolian Val separator between the N and the V and then between the Ellender capital S so between the words and as a bonus feature not only is it allowing us to write that text but it will also do unicode normalization for us so this is exactly the same on copy copy pasting into here so you might expect that that length and that length will be the same but actually the length property on string is going to include the mongolian val separator whereas the version here is not going to be the same string because it doesn't include it it's been normalized away and we can see that if we do name of anything see that we get Mongolian valve separator we have a length of 23 without the mbss and 25 width so next time you're faced with a broken keyboard or you know if if kathleen's up here and trying to find the double quotes you no longer need to worry about it just use name of on dynamic see always useful stuff okay yeah and just those next caffeine she doesn't have anything like fruit or anything with heard she just okay hmm I may need duck Kathleen yesterday we're saying about when not to use topple deconstruction well how about for dates and times how would you deconstruct a date so this is C sharp 7 feature you can write VAR XYZ equals foo if who has a deconstruct method with 3 out parameters and the trouble with States is you know we like to think of it as day month year and Kathleen probably like to think of it as months a year and we should really you know be open and inclusive of everyone so really what we want to be able to deconstruct using date time extensions and you have x y&z and who knows what x y&z will be maybe they will be year months they may be there will be months a year maybe there will be day month year so if we run this and run it the first time it's doing day month year which is you know reasonably sensed it and it's sensible if I do hash define us date format sorry it was it was months a year then presumably and actually I don't define it there I define it in the extensions so I can define and it needs to be above the using directives again I can define that we want the ISO date format which is kind of useful instead of the u.s. date format so I can do us explicitly 119 and if I do UK date format then I don't need to change my calling code at all I can put this one place in the project and it will now do 19 1 2017 and that's just simple preprocessor thing but what I do like about this is that I haven't changed my implementation at all my deconstruct method I've just changed the signature to have day month year or months a year or year month day and then the body of the method in every case uses the same thing well ok that's that's fine if you're happy to set the project do this define not here but in the project properties but what if in some source files you some of your fourth is written by UK developers and some of its written by us developers and they both want to be able to not write you wouldn't actually write that you would write whatever comes naturally to you so I bright day month year in fact I write year month day because I'm so used to the ISO format and then in a different file Cassidy might want to write month day year well we've got her covered too because instead of having one implementation we can have three different implementations all still extension methods just in different classes and so long as we use using static and say which one we want to import we'll get the right thing so if we this will do the ISO date format so we'll end up with 2017 119 yes and then yes if scattering were writing some code instead we could use this and she would then declare var months a year and it would all do the right thing so this allows you to let developers from multiple cultures write perfectly nice code for themselves so long as only people from the same culture ever need to read or maintain that code then everyone's happy don't do any of this if I didn't make it really clear before don't do any of it however I do really like I would recommend the using static as a way of importing extension methods we have this in no time I have a testing assembly which has a bunch of extension methods on int and double and things so that you can do this won't work here but I'd be able to do local date birthday equals 19 June 1976 which I wouldn't recommend in production code but it's really clear when you're writing test code for you know maybe you've got some analysts or whatever has said suppose the year starts here the business year starts on April the 1st well 1.8 Perl 2015 is really really clear and and I've got different ones for local dates or you know another time I could write duration session length equals 1 dot hours for example but I don't necessarily want both the hours extension method and the June extension method at the same time and before c-sharp 7 so it before c-sharp 6 this is a problem because you imported the whole of all of the extension methods within one namespace in one go just with using stuff now you can pick I want the extension methods from that class please and that class but not the one in the middle this is one of my beefs that I've been giving feedback to Microsoft on since 2005 and as of 2015 it's all good ok do shell if there's anything in the meantime that you want to come up with otherwise I'll just keep moving on to the next thing so how many of you like regions hash region stuff yeah ok um I used to like it more than I do now I sometimes use it if if I've got a huge amount of code that's sort of all boilerplate stuff that's quite handy but I don't use it religiously all the time um the trouble is it looks a bit boring but the great thing is with expression body members we can get something that looks a little bit like that but a bit more attractive just by declaring a property or a field well a property so ignore the the implementation details for now but look how we can write something that's a little bit like a region okay it's not going to fold but isn't that nice we can highlight it as much as we like we could you know get rid of the middle bit so long as we've got some balance actually I'm not even sure that balance is required but we can you know have a bit awesome code you could use this for different levels of headings it's great and the the name Jay here I couldn't find anything any Unicode identifier that looks like a backward semicolon and we do need to have the semicolon so if anyone finds any Unicode characters that are valid in identifiers and I could use as the variable name that would be awesome but so this is a property of type J call underscore which is a get only property that returns a well it's got to return a J and J is a delegate that itself returns a J when given a middle bit as the sort of fairly self descriptive stuff that comes in the middle so we've got a lambda expression that itself returns a lambda expression which returns all we've got some strings here well we've got to have been given this this will have to be middle bit in order to get the lambda expression for J to work so if this is a middle bit we want to return a J given a less than or equal between a string and the middle bit well okay less than or equal between string and middle bit we can do here we've got operator overloading but then we also need to be able to convert from a middle bits to J and that's fine we can just do an implicit conversion and all of these do null is not actually going to do anything and I can't remember offhand oh yes the reason I need the implicit conversion from a J to a little bit is well that's going to be a middle bit but then I need to compare that with another middle bit it's possible that I could get away with less than or equal between J and middle bit as well but an implicit conversion is simpler the one you know the one thing that you might want to complain about this code and obviously the only thing is that we have to include these greater than or equal to operators as well and that's just the compiler won't let you do less than or equal to but not greater than or equal to so you know maybe that's the one thing that makes you decide not to use this code who can help okay preprocessor oddities this is a slightly old one and this is not new to c-sharp at all but it's just an interesting thing around the fact that c-sharp doesn't really have a preprocessor so if you give the seed pre-process or something it sort of doesn't really care about the code it just cares about your hash defines and your hash if etc etc c-sharp is a little bit different so you can confuse things I wouldn't ask what you think this will show because actually Visual Studio is highlighting it already but here we have we're defining foo up at the top and then we're saying if foo and so we're printing out so is defined and then we start a multi-line comment and this is the interesting bit the hash elf I believe in a C preprocessor and someone will say I'm wrong I believe in the C preprocessor that hash else would say oh no well who was defined so I'll ignore everything from here onwards the c-sharp compiler is saying no you're in a multi-line comment so the piles are really LexA you're in a multi-line comment so that hash else is just part of the multi-line comment that's absolutely fine so we'll keep going and this is all part of the comments and the comment ends with this with this star slash and at that point we're out to the comment so this elf means the preprocessor is saying oh I'll just nor everything else fine so we just print foo is defined if we don't define so let's define foo - instead suddenly okay we don't see this and we don't see the start of the comment you can write whatever you like in stuff that's ignored by the preprocessor so it doesn't care about the start of a comment so it sees this elf as the bit that it should start caring about and now we print foo is not defined and now this little bit here that the second half of this was the end comment when foo is defined it's now the beginning of a multi-line comment because sue isn't defined so now we're in a multi-line comment again and this hash elf that was useful before is now in a comment and we just have this comment and we end it I suspect I suspect I could probably take that away and the whole thing would keep working either way but it's kind of multi metric as it is so this will print so is not defying twice can anyone think of a similar feature we could use to demonstrate something kind of the same although it'll have a little bit more effect on the output what else might start consuming that hash elf not as a preprocessor hash elf Oh sort of elaborate so if I leave off the end here it's just going to say well that's certain unterminated string literal multi-line strings yes for rating string literals so this is largely similar here we have foo is defined so we're going to get a verbatim string literal that's the app there which says foo is defined and then the string contains hash else and it also contains string X equal F and then this double quote here is the end of the verbatim string literal so this will print so is define elf string equal that if we don't define foo or defined through two now this isn't the end of the previous string literal it's the start of a new verbatim string literal and now this else is consumed and now we've got through is not defined and this is one of those things that no it doesn't actually have any I can't pretend it could be useful in any way shape or form but it is quite fun I'll give another one of those that I have presented before so feedback welcome something I wasn't aware you could do before which was to use attributes on flight parameters has anyone ever seen and this is actually a use that maybe with a rosin analyzer which you know is to do you could have so sue is a generic type and it's type parameter the type parameter itself has an attribute of must be immutable and you can imagine writing a roles and analyzer saying everywhere I see a foo of tea or a foo or something I will validate that either that's a foo of tea in a generic method or type where the tea also has must be immutable or if it's a concrete type argument I'll check that it has the immutable attribute so here in in our usage code we've got bar is a class which is immutable Baz is a class which isn't and we would like this to be fine and that's not to be fine and sure we can't change the actual compiler rules to say c-sharp says it's wrong but we could fairly easily I well fairly easily and writing rosin analyzers is not easy but it's feasible and I I think it would be entirely feasible to write analyzer so that it would come up with an error here and that could be actually useful but I'm intrigued has anyone else seen the bit of syntax to be clear this is the syntax that is surprising to me anyone see you have right what was it useful ah right in core effects labs it's used for is primitive and so it could check endianness is that Alright okay so it could cope with networked house order or host the network order depending on the native Indian Asst cool if anyone sees any any uses in the wild beyond that please email me cuz I would love to collect them somewhere you could right so yes you could do it for enums or delegates instead for unconstrained melody type things yes yeah that would be quite a cool thing and intellisense won't help you there but yeah yeah yeah that'd be a nice way of doing things cool let me know if you find anything else let me just shut some windows down I think we're dealing with string interpolation next yes we are right so you'll use string interpolation in c-sharp six are you all aware that it uses the current culture by default so if you're logging using variant culture I'll come back to logging so lambda expressions the expression in an interpolated string literal has to be an expression okay so you can't do this this is meant to be well I'm opening the bit that's meant to be the interpolated part and then oh I'll just have a block here and do stuff in the block now that's not allowed that's not an expression lambda expressions are expressions but they don't have a type so you can't just write you know I can do lambda so I can't do console dot write line and then just goes through why yeah won't work okay that's not going to pass because the lambda expression has no type so it can't convert it to anything but you can cast the lambda expression to a delegate flight and then you can just use it on its own so if I let me just comment everything else out here the hideousness we'll see later so if we just have a longer expression thank you okay so this now compiles I believe and we've got the lambda expression we've got that in brackets because precedence and then we've got a cast I suppose I could do it probably Jo I don't think I've ever tried to get away from casting by just using as it's not going to work now I think I think you'd still need the brackets and even then okay as operator isn't as powerful as cough fine bum bum right okay so this is now converting it to a func of string and this is just going to print or am I on I'm on the wrong project straight interpolation on lambda expression okay so that's just printing oh it's a func of strings well once we've got a func of string we know how to invoke it so we can just add a couple of brackets we can let's make it a little bit more readable by calling in poke instead and so now when we evaluate the string as in when at the execution time it evaluates this argument it will create the delegates and then immediately invoke it which will print hello and then return why great we can then if we want to avoid the cast if you know you're doing this regularly in code for some bizarre reason and you can just write a method and in this case I have got it in the same class but you could write it somewhere else and import static there using static it so that you could use it everywhere and then you just need to pass in the lambda expression that will do exactly the same thing but you can if you can stomach the brackets you can do a whole program in our interpolated string literal so when we run this well let's just run it and then have a look at the code so ah what's your name John hello John so the order of execution here is it we have console dot write line starting with hello but before we can get into the console dot write line evaluation sorry invocation we have to evaluate this string and this string which is a verbatim interpolate sorry interpolated verbatim string literal is going to need to create this delegate and then invoke it and the delegate will print something else to the console then ask our name and then we're returning the name so that that can be appended on to hello and that's what ends up being called to console lot right line so I think you can see this can radically simplify your code yeah Oh yep Oh crikey I will let's noodle on that later on I think we can definitely come up with something nastier yeah but yes I think trying to do that now is going to fail but there are other things we can do with string interpolation which are fun so normally with regular string interpolation even if we use formattable string and this is the bit that you might learn something more how many of you have used the theme for Mashable string okay right brief diversion so the type of an interpolated string literal is string formattable string is a type introduced in net 4.6 and next standard one three there is no conversion from string the type to formattable string but there is an implicit conversion from an interpolated string literal expression to formattable string if that sounds crazy it isn't you've seen this before if I do var X equal ten the type of X is int the type of the integer literal there is int I cannot do bite y equals x that won't compile because there's no implicit conversion from int to bite but I can do bite y equals 10 because there is an implicit conversion from a constant expression of an integer type where the constant value is within the range of bite to bite okay so it's worth getting in your head that the difference between a conversion for a type to another type or a conversion from an expression to another types and you also get that with lambda expressions they have no type so there has to be an implicit conversion from a lambda expression to some other type or you could use them but it is a slightly subtle difference and dynamic has conversions one way but not the other it's really desert so the point of formattable string though the real usage of formattable strings is to be able to perform the string formatting in whatever culture you want that's the intended use so obviously we're not going to bother doing that we'll do more interesting stuff instead when you use a conversion from a string and interpolate string literal to form a full string it works out what it would do in terms of what's the format string that it's going to call string dot format with so that ends up being that here's a regular braces with numbers in and it evaluates value in this case and it does that once and create an instance of formattable string that's got the format string and the arguments as values so that's evaluated once and then when you call format small dots of string it says ok I will call string dot format yeah with whatever culture you specify if you're specifying one and pass in the values that I evaluated before and it does that evaluation once in the conversion so here unlike for example with link where if you have a link query imagine something like this but as a link query changing value if value were captured within your link crew changing the value of value would change the results of the query here if I run this regular evaluation it's going to print before twice and then the bit of code that's counting here well our formattable string is just Oh get date time net at UTC now that's evaluated once and then retained wouldn't it be nice if we could evaluate things kind of lazily instead at the points of formatting instead of at the point of constructing the formattable string well we can sort of with a bit of work so this is very similar code to what you saw before except for this chunk okay so fairly obviously this is a lambda expression and we have a method that accepts a thunk of object and returns whatever I call it captured capture so it remembers not well it is evaluating it once and it evaluates to a single delegate but the good thing is we've now got a delegate and we can invoke that delegate as many times as we like and in particular we can implement I formattable which means that when string dot format gets called will end up in here and at that point we can then just pull our delegate so we can evaluate it every time we're formatted we can re-evaluate the delegate and if we've been given if it returns a value which is also formattable then we can pass on the format string to that so for example in this date time now I specified that I just want to give the hour minute second and down to millisecond so if we run this code instead even though just to be really clear all we're doing is printing formattable ten times every time we print it there we go it's printing a different time let's take a formattable string one step further how many of you know about sequel injection attacks anyone who didn't put their hand up the first thing they do after this session all you're allowed to leave now is you right up there you go and read and OB tables calm or just search for sequel injection attacks and never ever write sequel that looks like this okay let's get rid of that for the moment but none of you sorry none of you would ever write code like that right it's clearly going to be broken well you know you might think that you need apostrophes around the name ideas and integers so that's fine but that's clearly going to break if if name has any apostrophes in this is the one that I'm saying you can use this if you want it is justifiably nice and but there are some downsides to think about but they're not sequel injection with this code so I assert that this is safe code we're not calling new sequel command we're calling new sequel command so we're not calling the sequel command constructor we're calling this extension method that I've created on sequel connection so far so good and that doesn't accept a string it accepts a formattable string but the interesting thing is what it does with that formattable string I'm going to sort of ruin the surprise and show you the results now so and when we run it I'm not going to talk to a natural database of course and instead I'm going to print the command text and then there are two parameters and I'll print their names and their values so parameter or a sequel okay does that look much more like something that you want to evaluate yeah if you're not using entity framework or whatever yes that's now a perfectly reasonable way of doing the query and the good news is you didn't have to write command dot parameters dot add p0 and make sure p0 was the same as you put in the secret itself you never put p0 anywhere in your code how many people are interested in the token p0 and want that to appear in their source code no one no it's it's just an artifact of I want to link this bit here to this parameter over here so in order to do this now you've seen the results how do we do it so we have our new sequel command method that takes a formattable string there are two bits in formattable string there's the format which live carefully document it will be this and sorry I didn't put the ml jar back in up here ok so yep as the format and then you can go get arguments which will return you an object array with the arguments in which in our case are going to be the name and the ID great so first bit is pretty simple we can take the Vettes take the arguments and convert just the arguments into sequel parameters wrapping those arguments and we'll use the overload of selects that accepts the position as well and we'll use that to name our parameters so we end up with p0 p1 p2 etc and we can specify the value there as well that leaves two things we need to get the sequel itself correct and we need to handle types so that you can specify what you want the sequel DB type to be well both of these are done with format capturing parameter so we're taking our sequel parameters so we've got p0 corey p110 and wrapping each of those in a format capturing parameter so it's a parameter that captures formats and then all we're doing then is using those as the actual arguments for string format so that clearly the interesting bit is going to be what happens when you format one of these format capturing parameters so format capturing parameter in turn remembers the sequel parameter it's wrapping and then it does two things when the string is called the first is so that we get the command text right it's just returning the name of the parameter so where we have the placeholder in the original interpolated string literal that's where we want the @p 0 yes great so that's what the final bit of code is but also as a side effect and you never ever ever make formatting a string have side effects but in this case it's kind of useful so we're going to see whether we've got a bit to the right of the colon that says how to format the value and we're saying that if you like the format of the first parameter is that it's an inverter so all we need to use is oenomaus and we'll say oh we don't care about casing so we could do int here and this will use M or let's use text instead I think and now if we run this again instead of saying M bar chart it will save the type is text is come up with sequel DV type text so out of interest I'll give you the downsides now so the upside is this looks cleaner ignore the diagnostic stuff this is all you need to do to end up with a sequel command that has all the parameters and you can see where the parameters are you won't get any of those cases where you've added the same parameter with two different names or you know you need to look back from the sequel to the parameter filling code you've got them all in the right place that's the good bit the bad bit is you hire a new developer who may be new to c-sharp they see this and they think that's the way that I'll do all new sequel command connection that and suddenly bang sequel injection attack or likewise you have you run some static tool analysis that says that looks awfully like a sequel string that you're using string interpolation for I'm going to complain at you so if you're not using the static tools and you're quite capable of explaining to every member of your team how this works I think this could be a win how many people do like this a few and how many really don't a smaller number really don't okay and the rest of you are sort of not entirely sure did everyone follow the explanation you should never ever use this code if you don't understand it okay definitely but this is you know potentially useful code I see I've only got ten minutes left I've gone through about maybe 15% of the code within this solution so go and have a look on github later let me see what we'll do are any of the rest really really fun compared with now let's do some link everyone like link yeah and I like link as well but the one thing I don't like about it is it's so verbose you have to write so much code to do anything you know take select for instance that's six characters I'm never going to get back from my life so I think we've got other ways that we can write link queries we just need to you know write the framework for them so that's there's an intro somewhere basic demo so I was looking at some operators I've got the evil method I might rename that to to link to operators at some point so it just takes any ienumerable and then lets us work with link to operators so here we have you know ampersand it's a bit like bitwise operands it's a bitwise evaluation it's always going to shrink things so it's very much like a filter that's going to do our where for us and then we think of link as a pipeline year we massage the shape of the pipe changes so the pipe operator is going to be our select so let's just look through what this is going to do so we start off with hello world how are you so I get a little tip and then we'll take away from that sequence the world so clearly that leaves hello how are you and then we'll concatenate today notice how already this is so much simpler than it would have been in them linked to object yeah then we'll filter it yeah you may not be terribly used to seeing lambda expressions as operands but hey it works you do need the brackets I think it won't compile them precedents again gets completely confused and so will convert it will filter out anything that's shorter than five characters so end up with hello how are you sorry hello today and then we'll convert each of those to uppercase and I'm no longer thinking about culture and flows and whatever so I don't care that it uses account culture so I will print that query three times yeah why not multiply a sequence by a number that's clearly just going to be hello today hello today hello today and the other example in this basic demo is XOR so XOR for two sequences is fairly obvious and it's just going to be things that are in one sequence or the other sequence but not both so this should print through bars crux so we should end up with hello today hello today hello today food as quacks basically alright so that's some of the operators and I will go into a couple of my favorites in a minute but do any of you have favorite operators dear me no favorite unicode character no favorite operator yeah I try to adopt the Mongolian Bell separator because they're they're running a scheme where you can pay 50 bucks or something to adopt character they don't let you adopt formatting characters it's most sad one day the Mongolian Bell separator will have a certificate on my wall so I've overloaded everything you can overload as I'll see sharp six I haven't yet tried to overload the is operator which I didn't know about some tell Kathleen tells me about it yesterday see or visit built it was even alright yeah so there's still got to go but I've overloaded everything else let's have a look at division and before I switch to the code when it might be a bit a little bit more obvious what do you think it means to divide a sequence by something sorry difference between them well that's more subtract is a different thing you could do with subtraction split okay what kind of split are we talking about give us an example right okay so take a collection of length 6 and divide it by 3 and you get collections each with either each with 3 or dividing it into three collections that's one way of doing it and and that's one thing I've implemented so that's the second part here where if we divide our sequence of each of the words I'm splitting at the end of this very long line and I'm calling split so we've got a bunch of words from Hamlet and then we're dividing them by 5 and I've chosen to make that mean divide them into groups of sequences of 5 and that nicely means that the remainder operator is clearly you know we won't give you a batch of less than 5 words and the remainder operator will give you whatever was left over after that division great and I'm more fond of dividing by a function so let's start off with our little company where people are in different departments and if I said to someone non-techie at all here's a bunch of people in the company could you divide them by Department they would naturally group them into you know well all the people from accounting over here all the people from sales over here so we can do exactly that so we can take the employees and divide them by a lambda expression that shows the department I'll just show you that division so too much stuff so we've got accounting has days sales has Bill and Betty etc and our dividing by an integer and the double commas here where there was a comma in the original text so we've got five words here to be or not to then be that is the question whether tis nobler in the etcetera and I think Shakespeare would be a lot duller if you read it always in sections of five words it wouldn't quite have the same thing and I'll show you my other favorite this is the the operator dedicated to Eric Lippert Eric made a foolish statement one time he said that the unary plus operator is kind of pointless it's only there for symmetry what other use could it possibly have it's there so that you can write plus five you know VAR x equals plus five and that still work because that's symmetric with minus five I say there are interesting other things you can do with it before I guess on to unary plus though what about unary minus what does it mean if I have a sequence what does - sequence mean just on its own not through - sequence just you know var new sequence equals - sequence what would you like it to do reversing someone said reversing okay anything else it could do lower case okay I haven't implemented that but I'll hear that as the equivalent of sort of negating element-wise which is if if you view negating a capital letter to a lowercase letter if you start with numbers instead you could have negative numbers when I asked on Twitter someone else suggested it could pop the sequence so the return value could be the first element of the sequence but in evaluating it you could also modify the sequence that you call this on and remove that first element again ever ever do that operators should not modify anything and however we can there's nothing to stop us from doing it so let's do it and I should show you the some of the code for this by the way is in operator enumerable it's mostly very simple so you know if we have plus we can use all our normal operators there are all the normal linked operators and I have everything's kind of dynamic but the implementation is a lot less sort of fun because it's fairly trivial it's a lot less fun than the things you do with it so we have three different things we could do with the unary minus operator and this is where the unary plus operator comes in I've implemented the unary plus operator as a sort of mode selector so if you just do - sequence I believe it reverses it yes so - sequence is the reversal sequence but then if we use plus sequence to mean the same sequence just with a different meaning for unary - so it goes on and it goes it cycles round so you can go from reverse to negate each individual item - popping mode which is the previous mode so if we print - sequence here we'll get the reverse so these first seven digits of pi so reverse will get to nine five one four one three if we do - plus sequence we'll end up with just minus three minus one minus four minus one minus 5 minus nine minus two and of course we can and combine these so - sequence we've seen is the reverse one starting with two plus - sequence is the reverse one starting with two but in a different mode and - + - sequence is the reverse time negated so it's minus 2 - 9 - 5 etc if we want to get into popping mode the plus + operator is clearly entirely different from + + that I can't remember what it does but it's it's bound to do something different so we have to have this this annoying space in there of course it's the Mongolian vowel separator was still being deemed a space I could put that in there so you would find it hard to tell the difference between that and plus plus Oh for those halcyon days and then if we print popping three times we end up with 3 then one then four and then if we print just popping instead of - popping sorry and it will print 1 5 9 - and then just to prove that it does indeed cycle if we do plus plus plus sequence and then negate the result will go back to just reversing so let's run that yeah where where that result is the same as the straight reversing and as I say I've got all the other operators there so the tilde operator looks like a bit of a wavy line to me who knows exactly what it's going to do so it shuffles the elements it just shuffles the sequence there are other things powers and all kinds of stuff please go and investigate on github and unless I've completely misread my timing I'm now out of time come and see me for any questions about details have a look on github I haven't written explanation for any of it but hopefully you can get a feeling of it and and I hope you've enjoyed it don't use any of it thank you [Applause]
Info
Channel: NDC Conferences
Views: 110,422
Rating: undefined out of 5
Keywords: .net, c#, jon skeet, ndc, ndc london
Id: JIlO_EebEQI
Channel Id: undefined
Length: 62min 16sec (3736 seconds)
Published: Thu Mar 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.