C# and .NET Basics

Operators

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "Operators" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer discusses using operators to create binary statements that can compare values, change values, or perform mathematical operations. This lesson covers various operators such as addition, subtraction, multiplication, division, modulus, increment, decrement, relational, logical, and assignment operators.

Preview
Close

Transcript from the "Operators" Lesson

[00:00:00]
>> Spencer Schneidenbach: Let's switch on to operators. Just like in javascript, just like in pretty much every other language, there are things that you can use, operators that you can use to create what are called binary statements. And those binary statements can do a whole lot of things, they can compare values, they can change values, they can add values.

[00:00:18]
And you've got all of the usual suspects here, of course, we've already seen that for our add numbers method, we've already seen that. We know how to add integer here, a plus b. We also have a minus b if we wanna subtract multiplication and division, all handled by an asterisk and a slash respectively.

[00:00:38]
We also saw modulus, which is checking, the is basically doing, what is the remainder of two integers that are being divided? What is the remaining value? I've only really seen that used in context of checking to see if something is equal to is even, but that operator does exist if you need it.

[00:01:00]
Now here's an interesting one, there's the increment operator, which will, in place, increment the value of an integer or very or a number. So let's take, for example, this is a, this is kind of a weird behavior, so you have to bear with me. So if you have the ++ on the right side of the variable, it will increment that variable, but it's not gonna do it before it returns this.

[00:01:28]
So if you set b to the value of a++, it's going to set b to 5 and then a to 6, which is kinda goofy and a little bit counter intuitive. And the opposite is. Yes, the opposite is true, which is, if you put the ++ at the beginning of the a it will increment it before it does anything else, then return the value, and then they would have the same value.

[00:01:50]
So most of the time I see, if I'm seeing this, it's value equals zero, I just see value++, typically I see it standing alone. And the reason I see it standing alone is because I think that the other way is just a little bit confusing, but it's very common to just increment a number by 1.

[00:02:09]
The decrement operator is the same thing, but actually subtracting 1 from the value instead. So this particular web page, has a ton of examples of that you can look over. We've seen some of the relational operators, so we've seen double equals again, this is a strict double equal, so it will not work on variables of different types, it will not attempt type coercion.

[00:02:34]
It's very strict and safe like that, which I like, and the not equal to is exactly, the same way. So string name equals go mark now if value does not equal name, we won't even compile. It won't even it would even try to compare the values, and that's done very purposely.

[00:02:57]
There's some things that are surprises in some languages, but c sharp is very good about trying to minimize those surprises so it definitely respects the types that are established. Let's see greater than, less than greater than or equal to and less than or equal to, which, again, are usual suspects in the programming world.

[00:03:19]
So this is another binary operator where we say, if this value is greater than or equal to seven, we wanna run some code console that right line run this code. As you can imagine, this will not execute because this value of 0 is not greater than or equal to seven, and we can see that, and we just go.

[00:03:41]
Go, kill that run this boom, go. But if we invert this value,you'll see that the code does get run and that the statement is printed to the console.This returns a boolean, so that means that you can declare this as a boolean as well. Is less than or equal to seven, you can see that that declares a bool, is less than or equal to seven.

[00:04:13]
I'm gonna anticipate the question, do I really spell out my variables like this? Bet you're behind, I do, I very much, am a fan of explicit, easy to read, reads like English code. I don't like a lot of magic in my software, I don't like a lot of magic in my coding platform, and I don't like a lot of magic in general.

[00:04:32]
I like to be as explicit as I possibly can be. Let's see, yes, this one, I definitely want to cover the logical, the conditional, and the logical and conditional logical or logical, or I call them the double and or the single land. And I almost never use single land, so let's take a, let's declare this as a another bull and we say is equal to zero, and that's value is equal to zero.

[00:05:03]
So, as I mentioned, is less than or equal to seven, if we do is equal to zero. No matter what because this is an or this will basically return true if either side of this equation, or either side of the, this binary operation is equal to true. The important difference between that and the double or is that this will not run, this will not be checked if this executes and this returns true.

[00:05:34]
It's called short circuiting and it's a very important feature to understand. And this has implications, because the implications being that in addition to doing variables, you could absolutely have a method that's public bull, static bull is spencer around.
>> Spencer Schneidenbach: String, name, return name.equals spencer. This has implications because you might have an operation inside of this that does something to the database.

[00:06:14]
That does something that does some kind of operation, some kind of mutation, some kind of change to another system. There may be a very Important, there may be a situation where you don't wanna execute this code. There may be a situation where you don't wanna execute this code, but you wanna do an and comparison instead.

[00:06:31]
So in this case, if this is true or if this is false, excuse me, this will not execute because it says, well, both of those things have to be true. And if this is false, I know this statement is not false, whereas a single ampersand a single and will execute both sides of this code no matter what.

[00:06:50]
Again, I think this is really important to kind of understand, and because of what can happen in the real world in terms of when you call this method, it may do some kind of mutation. It may make a database call that you just don't want it or need it to make in this context, last operator that I will mention is the assignment operator.

[00:07:09]
We have seen that a bunch of times as we've declared variables, anything where we assign this is assign any. Anytime we see a single equals we should think assignment, and that's exactly what we get here, this is also an assignment operator. If we change this value to three, there are instances where you may want to do some additional assignment, maybe you want to do a plus equals or a minus equals.

[00:07:40]
And I don't think I've ever written a multiply or divide equals or a modulus equals, but those operations will essentially perform will translate. So if you do a plus equals b, it basically translates to assign the value of a to a plus b, and we can see that here is that if we value +=7, that if we cancel writeline(value).

[00:08:11]
We can see that it will write 10 to this, because what it's doing is taking seven and adding it to 3, so useful for addition, subtraction. Sometimes multiplication and division, but not statements that I write a ton of.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now