Working with Variables, operators, and expressions

CSharp variables

Topics: 
  • Understand statements, identifiers, and keywords.
  • Use variables to store information.
  • Work with primitive data types.
  • Use arithmetic operators such as the plus sign (+) and the minus sign (–).
  • Increment and decrement variables.

Understanding statements

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.

Statements in C# follow a well-defined set of rules describing their format and construction. These rules are collectively known as syntax. (In contrast, the specification of what statements do is collectively known as semantics.) One of the simplest and most important C# syntax rules states that you must terminate all statements with a semicolon. 

For example: Console.WriteLine("Hello, World!");

Using identifiers

Identifiers are the names that we use to identify the elements in our programs, such as namespaces, classes, methods, and variables. (We will learn about variables shortly.) In C#, we must adhere to the following syntax rules when choosing identifiers:
You can use only letters (uppercase and lowercase), digits, and underscore characters.
An identifier must start with a letter or an underscore.

For example, result, _score, footballTeam, and plan9 are all valid identifiers, whereas result%, footballTeam$, and 9plan are not.

 C-Sharp (C#) Identifiers 


Using variables

A variable is a storage location that holds a value. You can think of a variable as a box in the computer’s memory that holds temporary information. You must give each variable in a program an unambiguous name that uniquely identifies it in the context in which it is used.  

Naming variables

We should adopt a naming convention for variables that helps us to avoid confusion concerning the variables we have defined. This is especially important if we are part of a project team with several developers working on different parts of an application; a consistent naming convention helps to avoid confusion and can reduce the scope for bugs.

               - 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 your code with applications built by using other languages, such as Microsoft Visual Basic.
  • 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. Also, defining identifiers that differ only by case can limit the ability to reuse classes in applications developed by using other languages that are not case sensitive, such as Visual Basic.
  • Start the name with a lowercase letter.
  • In a multiword identifier, start the second and each subsequent word with an uppercase letter.  (This is called camelCase notation.)
  • Don’t use Hungarian notation. (If you are a Microsoft Visual C++ developer, you are probably familiar with Hungarian notation. If you don’t know what Hungarian notation is, don’t worry about it!)

For example, score, footballTeam, _score, and FootballTeam are all valid variable names, but only the first two are recommended.

Declaring variables

Variables hold values. C# has many different types of values that it can store and process—integers, floating-point numbers, and strings of characters, to name three. When you declare a variable, you must specify the type of data it will hold.

We declare the type and name of a variable in a declaration statement. For example, the statement that follows declares that the variable named age holds int (integer) values. As always, we must terminate the statement with a semicolon.

 C# Primitive (Built-In) Data Types 

C# has several built-in types called primitive data types. The following table lists the most used primitive data types in C# and the range of values that you can store in each.

Using arithmetic operators

C# supports the regular arithmetic operations you learned in your childhood: the plus sign (+) for addition, the minus sign (–) for subtraction, the asterisk (*) for multiplication, and the forward slash (/) for division. The symbols +, –, *, and / are called operators because they “operate” on values to create new values.



Comments