Lesson 2.1

C# Variables - defining and naming rules


Objective

What is Variables?
How to define variables in c sharp?
How to store value in c#?
Variable Naming Rules

What is Variables in C#

A variable is an area in memory which keeps values assign by program. For example, you are writing a program in which you ask a number from the user. You need a container to store values given by user. This container is called a variable.

"A variable is a storage location in memory that holds value and that value available for program during execution. A variable must have a name."

How to define Variables?

There are some rules which need to be followed while choosing variable names. However, you may choose any valid name for variables but a good and suitable variable name makes your program more readable and understandable.

Rules

Follow these 7 rules while choosing a variable name.

1. Variable name must begin with a letter or underscore (_).
2. A variable name may contain number after first letter of alphabet.
3. No space is allowed between variable names.
4. A variable name may be any length but you should keep it under 20 or 30 characters.
5. You cannot use reserved keyword for variable name.
6. C# variable name is case sensitive.
7. Use meaningful name.

For example, we are writing a program for calculating sold pizza. There may be different types of pizzas are there such as small pizza, large pizza, medium pizza etc. variable name should be like this:

String Variables
StrSmall_Pizza
strLarge_Pizza
strMedium_Pizza

Int Variables
intSmall_Pizza
intLarge_Pizza
intMedium_Pizza

We made a pattern while naming variables because it gives as better understand about which type of variable is. For example str denotes string variable where int denote integer variables.

How to define variable names?

It is very simple to define variable names in c#.

<type> <name>

Example:

String strUserName
Int intTotal
Double douAmount
Float floTax

All the above examples contain two sections. First section is datatype which defines which type of variable is and another section is a variable name.

Programming Examples

In this example, we will create variables of different data type, store value in them and show output.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VariableName
{
    class Program
    {
        static void Main(string[] args)
        {

            //Defining Variable Names.
            string strName;int intAge;           

            //Asking user name and storing it to strName Variable.
            Console.Write("Enter Your Name: ");
            strName = Convert.ToString(Console.ReadLine());

            //Asking UserAge and storing it to intAge Variable.
            Console.Write("Enter a number: ");
            intAge = Convert.ToInt32(Console.ReadLine());

            //Printing Name and Age on Screen.
            Console.WriteLine("Your Name is {0} and you are {1} years old", strName, intAge);
            Console.ReadLine();
        }
    }
}

		  
Output

Enter Your Name: Simules Ferry
Enter a number: 32
Your Name is Simules Ferry and you are 32 years old
__

Explanation

1. In this program, we have defined 2 variables strName and intAge. These variables are defined for storing values entered by the user at runtime. strName will store a string value and intAge will store a integer or numeric value.

  //Defining Variable Names.
  
  string strName;
  int intAge; 
		   

2. Printing message on screen "Enter Your Name:" using Console.Write() or Console.WriteLine() Method.

 Console.Write("Enter Your Name: ");
		   

3. Store input in strName Variable. But before storing we have converted input value in string using Convert.ToString() method.

 strName = Convert.ToString(Console.ReadLine());
		   

4. Displaying Result using Place Holder {0}. A place holder contains variables values. You can define many place holders to print variable values. For example, if you have 3 variable and want to display their values on screen you can define 3 place holder for all variables.

String s1, s2, s3
So you can define place holder as {0} for s1, {1} for s2 and {2} for s3 variables.

Console.WriteLine("Your Name is {0} and you are {1} years old", strName, intAge);           
		   

5. At the end, we write Console.ReadLine() method to hold output screen. If you don’t write this method then the program will execute and output screen gone within a second.

 Console.ReadLine();
		   

Summary

In this chapter, you have learned all about variables. You have learned what is variable in C#, How to define them, how to store value and how to use variables in program. In the next chapter, we will learn about Data Types in C#.

BackNext