C#: Variables and Primitive Data Types

Hello readers, most of you are familiar about variables and data types, we all deal with variables and data types in most of the programming languages,
before starting any program we declare variables, initialize them with some values and so on. So just like other languages in C# also we deal with variables, there is not much change in C# with variables but what change is input and output statements, like if you are working with C++ then we use "cin" and "cout" for our input and output, if we are dealing with php then also initialization, input and output differ, so basically it depends on programming language we are dealing with.

Today in this chapter we learn about "Variables and Primitive Data Types in C#", why we use variables and data types?, what is variable? , what is data type?, how can we implement it? . Now its enough for our intro part. Lets get started!!

Variables

To get started with variables just have look at some point mentioned below
  • A variable refers to the named storage location in the memory that contains data.
  • This data can contain any value such as integers, character or decimal.
  • A variable needs to be declared and initialized before you use it.
  • Variables are associated with data types.
  • Variable declaration refers to the task of defining the data type of a variable. In other words, every variable has a type that determines the value to be stored in a variable.
  • Variable initialization refers to the task of assigning the variable with any starting value before it is used in a program.

Here in above figure you'll see we have taken three variables "x" , "y" and "z" and also we initialized them with some value ( value can either be integer, decimal or character) . Like here we initialized x=10, that means the value "10" is stored in the location called "x", we initialized y='K' that means the value "K" is stored in the location called "y" and where "K" is alphabet i.e character . Also we initialized z=7.5 i.e decimal and which means the value "7.5" is stored in the location called "z". Note down here value can either be integer, decimal or character.

Note : For characters we use single quotes, characters, floats, integers can also store NULL values, like y=' ' , x=0 and z=0.0.

Data Types

  • The data types represent kind of data that will be stored in a variable.
  • C# provides the built in data types, also known as primitive data types.
  • Built in data types are the predefined data types.
  • The most commonly used data types are :
    • int - used to store integer values.
    • float - used to store floating point numbers such as decimals.
    • char - used to store the alphabets or characters.
    • string - used to store strings , statement or word.
    • bool - used to store TRUE or FALSE

Here in above figure you'll see the highlighted part i.e "float", "char" and "int". These are primitive data types. Primitive data types are the built in data types provided by programming language , here that language is C#. Here you see to store "z=7.5" we have taken "float" data type , float is used if you are storing value which is floating point or decimal. Second, one is "char" , char is used if we are storing value which is some character or alphabet. For example in some software , they asks you to hit "Y" or "N" for yes or no for exit , when you press "Y" the software terminates and when you press "N" it didn't , so "char" is used for such applications. Now, our third data type is "int" , i.e int x=10, "int" is used for integers values.  

So, basically data types are the kind of data we are feeding in our memory. There our various other data types also like string, bool and so on depending upon the kind of value.

The Syntax


using System;
​class prg
{
 public static void Main(String []args)
 {
        int a=10;
        float b=10.5;
        char c='K';
        //.
        //.
        //.
        //.
        // do something
        //.
        //.
        //.
        //.

 }
}
Here variables a, b and c are also known as constants. Constants are the values which acquire permanent space in memory and they always result in definite values. "prg" is class name , you can change "prg" to any name which you like but i suggest you to use class name which depicts type of your program like if you are making program for armstrong number then use "Arms" for class name. Well' it depends on you mainly, you can name class with which ever name you like. "using System",here "System" is a namespace and "using" is keyword.

Keywords are some predefined words in a language with a specific defination and they are all used to construct a well structured program.

System contains many commonly-used types. These are separate from the language-level aliases in the C# language, there are various other namespaces also like ReadLine, WriteLine ans so on, we discuss them one by one, now just only focus on variables. Also ";" here semicolan is used for terminating our statement.

Now, let us take an example to understand about Variables

using System;
​class prg
{
 public static void Main(String []args)
 {
        string r="Raunak";
        Console.WriteLine("Hello my name is " + r );
 }
}

Here r="Raunak" means the string value "Raunak" is stored in the variable called "r". WriteLine is a method of the Console class defined in the System namespace, it is used for displaying statement. Here it is used ti display "Hello my name is....". The output of given program after compilation is given below. If you don't know how to compile programs in C# then follow this link

http://kodestat.blogspot.in/2014/02/compile-and-run-c-program-in-command.html

Output












Now what if we change value "Raunak" with some other name like "Kodestat", "Google" etc ??

using System;
​class prg
{
 public static void Main(String []args)
 {
        string r="Kodestat";
        Console.WriteLine("Hello my name is " + r );
 }
}

Output












Here in output you'll see "Hello my name is Kodestat" . If we changed variable value then it resulted in the change of output. This is because when we say

 " r="Kodestat" means the string value "Raunak" is stored in the variable called "r"  "

it means we are storing value "Raunak" in a variable named "r" which is memory location where it is stored.

For practice visit following tutorials

1. Sum and average of two numbers
2. Swapping of two numbers
3. Area and Perimeter of circle
4. Area and Perimeter of rectangle

I think now you all know about the concept of variables. If you like this course and have views regarding this chapter mention your feedbacks, suggestions and questions in comments. Thank You!! 
SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment