C# Language: Variables and consts

in c-sharp •  2 years ago 

CC.png

In variables and constants we store some data on which we can work, but as the name suggests, they differ in that the variables can be changed and the constants no longer.

1. Variables types

There are different types of variables (and constants), some to store integers, other to store floating-point numbers, some variables store words, and others store only the logical value, true or false.

bool – stores the truth (true) or false (false)

char – a single character written in Unicode

string – string, characters in Unicode

short – 16-bit integer

int – 32-bit integer

long – 64-bit integer

float – a 32-bit floating point number

double – 64-bit floating point number

Below is a program that explains the operation of these variables:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            bool negation = true;
            int a = 2432;
            char  character= 'f';
            string text = "this is some text";
            short number = 4;
            long bigNumber = 231242543654765;
            float FloatingPointNumber=12.56f;
            double BiggerFloatingPointNumber=1231243124324.97;

            Console.WriteLine("We will enter something on screen!\n"+negation +"\n"+a+"\n"+character+"\n"+text +"\n"+number +"\n"+bigNumber +"\n"+FloatingPointNumber+"\n"+BiggerFloatingPointNumber);

            Console.ReadKey();
        }
    }
}

You can also display it in a different way

Console.WriteLine("We will enter something on screen!\n{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}", negation ,a,character,text ,number ,bigNumber ,FloatingPointNumber,BiggerFloatingPointNumber);

For sure you understand this, in place {0} will be replace variable value "negation" in place of {1} will be replace variable value "a", etc.

2. Conversion

You can also "convert" the type of double into an int type of type, int type stores only integers so it will display a digit on the screen without numbers after comma.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            double BiggerFloatingPointNumber= 124.97;
            int converting= (int)BiggerFloatingPointNumber;

            Console.WriteLine("We will enter something on screen!\n{0}",converting);

            Console.ReadKey();
        }
    }
}

However, if we want to convert the string type with the word "1001" to the type int then convertion do nothing. We will have to use static method.

string text = "1001";
int number = int.Parse(text);
Console.WriteLine("We will enter something on screen!\n{0}",number);

In case we want to do the opposite, means to replace the int type with string, then we have to use the special ToString() method.

int number = 1001;
string text = number.ToString();
Console.WriteLine("We will enter something on screen!\n{0}",text);

Based on all the information above, try to guess what the following program will display:

int number = 1001;
string text = "234";
Console.WriteLine("We will enter something on screen!\n{0}\n{1}",number ,text+6);

If you thought that 1001 and 2346 are congratulations, you're right because the number 234 was a string and the number 6 are was simply glued to that chain

In addition, we have a ready class "Convert" which has many ready conversion tools, below example:

int number = 0;
bool negation = Convert.ToBoolean(number);
Console.WriteLine("We will enter something on screen!\n{0}",negation);

The above example will display false on the screen.

3. Constants

As the name suggests, constants can not change, but they work just like variables. The following example generates an error because we tried to change the value of the "number" constant.

const int number = 123;
number = number + 34;

This content also you can find on my blog devman.pl: http://devman.pl/csharplan/language-c-variables-consts/

If you recognise it as useful, share it with others so that others can also use it and leave upvote and follow if you wait for next articles :) .

Thanks and invite you to the next lesson! And I apologize for my English, but I hope that everything is understood, if not, write about it in the comments, or a message to me.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

after three months i learn this language in this time i am learning c++

  ·  2 years ago (edited)

Good! C# it is a simple language, but If you're learning C++ you must careful on diffrences beetwen this languages which are big :)

Hello slawas! Thanks for sharing. Keep writing good quality content and let's grow the community together.