C#
C# Interface Tutorial with Example
What is an Interface Class? Interfaces are used along with classes to define what is known as a...
A variable is a name given to a storage area that is used to store values of various data types. Each variable in C# needs to have a specific type, which determines the size and layout of the variable's memory.
For example, a variable can be of the type String, which means that it will be used to store a string value. Based on the data type, specific operations can be carried out on the variable.
For instance, if we had an Integer variable, then operations such as addition and subtraction can be carried out on the variable. One can declare multiple variables in a program.
Let's look at a quick example of the declaration of multiple variables of different data types.
In our example, we will define two variables, one of the type 'string' and the other of the type 'Integer'. We will then display the values of these variables to the console. For each example, we will modify just the main function in our Program.cs file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
String message="The value is ";
Int32 val=30;
Console.Write(message+val);
Console.ReadKey();
}
}
}If the above code is entered properly and the program is executed successfully, the following output will be displayed.
From the output, you can see that the values of both the string and integer variable are displayed to the console.
Operators are used to performing operations on values of various data types. For example, to perform the addition of 2 numbers, the + operator is used.
Let's see the table of operators available for the various data types
These are operators used for performing mathematic operations on numbers. Below is the list of operators available in C#.
| Operator | Description |
| + | Adds two operands |
| - | Subtracts the second operand from the first |
| * | Multiplies both operands |
| / | Divides the numerator by de-numerator |
| % | Modulus Operator and a remainder of after an integer division |
| ++ | Increment operator increases integer value by one |
| -- | Decrement operator decreases integer value by one |
These are operators used for performing Relational operations on numbers. Below is the list of relational operators available in C#.
| Operator | Description |
| == | Checks if the values of two operands are equal or not, if yes then condition becomes true. |
| != | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. |
| > | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. |
| < | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. |
| >= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. |
| <= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. |
These are operators used for performing Logical operations on values. Below is the list of operators available in C#.
| Operator | Description |
| && | This is the Logical AND operator. If both the operands are true, then condition becomes true. |
| || | This is the Logical OR operator. If any of the operands are true, then condition becomes true. |
| ! | This is the Logical NOT operator. |
Let's look at a quick example of how the operators can be used in .Net.
In our example, we will define 2 Integer variables and one Boolean variable. We will then perform the following operations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Int32 val1 = 10,val2 = 20;
bool status = true;
Console.WriteLine(val1 + val2);
Console.WriteLine(val1 < val2);
Console.WriteLine(!(status));
Console.ReadKey();
}
}
}If the above code is entered properly and the program is executed successfully, the output will be displayed.
What is an Interface Class? Interfaces are used along with classes to define what is known as a...
What is Hashtable in C#? A hash table is a special collection that is used to store key-value...
In this tutorial, you will learn- .Net Framework Version History C# Version History .Net Framework...
So far we have seen how to work with C# to create console based applications. But in a real-life...
What is an Arrays in C#? An array is used to store a collection or series of elements. These...
C# is based on the C++ programming language. Hence, the C# programming language has in-built...