settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: How to assign integer values to variables in C#?

+4 votes

I need to create a new C# project and to create a program that assigns integer values to variables. I must make sure that each value is stored in the correct variable type (lowest - better - to be more productive). And of course to print them to the console.

The numbers are:

-100

128

-3540

64876

2147483648

-1141583228

-1223372036854775808

asked in C# category by user Jolie Ann
edited by user golearnweb

2 Answers

+2 votes
 
Best answer

The best option is to first try to find the most suitable variable type in order to save memory - the lowest ones - the program will load faster!

Here is my solution:

using System;

class PracticeIntegerNumbers
{
    static void Main()
    {
        sbyte a = -100;
        byte b = 128;
        short c = -3540;
        ushort d = 64876;
        uint e = 2147483648;
        int f = -1141583228;
        long g = -1223372036854775808;

        Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n", a, b, c, d, e, f, g);
    }
}
answered by user eiorgert
edited by user golearnweb
+1 vote

...and for another 5 numbers:

using System;

class DeclareVariables
{
    static void Main()
    {
        ushort a = 52130;
        sbyte b = -115;
        uint c = 4825932;
        byte d = 97;
        short e = -10000;

        Console.WriteLine("{0}: ushort \n{1}: sbyte\n{2}: uint \n{3}: byte \n{4}: short \n", a, b, c, d, e);
    }
}
answered by user andrew
edited by user golearnweb
...