C# Tutorial for Beginner : Chapter 2 Data types

INTRODUCTION

 Data types are old in all places within a programming language as C#. Because it's a high typed language, you are required in accordance with convey the compiler as regards which facts types you want to utilizes each and every time you set out a variable. Value type variables can be assigned a value directly. They are derived out of the class System.ValueType.

The value types at once contain data. Some examples are int, char, and float, which hold numbers, alphabets, and floating point numbers, respectively. When you set forth an int type, the system allocates memory to hold or store the value.
  • bool is one of the least difficult data types. It can contain just 2 esteems - false or true. The bool compose is imperative to comprehend when utilizing logical operators like the if statement.
  • int is short for integer, a data type for putting away numbers without decimals. When working with numbers, int is the most regularly utilized data type. Integers have a few data types inside C#, contingent upon the extent of the number they should store.
  • string is utilized for putting away content, that is, various chars. In C#, strings are permanent, which implies that strings are never showed signs of change after they have been made. When utilizing techniques which changes a string, the genuine string isn't changed - another string is returned.
  • char is utilized for putting away a solitary character.
  • float is one of the data types used to store numbers which might contain decimals.
For more detail take a look table below this :

image source : tutorialpoint

CODE

 To understand data type better, let us consider example code below :

using System;

class Solution
{
    static void Main(String[] args)
    {
        int i = 4;
        double d = 4.0;
        string s = "Hamuhamu";

        // Declare second integer, double, and String variables.
        int i2;
        double d2;
        string s2;

        // Read and save an integer, double, and String to your variables.
        i2 = Convert.ToInt32(Console.ReadLine());
        Double.TryParse( Console.ReadLine(),out d2 );
        s2 = Console.ReadLine();

        // Print the sum of both integer variables on a new line.
        Console.WriteLine(i+i2);

        // Print the sum of the double variables on a new line.
        double doubleResult = d + d2;
        Console.WriteLine(doubleResult.ToString("0.0#"));

        // Concatenate and print the String variables on a new line
        // The 's' variable above should be printed first.
        Console.WriteLine(s + s2);

        Console.ReadLine();

    }
}


CODE ANALYSIS

  • "using System;" in this project we use System namespace. If you are familar with java or other programming languages, then namespace here is the same as the library.
  • class Solution .In a program we need to create at least one class. And here we create a class with the name "Solution".
  • static void Main (String [] args). Main method is the method that will be first run in a class.
  • Declaration of variables with required data types. In the code example above I declare 3 variables at the same time initialize it. The variable i stores the integer value, the variable d stores with the decimal number, and the variable s stores the set of char called the string. So basically a string is a collection of chars in an array.
  • Then declare variable i1 with type integer, d2 type double, and s2 type string without initializing the three variables.
  • "I2 = Convert.ToInt32 (Console.ReadLine ());" reads the string from the input we wrote on and then converts it to an integer.
  • "Double.TryParse (Console.ReadLine (), out d2);" This line also serves to convert the string we write to the double data type. Actually this command returns a boolean value. If successfully convert it, then will return true and if it fails, it will return false value. However, "TryParse ()" also returns d2 value that contains double from conversion result if the process succeeds. You can just use a command similar to an integer conversion above like the following code "d2 = Convert.ToDouble (Console.ReadLine ());". I myself recommend using TryParse because it is safer in handling errors.
  • "S2 = Console.ReadLine ();" We do not need to convert it in any form because "Console.ReadLine ()" has returned it in string.
  • "Console.WriteLine (i + i2);" for processing arithmetic in "WriteLine ()". Note that there should be no string in it like "Console.WriteLine (i + i2 +" hello ");" because the result is not adding instead showing the variable numbers i then i2. To overcome this then the arithmetic process is inserted in brackets like this "Console.WriteLine ((i + i2) +" hello ");"
  • "Double doubleResult = d + d2;" This line processes the sum operation between variables d and d2. The result of the sum is stored into the variable "doubleResult"
  • To print a double value with floating point or decimal view then print doubleResult in this way "Console.WriteLine (doubleResult.ToString (" 0.0 # "));". Normally 4.0 will be printed only 4 in the console. To display a decimal on a double value then we add an argument to the ToSting () method.
  • "Console.WriteLine (s + s2);" This line for printing string s and s2 in a row.

RESULT

 After you run the code, it will ask you to input the text. After that it will show the result like the picture below


Comments

Popular posts from this blog

Quantization of Image Data Implementation in C#

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Computer Graphics Project "MINION" Source Code with Glut OpenGL