How to use C# Double Variables

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to show you guys How to use C# Double Variable in your projects. In our previous tutorial, we have seen How to use C# Int Variable and we have also discussed about the term Variables in detail. So, I am not gonna discuss it again and gonna come straight to our topic C# Double variable. It's my second C# tutorial in the series of C# variables. In the coming tutorials, I will discuss the remaining C# variables in detail. I have also provided the project code below but I would suggest you to do it on your own so that you get most out of it.  Moreover, you should also have a look at Introduction to C# Windows Forms and How to use C# Control in Widows Forms as I am gonna use two C# Controls in today's tutorial as I did in C# Int tutorial. So, let's get started with How to use C# Double Variable:

How to use C# Double Variable ???

  • C# double variable can store 64 bit value in it.
  • C# double can store integer as well as decimal values means you can save decimal values like 5.2, 3.1 as well as simple integers like 2, 5, 38 etc.
  • C# double variable approximate value is ±5.0 × 10^-324 to ±1.7 × 10^308 as per Microsoft Official Page and its precision is 15-16 digits.
  • So, first of all, design a new simple C# project as we did in Introduction to C# Windows Forms.
  • I am not gonna design a new one, instead I am gonna use the same project as I did for C# Int Variable.
  • So after creating a new project, add a Button and a Text Box in it as we did in How to add C# Controls in Windows form.
  • Now, I hope that you are done with the front end of your C# Project and it will look something as shown in below figure:
  • Now, let's add the code behind this form, so your code will look something as shown in below figure:
  • Now if you have a look at it then you will see that its exactly the same code as we did for C# Int variables.
  • But there's a slight difference that now all of the three variables a, b and c are double instead of int.
  • That's why I have assigned decimal values to them where a = 2.5 while b = 2.6.
  • C# Int variable can't use these value because these are not integers but double can easily handle them.
  • So, now their result should be 5.1 which is again a double so that's why our third variable is also double.
  • Here's the code for you to copy:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Variables
{
    public partial class Form1 : Form
    {
        double a = 2.5;
        double b = 2.6;
        double c;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ClickHere_Click(object sender, EventArgs e)
        {
            c = a + b;
            txtClick.Text = c.ToString();
        }
    }
}
  • So, now run your project and then click the button and you will get results as shown in below figure:
  • So, now you can see the value saved in C# double variable c was 5.1 which is now displayed in the Text Box.
So, that's all for today. I hope you guys have now understood How to use C# double variables in your projects. Thanks for reading. Have fun and take care. :)

How to use C# Int Variables

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new tutorial on C# language which is How to use C# Int variables. In our previous C# tutorials, we have designed a small project in C# which prints Hello World on the screen. Then we have seen Introduction to C# Windows Form, in which we have designed our first Windows form in C#. Then we have added the C# controls on that windows form.

Now, in today's tutorial, I am gonna introduce you to a new concept which is variables. Variables are used in every programming language and without variables we can't design even a simple code. There are different types of variables available in C# language and today among these variables we are gonna focus on C# Int variables. If you are doing engineering then you must have the idea about integers. :P So, let's get started with C# Int variables. Before discussing the C# Int variables, let's first get an overview of variables.

What are Variables ???

  • In any kind of programming projects, there's a need for storing some data in temporary memory spaces. these temporary memory spaces are named as variables.
  • For example, I have a simple project in which I need to add 2+2 and then I need to add 2 more in the output.
  • So, in that case what I am gonna do is first of all, I am gonna do the simple addition operation and will add the 2+2 and then I need to save the output which is 4 somewhere.
  • So, I am gonna save this output 4 in a variable and then I am gonna add 2 more in that variable and again I will save the output which is 6 in another variable and then I can print it on the output.
  • I think now its quite clear from the above example that variables are used to store data for a short time.
  • Now data can be of any type, it could be an integer or some decimal value or it could be characters.
  • So, that's why we have a long list of variable types in C#, which are as follows:
  • Int (Integer)
  • Double
  • Float
  • Char
  • String
  • Byte
  • sByte
  • Decimal
  • Boolean
  • You can read more about the variables from Microsoft Variables Page.
We will cover all these variables in separate tutorials one by one and we will see how to use them and to work with them. So, rite now we are gonna have a look at usage of C# Int variable, so let's get started with it.

How to use C# Int variables ???

  • C# Int variables are used for storing integers values. Int is actually a short form of Integer.
  • So, for example, you have to save 2 in some variable then as 2 is an integer so you can save it in C# Int variable.
  • But if you have 2.05 value then its not an integer because it has a decimal part so you can't save this value in an integer so C# Int variable will only save integers in it.
  • Let's design a simple example on C#, as we did in Introduction to C# Windows Forms.
  • Now in this project, I am gonna add a Button and a Text Box and on pressing that Button I will print a variable in this Text Box.
  • So, design a simple project, as shown in below figure:
  • I have changed the name of the Button to "ClickHere" and the Text of the Button to "Click Here".
  • I have also changed the Name of the Text Box to "txtClick".
  • Now add some code in the Button as shown in below figure:
  • In the above code, you can see first of all I have declared the C# Int variables, which are a, b and c.
  • Then I have also assigned a value to a and b, which is 2.
  • Now in the Button Click function, I have added both the C# Int variables and then saved their result in another C# Int variable named as c.
  • Finally, I have printed that c variable in a text box.
  • You must be wondering why I have used toString() because text boxes can only print strings but c is an integer so that's why I have converted that integer into string.
  • I will discuss this string conversion in more detail later.
  • Here's the code for you to use:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Variables
{
    public partial class Form1 : Form
    {
        int a = 2;
        int b = 2;
        int c;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ClickHere_Click(object sender, EventArgs e)
        {
            c = a + b;
            txtClick.Text = c.ToString();
        }
    }
}
  • Now run your project and when you click the button then 4 will appear in the text box as shown in below figure:

So, I hope now you have got the idea How to use C# Int Variables. That's all for today. You should also have a look at How to use C# Double Variables. In the coming tutorial, we will discuss other C# variables just like this C# Int variable. Till then take care and have fun !!! :)

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir