Working with variables, operators, and expression in C#

In this article, you will be able to:

  • Understand statements, identifiers, and keywords
  • Use variables to store information
  • C# primitive data types
  • Use arithmetic operators such as the plus (+) and the minus (-)
  • Increment and decrement variables

Understanding statements in C#

A statement is a command that performs an action, such as calculating a value and storing the result, or displaying a message to a user. We combine statements to create methods. We will learn more about methods in next article "Writing methods and applying scope". but for now, we can think of a method as a sequence of statements. Main, which was introduced in the previous article.

In C-Sharp statements follow a well-defined set of rules describing format and construction. These rules are collectively know as syntax. 

What is Identifiers in C#?
C# has 77 identifiers for it's own use, and we cannot reuse these identifiers for our won purpose. These identifiers are called keywords, and each has a particular meaning. For example of keywords are class, namespace, and using.

The following is the list of keywords


C# also uses the identifiers that follow. These identifiers are not reserved by C#, which means that you can use these names as identifiers for our own methods, variables, and classes, but we should avoid doing so if at all possible. 









Using variables:

A variable is a storage location that holds a value. We can think of a variable as a box in the computer's memory that holds temporary information.

Naming variables

We should adopt a naming convention for a variables that helps you to avoid confusion concerning the variables you have defined. This is especially important we are part of a project team with several developers working on different parts of an application.

The following list contains some general recommendations:
  • Don't start an identifier with an underscore. Although this is legal in C#, it can limit the interoperability of our code with applications built by using other languages, such as Microsoft Visual Basic (VB).
  • Don't create identifiers that differ only by case. For example, do not create one variable named myVariable and another named MyVariable for use at the same time, because it is too easy to get them confused.
  • Start the variable name with a lowercase letter.
  • In a multi word identifier, start the second and each subsequent word with an uppercase letter (This is called camelCase notation). For example, FirstName, LastName etc...

Declaring variables

Variable hold values. C# has many different types of values. For example, numeric, character, decimal, etc.
We can declare the type and name of a variable in a declaration statement.

For example-

int age = 29;


Note: int is a data type age is variable name and 29 is assigned value in that variable. The equal sign ( = ) is the assignment operator. 

Working with primitive data types

The following table lists the most commonly used C# built-in types that called primitive data types. 




Unassigned local variables

C# doesn't allow to use an unassigned variable. We must assign a value to a variable before we can use it; otherwise, program will not compile. 

Example: 
int age;
Console.WriteLine(age);     //compile-time error


Displaying primitive data types in C# Console App

  • Open Visual Studio 2019
  • Click on Create a new project



  • Select C#, Windows, Console
  • and select Console App (.NET Framework)
  • Then click on Next




  • Type Project name
  • Select location (where you want to save the project)
  • Then click on Create




  • Now declare primitive data type in variable
  • And run the project by pressing F5 or click on Start button from the menu bar



Using arithmetic operators (+, -, *, /)

int val;val = 10*10;  //result 100


Operators and types 

Not all operators are applicable to all data types. The operators that we can use on a value depend on the value's type. 



Perform calculation in the MathsOperators 





Controlling precedence


Incrementing and decrementing variables

The ++ and -- operator are unary operators, meaning that they take only a single operand. 

Prefix and postfix

Increment (++)  and decrement (--) operators are unusual in that we can place them either before or after the variable. Placing the operator symbol before the variable is called the prefix and after called postfix

Example: 
count++;  // postfix increment
++count;  // prefix increment
count--;   // post decrement
--count ;  // prefix decrement

count++ is the value of count before the increment takes place, whereas the value returned by ++count is the value of count after the increment takes place.






Comments