Click here to download the sample source code for this if statement lab exercise If you forgot how to open the files using Visual Web Developer, click here to learn How to open C# ASP.NET Website files. Please use these files only as a reference. You should go through the exercises yourself and create your own project according to the instructions in the exercise.
C# programs contain different types of or symbols that perform operations on variables of various types. Some operators can perform mathematical operations such as addition or subtraction, whereas other operators can take two string variables and combine them into one. One operator that we have seen in use as we experimented with data types is the assignment operator =. The assignment operator is the most basic and widely used operator in the C# language. When used in a C# statement, the assignment operator takes whatever expression is on the right hand side of the = symbol, evaluates it, and then puts the resulting value into the variable on the left hand side of the =. Let’s take a look at some more commonly used operators.
Create a new website using Visual Web Developer and call the project Lab1_ext. Go to the Design view for the file Default.aspx. Drag a new Button onto the form and change the Text property to say “Addition operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
int i = 30; int j = 10; //Addition operator int result = i + j; TextBox1.Text = result.ToString();
Drag another Button onto the form and change the Text property to say “Subtraction operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
int i = 30; int j = 10; //Subtraction operator int result = i - j; TextBox2.Text = result.ToString();
Drag another Button onto the form and change the Text property to say “Concatenation operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
string s1 = "Hello student,"; string s2 = " how are you?"; //string Concatenation operator string result = s1 + s2; TextBox3.Text = result;
Drag another Button onto the form and change the Text property to say “Increment operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
int i = 50; //Increment operator i++; TextBox4.Text = i.ToString();
Drag another Button onto the form and change the Text property to say “Decrement operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
int i = 50; //Decrement operator i--; TextBox5.Text = i.ToString();
Drag another Button onto the form and change the Text property to say “Addition assignment operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
int i = 50; //Addition assignment operator i += 5; TextBox6.Text = i.ToString();
Drag another Button onto the form and change the Text property to say ”Concatenation assignment operator”. Drag a new TextBox to the right of the Button control. Double click the Button to create the event handler method and put the following code for the button:
string s = "Hi "; //Concatenation assignment operator s += " there!"; TextBox7.Text = s;
Run the project using the green play button at the top of the IDE. Click on the various buttons and see their outputs on the web page. Notice that some of the operators such as the + symbol act as different types of operators depending on the data types that are used in the expression. If the + symbol determines that a string type is being used in the expression, the C# language will perform a string concatenation or combination of two strings together. Whereas, when numeric types are being used, the C# language will perform mathematical addition operations. You should also note that the different operators accept variables instead of hard coded values so the code example above for the addition assignment operator could have been alternatively written as:
int i = 50; int j = 5; //Addition assignment operator i += j; TextBox6.Text = i.ToString();
Notice that a variable j is used instead of the hard coded value of 5. This is important to understand because frequently in your coding, you will encounter situations where you don’t know the values of variables ahead of time so you will need to perform these types of dynamic expression operations. Spend some time looking over other types of C# operators as well on the and experiment with those that you find interesting.