C# Comparison Operators

Comparison Operators

Comparison operators are used to compare two values (or variables). This is important in programming, because it helps us to find answers and make decisions.

The return value of a comparison is either True or False. These values are known as Boolean values, and you will learn more about them in the Booleans and If..Else article. Continue reading C# Comparison Operators

C# Assignment Operators

Assignment Operators

Assignment operators are used to assign values to variables.

In the example below, we use the assignment operator (=) to assign the value 10 to a variable called x:

Example

int x = 10;

Continue reading C# Assignment Operators

C# Operators

Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

int x = 100 + 50;

 

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable: Continue reading C# Operators

C# User Input

Get User Input

You have already learned that Console.WriteLine() is used to output (print) values. Now we will use
Console.ReadLine()
to get user input.

In the following example, the user can input his or hers username, which is stored in the variable userName. Then we print the value of
userName
:

Example

// Type your username and press enter
Console.WriteLine("Enter username:");

// Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();

// Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);

Continue reading C# User Input

C# Type Casting

C# Type Casting

Type casting is when you assign a value of one data type to another type.

In C#, there are two types of casting:

  • Implicit Casting (automatically) – converting a smaller type to a larger type size
    char -> int -> long -> float -> double
  • Explicit Casting (manually) – converting a larger type to a smaller size type
    double -> float -> long -> int -> char

Continue reading C# Type Casting

C# Data Types

C# Data Types

As explained in the variables chapter, a variable in C# must be a specified data type:

Example

int myNum = 5;               // Integer (whole number)
double myDoubleNum = 5.99D;  // Floating point number
char myLetter = 'D';         // Character
bool myBool = true;          // Boolean
string myText = "Hello";     // String

 

Continue reading C# Data Types

C# Identifiers

C# Identifiers

All C# variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).

Note: It is recommended to use descriptive names in order to create understandable and maintainable code: Continue reading C# Identifiers

C# Multiple Variables

Declare Many Variables

To declare more than one variable of the same type, use a comma-separated list:

Example

int x = 5, y = 6, z = 50;
Console.WriteLine(x + y + z);

 

Continue reading C# Multiple Variables

C# Display Variables

Display Variables

The WriteLine() method is often used to display variable values to the console window.

To combine both text and a variable, use the + character:

Example

string name = "John"; 
Console.WriteLine("Hello " + name);

You can also use the + character to add a variable to another variable : Continue reading C# Display Variables

C# Constants

Constants

If you don’t want others (or yourself) to overwrite existing values, you can add the const keyword in front of the variable type.

This will declare the variable as “constant”, which means unchangeable and read-only:

Example

const int myNum = 15;
myNum = 20; // error

Continue reading C# Constants