C# Picking Apart Expression Trees - ConstExpression

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so I feel like I've just kind of thrown you in the pool of expression trees this is kind of a scary and kind of big and then all of a sudden we're doing this left right no type right thing and yeah it yeah let's let's take all this and just back up for a minute I'm gonna just get it all off the screen open up notepad and just paste it there for my reference I wanna I want to start out purely basic and go from there let's first of all get our funk back our int bool I'm gonna call it I think I called it test all right I agree than 5 and I'm just gonna comment this line out and I want to build this up one by one into an expression tree just like the compiler did when I made this thing an expression first things to notice is we have this 5 5 is constant okay as far as I'm concerned this symbol in my life will represent 5 1 2 3 4 5 5 elements 5 things I can touch 5 dollars of its dollars it has some meaning to me and it will not change that's it is constant to me so I'm gonna come down here and say expression control dot get the using back in their expression dot constant alright and this is a simple static method takes some parameters ok the value object value well it's gonna be a 5 and then we can go as far as passing in the type I know that it's going to be an int so I will say type of int like so and let me put my cursor on constant control ki to bring this little pop-up here and it says creates a system that link expression constant expression you notice the return type here is constant expression so let me go as far as saying constant expression Const expression gets expression not constant alright this constant static method is a factory method you can go look at the factory design pattern if you want but it's kind of a long way of just saying new constant expression but instead of saying new constant expression we must say expression doc student all right that's going to create an object right out on the heap but it's not necessarily round but I drew it round let's let's see what's inside of this Constance pression Const expression dot can reduce let's get into that later no time let's look at the know type you're console.writeline Const expression not node type and control v this is just looking whew constant go figure the node type is constant what is this know type anyway click on that f12 and know type is right here it's an expression type I'd have 12 on that and oh look at that it's an enum and it is a rather long enum but if you'll notice pretty much all the stuff you see here should look pretty straightforward box try throw switch bubble all he smokes is true type equal post-increment this is getting a little bit into compiler theory if you've written a compiler this should look familiar to you if not don't stress it this is this is all the types of expressions that we can have in c-sharp and one of those values is constant hence the sorry let's go back here the node type of our constant expression will be constant let me just draw our constant expression this thing is referencing this thing out on the heap the node type I'll say node type here is constant all right very good that's one thing we've discovered about this object we created with expression not constant let's go a little further and see what else we can print out here Const expression dot let's go down here and see what else is cool there's type and there's value I want to look at the type here do you have any idea what the type of our constant expression just might be I actually passed it in right here remember int is shorthand for system dot in 32 so our cost in expression this five returns an int 32 let's look at something else here console dot right actually we should we should probably put that in here as well as our object here typ e is I'm gonna cheat here system dot that's that's huge let's just say int32 I don't know maybe I should change the font on that alright console.writeline get that away console.writeline Const expression dot hey what's this value gets the value of the constant expression right there okay let's try that one out ctrl f5 what do you think the value of this constant expression will be hopefully you guessed it it's a five that is the value of our constant expression is literally a five all right so I'm going to go out here on the heap and for value I will say the value is a five alright so don't make this harder than it really is the compiler saw our five here all right when we said expression like this we said expression I better do this properly expression like so and we can even uncomment it this it doesn't really matter anymore but the compiler sees this five here and says oh okay that's a constant it's an int so I'll just say expression constant five of int and then at runtime when this this method executes so it will create a constant expression object the same as I drew out here and it'll said it's no time to constant because it is a constant expression and then the the type of a constant expression while this returns a five or at an int it returns an int the value however is a five so all that information is stored right here in this object that is sitting out on the heap and I can certainly write out those properties when I say no type it'll grab the node type value of that enum when I say cost expression type it'll give me the type that the cost expression returns when I say cost expression off value it actually gives me the value let me control f5 that and just show you all that again constant is the node type in 32 is the type of the expression and five is the actual value returned by that expression so there you go I've spent seven minutes picking apart just one piece the the whole entire lambda expression here the next videos I'm going to continue with that but also going to talk about expressions if you've written a compiler you should be comfortable with expressions if you need a review I'm going to give it to you if you don't understand and really know what expressions are that's fine I'm gonna start at your level okay we'll talk about that in the upcoming videos
Info
Channel: Jamie King
Views: 14,906
Rating: 4.974359 out of 5
Keywords: C# (Programming Language), Lambda Expressions, Anonymous Function, Expression Trees, Language Integrated Query (Programming Language)
Id: M7uIRA1k_vc
Channel Id: undefined
Length: 7min 3sec (423 seconds)
Published: Tue Jul 02 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.