TrueCoders Logo
Seats Left - Registration Ends Dec 12th

C# Coding Bootcamp VariablesApril 11th, 2024

C# Coding Bootcamp Variables

Watch our C# Variable Lecture Video from our C# Coding Bootcamp: C# Coding Bootcamp Variables

And if you're interested in learning more about C#. Check out our Software Engineering Bootcamp to gain all the benefits of learning in a C# Coding Bootcamp with fellow students and unlimited 1 on 1 Instructor Support

Why:

At the end of the 16th century, François Viète introduced the idea of representing known and unknown numbers by letters, nowadays called variables, and of computing with them as if they were numbers, in order to obtain the result by a simple replacement. In reference to programming, values being used in the program are able to be remembered more easily.

Essentially, variables play an important role as they allow us as programmers to write flexible programs. Instead of entering data directly into a program, a programmer can use variables to represent the data.

A variable is a container that stores some value.

Note: The opposite of a variable is a constant. Constants are values that never change.

C# Coding Bootcamp

Check out our Software Engineering Bootcamp to gain all the benefits of learning in a C# Coding Bootcamp with fellow students and unlimited 1 on 1 Instructor Support

What:

Variables keep track of the data throughout the program. It is like a container to store the information. It is used to store, retrieve, and modify changeable data.

Variables consist of a data-type, a variable name and a value (initialized by providing a value):

SYNTAX:

datatype variableName; // Declaration

variableName = value; // Initialization

datatype variableName = value; // Declaration & Initialization Syntax

Variables are always assigned with a data-type, meaning it holds the value of a specific type, such as string, bool, int and so on. C# is strongly-typed meaning once a variable has a type, that type cannot change. C# is also statically-typed meaning every variable must have a type.

C# Variable Naming Rules/Conventions:

You can’t just choose any sequence of characters as a variable name. Instead, C# has some rules regarding variable names that are to be followed:

  • camelCase for local variables, such as cost, firstName, dateOfBirth, and petName.
  • A meaningful or descriptive name that is neither too long nor too short to identify the information stored in a variable just by looking at it
  • Can contain the letters a–z and A–Z, numbers 0–9, and the underscore (_) character..other symbols are not allowed.
  • Cannot have spaces
  • and cannot start with a number.
  • Cannot use a word reserved by C# language—keywords such as namespace, class, using, and so on...

Invalid Names:

  • ~Valid$PascalName! (symbols)
  • 1 (starts with a number)
  • class, while, if, protected (keyword)
  • 1order, 1name (starts with a number)
  • -validName (starts with a dash)

How:

Declaring a variable

Create a variable by declaring its type and then giving it a name using the syntax below.

When you declare a variable, the computer knows that it has to reserve a place in its memory for this variable.

// dataType variableName; 

int y; 

Assigning a value to a variable

To initialize a variable, you need to assign it a value. This is done by naming the variable followed by an equal sign (=) and then the value.

// variableName = value; 

y = -20; 

Note: The term initialize means to assign an initial value.

Variable Declaration and Initialization Syntax Combined on a Single Line

// dataType variableName = variableValue; 

int x = 10;

Here, the datatype must be one of the valid data types, and variableName is the identifier used for the variable. This is an explicitly typed local variable where the type is explicitly defined

  • string name;
  • int age;
  • int weight;
  • bool isMarried;

C# Coding Bootcamp:

C# Coding Bootcamp

Check out our Software Engineering Bootcamp to gain all the benefits of learning in a C# Coding Bootcamp with fellow students and unlimited 1 on 1 Instructor Support

Ready to Change Your Life?

Let TrueCoders Help You Learn to Code

Join TrueCoders today and discover a coding program that not only equips you with essential skills but actively supports you in launching your career. Don't wait until the end of the online coding courses to start your job search – let us help you succeed by empowering you from day one.


Sign up today for our software engineering bootcamp or our web development bootcamp