Henry's Blog

Data Types, Conversions and Variables in C#

March 27, 2020

Introduction

If you’re new to programming with C# or interested in learning C# as a programming language, then you are in the right place. On this post, I will be sharing with you the different data-types in C#, what there different ranges are, and how many bits they use. I will also talk about data-type conversions and also show you some example illustrations, so as to help you get a more better graps as you begin. C-SHARP (C#) is a general purpose, multi-paradigm programming language developed by Microsoft that runs on the .NET Framework.

So what is C#?

C-SHARP (C#) is a general purpose, multi-paradigm programming language developed by Microsoft that runs on the .NET Framework. C# is widely used for building mobile applications, games and windows applications.

using System;

namespace Hello_World { class MainClass { public static void Main(string[] args) { //Implicit converion int num = 444; long bigNum = num;

        float myFloat = 33.66f;
        double mynewDouble = myFloat;

        //Explicit conversion
        // cast double to an integer
        double myDouble = 33.75;
        int myInt;

        myInt = (int)myDouble;


        //Type conversion
        string myNumString = bigNum.ToString();
        string myFloatString = mynewDouble.ToString();
        bool codingIsSweet = true;
        string myBoolString = codingIsSweet.ToString();


        //String to Int
        string myString = "15";
        string mySecondString = "30";
        int num1 = Int32.Parse(myString);
        int num2 = Int32.Parse(mySecondString);

        int result = num1 + num2;

        Console.WriteLine(bigNum); // => 444
        Console.WriteLine(mynewDouble); // => 33.6599998474121
        Console.WriteLine(myInt); // => 33
        Console.WriteLine(myNumString); // => "444"
        Console.WriteLine(myFloatString); // => "33.66"
        Console.WriteLine(myBoolString); // => "True"
        Console.WriteLine(result); // => 45
    }
}

}


A blog by Henry Okonkwo , a full-stack software engineer who lives and work in Kigali. Feel free to follow me on any of my social media accounts.

GithubGithubTwitterInstagramFacebook

© 2020, Henry Okonkwo