Datatypes Int, Float, and Double (source code)
This article shows the source code for one of our lessons from C# Masterclass. Moreover, here you can find extra visualization of data types.
Data types visualization
I propose to think of numerical data types as cups of coffee with different sizes.
Because when we initialize any variable, C# asks our computer for a chunk of memory. And the size of this chunk depends on the type.
So every time you do so, try to verify that you don’t drink an espresso from a cup for an americano.
Moreover, later in the course, we will teach you to cast (AKA change the type) variables. This analogy will help you understand why when we change the type from long to int, we may lose some precision.
Source code
// SOURCE CODE FOR THE INT TYPE int num3, num4, num5; int num1 = 13; int num2 = 5; num2 = 100; int sum = num1 + num2; Console.WriteLine("num1 is " + num1); Console.WriteLine("num1 " + num1 + " + num2 " + num2 + " is " + sum); // SOURCE CODE FOR THE DOUBLE TYPE double d1 = 3.1415; double d2 = 5.1; double sumD = d1 / d2; Console.WriteLine("d1 " + d1 + " devided by d2" + d2 + " is " + sumD); // SOURCE CODE FOR THE FLOAT TYPE float f1 = 3.1415f; float f2 = 5.1f; float sumF = f1 / f2; Console.WriteLine("f1 " + f1 + " devided by f2" + f2 + " is " + sumF); // SOURCE CODE FOR THE LONG TYPE long l1 = 8758758975875; long l2 = 7697595897587; double sumL = l1 / l2; Console.WriteLine("l1 " + l1 + " devided by l2" + l2 + " is " + sumL); ///double dIDiv = d1 / num1; ///Console.WriteLine(dIDiv);
If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.