SEARCH C# PROGRAMMING CODES HERE

Data type & Variables in C# programming language

Data type & Variables in C#
Data types
Data types are used everywhere in a programming language like C#. To perform any specific task in c#  first you are required to inform the compiler about which data types you wish to use every time for that you have to declare a variable, So let's take a look at some of the most used data types in C# programming as following.

1-int
The integers data type is used for storing numbers without decimals. you can store a maximum of 10 digits numbers. When working with numbers, it is the most commonly used data type. Integers have several data types within C# In further chapters, we will learn more about integers type, depending on the size of the number they are supposed to store. So let's see the example which demonstrates to initialize integer value as shown below.

Program

using System;
namespace int_example
{
    class program
    {
        static void Main(string[] args)
        {
            int x = 40;
            Console.WriteLine(x);
            Console.ReadKey();
           
        }
    }

}

Output-10

In the above program, you can see I have defined an int variable name called x and then I have assigns an int value=40 to it.

2-string
Strings are one of the most significant data types in any modern language including C#. A string can contain any number of installed invalid characters ('\0'). The string is used for storing text, that is, a number of chars. In C#, strings are immutable, which means that strings are never changed after they have been created. For example, in the below code you can see I have defined a string variable named called x and then I have assigns a text value to it.
string x = "Rohit"; 
So let's see the complete process to implement in the program.
Program
using System;

namespace String_Example

{
    class program
    {
        static void Main(string[] args)
        {
            string x = "Rohit";
            Console.WriteLine(x);
            Console.ReadKey();
           
        }
    }


Output-Rohit

3-char
Char data type is used to hold a single, Unicode character. The char type represents a single character which is like a whole number. It takes 2 bytes in width. So let's see the program which demonstrates char declaration as follows.
Program
using System;
namespace char_example
{
    class program
    {
        static void Main(string[] args)
        {
            char value = "ABCDE"[1];
          
            if (value == 'B') 
            {
                Console.WriteLine(value);
            }
                Console.ReadKey();
          
        }
    }
}

Output-B

4-float
float is one of the data types used to store numbers which can contain decimals.
It permits IEEE 754 single-precision floating numbers to be stored this data type is available in mscorlib.dll which is verifiably referenced by each C# project when you make them.
The precision and approximate range for the float type as follows.
Range: -3.4 × 1038 to 3.4 × 1038
Decimal precision: 6-9 significant digits
To initialize a float variable you can use the suffix f, as shown in the following program.

using System;


namespace float_example
{
    class program
    {
        static void Main(string[] args)
        {

            float x = 2.5f;

            Console.WriteLine(x);
            Console.ReadKey();
           
        }
    }

}

Output-2.5


5-Double

Double is an 8-byte numeric sort and also it represents a double-precision 64-bit floating-point number which is present in the mscorlib.dll library which is implicitly referenced in any C# project. It is utilized to store large and small values and also it stores fraction values as well, for example, 2.5 and negative values, for example, - 2.5. It requires more memory than an int. 
if you want to initialize integer number to be double then you can use the suffix d, as shown in the following example.

Example-double x=5d;

The precision and approximate range for the double type as follows.
Range: ±5.0 × 10−324 to ±1.7 × 10308
Decimal precision: 15-17 significant digits
So now let's see an example to initialize double value as follows.
Program

using System;

namespace double_example
{
    class program
    {
        static void Main(string[] args)
        {

            double x = 54567.22555;

            Console.WriteLine(x);
            Console.ReadKey();
           
        }
    }

}

Output-54567.22555

6-bool 
bool is one of the simplest data types. It can contain only 2 values - false or true. The bool type is important to understand when using logical operators like the if statement. So let's see the below program which demonstrates bool type.

Program


using System;

namespace bool_example
{
    class program
    {
        static void Main(string[] args)
        {
            bool y = true;
            int x = 5;
            /* Assign the result of a boolean expression to y*/
            y = (x== 5);
          
            if (y) 
            {                                 /*if true then do this*/
                Console.WriteLine("Boolean expression is True");
            }
            else
            {      /*if false then do this*/
                Console.WriteLine("false");
            }   
            Console.ReadKey(); 
        }
    }

}

As we can see in the above program boolean expression y=(x==5)is true.

Output-Boolean expression is True



Variables
A variable can be compared to a storage room and is essential for the programmers. For each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable. Variables are initialized with an equal sign followed by a constant expression in C#.
<data type> <name>;
The data type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable name may consist of one or more identifier names separated by commas as shown below.

int x, y, z;
char c, ch;
float f, amount;
double d;

We use the following syntax to define a variable in C#
int  x = 10;
float Float = 1f;
bool Boolean = true;
string Name = "Rohit";
char Char = 'a';
double Double = 5.25;


For Example you have to calculate two integer values then you can  simply implement in this way as follows.
/*Declaration*/
int a=10;
int b=15; 
int c=a+b;

/*Initialize*/
Console.WriteLine("c");

Output =25