Lesson Weekend

Declaring variables in C# looks slightly different from declaring variables in Javascript. Let's walk through this process in more detail.

Variables in C

Defining a C# Variable

To create a C# variable, we need three things:

  1. A descriptive name so it's easy to tell what the variable represents. Our variables should always have descriptive names, regardless of the language we use.

  2. A data type such as string or int to declare what type of data the variable holds. We didn't need to do this in JavaScript because it is weakly typed. However, because C# is strongly typed, we always need to declare the data type of a variable.

  3. An initial value should be set with the assignment operator (the = symbol).

String Variables

Here's an example of a string stored inside of a variable:

> string phrase = "Hello World";

In the example above:

  • string is the data type our variable will be;

  • phrase is our descriptive name;

  • "Hello World" is the initial value.

After creating this variable, we can retrieve its value by calling phrase:

> phrase
"Hello World"

We can also perform actions upon this variable. We could use the concatenation operator on phrase to add another string to it:

> phrase + "!!!!"
"Hello World!!!!"

Strongly-Typed Languages

Remember, we must always define what type of data a variable will hold. We say string phrase = "Hello World"; not phrase = "Hello World";

Similarly, we could define a variable containing an integer like this:

> int number = 1;
> number

Again, C# variables must declare the type of data they contain. This is because C# is a strongly typed language, meaning it rigidly insists on consistent and explicit data types. In this case, int is the data type for integers.

Because variables behave exactly like the values they store, we can use any number of arithmetic operators with variables containing integers:

> number + number

> int bigNumber = 100;
> bigNumber

> number + bigNumber

Other Data Types

Other common data types include:

  • float
  • bool for the boolean values true or false
  • decimal
  • double
  • object

Don't worry about these yet. We'll explore these data types in upcoming lessons.

Naming Conventions

Before we wrap up, let's address naming conventions. C# variable names should always be written in lower camel case just like JavaScript. The first letter of the first word is lowercase, there are no spaces between words, and the first letter of any subsequent words are capitalized. It looks like this: myVariable, myMultipleWordVariableName, myEvenLongerMultiWordVariableName, etc.


  • Assignment Operator: The = operator is used to set the initial value of a variable:
> string exampleVariable = "hey, I'm a variable!";`
  • Lower Camel Case: C# uses lower camel case for variables just like JavaScript. Here are a few examples: likeThis, or evenLikeThisExampleHere.

  • Strongly-Typed Language: A language in which data types must be declared. In the following example, we state that our variable has the string data type:

> string anotherExampleVariable = "hey, I'm a variable too!";


  • C# variables must declare the type of data they contain because C# is a strongly typed language.

  • To create a C# variable we need three things:

  1. A descriptive name.
  2. A data type to declare what data the variable holds such as int or string.
  3. An initial value set with the assignment operator (=).


> string phrase = "Hello World";
  • string is the data type our variable will be.
  • phrase is our descriptive name.
  • "Hello World" is the initial value.

Lesson 4 of 5
Last updated more than 3 months ago.