top of page

C# Tutorial For Beginners: Data Types and variables

Updated: Jan 19, 2022

A variable is the name given to a memory allocation that our programs can store and read data. Like other coding languages, each variable in C# has a unique type, which sets the boundaries and limitations of its allocated memory and the values that can be stored when declaring it in our program.

In C#, there is a range of variables we can use as part of our coding process, for example:

  • String – Stores text that is surrounded by double quotes “Text.”

  • Char – stores a single character surrounded by single quotes ‘char’.

  • Bool – Stores only two states of ‘true’ or ‘false.’

  • Int – keeps whole numbers, such as -10 or 10.

  • Double – stores numbers with decimals such as -10.7 or 10.7.

General rules for names for variables

  1. You cannot use reserved words like (char or string) as names.

  2. A Name of a variable must start with a letter.

  3. Variables names are case sensitive (“MyName” and “Myname” are different variables).

  4. Names can contain digits, underscore (_), and letters.

  5. Names should start with a lowercase letter.

  6. A variable name cannot contain whitespaces.

Declaring Variables

To create a variable, you must first specify the type and assign it a value (or initialize it in other parts of the code). Variables can be declared only in specific places in code, such as classes and methods, etc.

And here are some additional examples of other variables:

20 views0 comments
bottom of page