From the course: LINQPad Essential Training

Explore the expression editor - LINQ Tutorial

From the course: LINQPad Essential Training

Explore the expression editor

- [Instructor] LINQpad is designed as a tool to quickly experiment with code ideas, run and test those ideas and show the results of the experiment in the results window. LINQpad does its best to make this simple so that we can construct our code without a lot of ceremony, we can test a one-line expression or we can build a class with many methods. And we can reference any .NET library, including our own libraries. We'll examine some of these concepts in this chapter. What we'll look at in this video is the expression editor. To change the kind of editor use this drop down. These are arranged by languages, C sharp at the top, then visual basic sequel and F sharp. I'm working with C sharp for the code examples. I'll focus on these top three items. Each one of these editor choices, expression, statements, and program impacts the way LINQpad parses, compiles, and runs the code, which means we should understand the differences between them. You'll find the examples for this video in the number two folder. All you need to do is select an item in this my Cory's list, and it'll load it into the editor. Now we are working with expressions and you see that that's true by looking at the editor at the top, in this dropdown, we're not using statements or programs we're using expression. What do we know about C sharp expressions? They are items that revealed a value. So one of the most simplest type of expression you can use in a C sharp is a literal value. So I've got a string literal in this example on line 10, what LINQpad will do is it'll look through this editor and see if it can find any valid expression. If it finds the expression, then it will evaluate it and output the results. And you output the results by clicking on this execute button that brings up the results window. And you see that this literal value of hello was I put is hello. Now this results window can be dismissed by clicking on this X button then the next time you execute the query, it will bring that up. Or if you leave it open, the next time you execute the query, it'll clear out the results and put the new values in here. You see on line 11, I have another literal value this time it's a decimal value. So I want to swap I want to comment out hello and uncomment this decimal value. We can use the same keystrokes that you used in visual studio, which has Control + K Control + U for uncomment. So let's try that and Control + K Control + C to comment. Now I'll reevaluate the expression and I get the literal value of 54. Let's uncomment this line, notice up here it says the expression editor can only evaluate one expressions. So there's two expressions here. So if I attempt to execute this query, I'll get an error. And it's telling me that there's a problem with this on line 10 it says there's a syntax error that I need a comma, or I could end the line of code with a semi-colon. It's not saying that, but that's, you know, that's one of the things you can do to fix this. If I put that in there and attempt to run it, it says that I should be using the C sharp statements for state and based crisis. Basically telling me I'm using the wrong editor. I should be using this editor. And we'll talk about that line in the next video. So let's take a look at a couple other expressions let's choose this item here. So here is an operator, an operator is considered an expression here I've got the greater than operator. That's a binary operator and it's going to return true or false returns to bullying so I run this, I get back the value of true here I've got the concatenation operator, so I'll do a Control + K Control. You'd uncomment that comment out this line, and let's see if we can get a concatenation result in the results window we do. And then finally, a method call is an expression. So here's some examples. For example, on line 65, I'm calling the two string method, and then I'm asking for the results of that two string method call to be returned as currency like this, you can experiment with the other items in here. Let's just look at the last one uncomment this. What I'm doing here is calling the math class. I'm calling two methods on the math class, square root and abs absolute, and we'll run that and get to our results. That's a look at the expression editor. One nice use case for using it is to paste in expressions when learning new.net framework ideas.

Contents