Have you failed a job interview?
Send us your job interview questions and get correct answers.

How can you declare a variable?

Experience Level: Junior
Tags: C#

Answer

Before you can use a variable you have to declare it first. The variable can be declared either implicitly using the 'var' keyword or explicitly specifying a data type the variable should have.

In the code below: The variable x is declared implicitly. Its data type wasn't explicitly specified, instead, in the place of the data type the keyword 'var' is used. Because the data type isn't explicitly specified and every variable needs to have its data type, the data type gets inferred from the value that is assigned to the variable.

The variable y is declared explicitly, which means its data type was specified before any value got assigned to the variable.

Example

var x = 10;

int y;
y = 20;

Comments

No Comments Yet.
Be the first to tell us what you think.
C# for beginners
C# for beginners

Are you learning C# ? Try our test we designed to help you progress faster.

Test yourself