wingman007 icon

2018_IntroC#PartTimePrimiteveTypes

wingman007 | PRO | 09/14/18 09:34:03 AM UTC | 0 ⭐ | 987 👁️ | Never ⏰ | []
C# |

4.55 KB

|

None

|

0 👍

/

0 👎

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace MyAppSTD2018
{
    class Program
    {
        static void Main(string[] args)
        {
            // I. Types have: name (e.g. int), size, default value
            // II. Variables have: name (A-Za-z_0-9 all utf-8, can't start with 0-9, can't be a keyword), type, value
            // built-in types:  
            //1. integer (default = 0)
            byte myByte = 23; // unsigned 8-bis (0 to 255) default = 0
            sbyte mySByte = -128; // signed 8-bit (-128 to 127) default = 0
 
            short myShort = -1000; // signed 16-bit (-32,768 to 32,767) 
            ushort myUshort = 2000;  // unsigned 16-bit (0 to 65,535)
 
            int myVar = 4; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
            int myVar2 = 5; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
            int sum = myVar + myVar2; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
            uint myUint = 12000U; // unsigned 32-bit (0 to 4, 294, 967, 295)
            sum = 0xA8F1; // hexadecimal literal
            sum = 0XA8F1; // hexadecimal literal
 
            long myLong = 42432L;// signed 64-bit (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
            ulong myUlong = 23423423U; // unsigned 64-bit (0 to 18,446,744,073,709,551,615)
            ulong maxIntValue = UInt64.MaxValue;
            Console.WriteLine(maxIntValue); // 18446744073709551615
 
            // 2. Real (default 0.0 for F/f D/d M/m)
            float myFloat = 4.566F; // signed 32-bit (±1.5e-45 to ±3.4e38) up to 7 symbols/digits
            myFloat = 67.8E35f; // litteral with "E/e"
            double myDouble = 34.56d; // signed 64-bit (±5.0e-324 to ±1.7e+308) up to 15-16 symbols/digits
            myDouble = -3.4e-10d;
            decimal myDecimal = 23.45M; // signed 128-bit (±1.0e-28 to ±7.9e28) precission 28-29 symbols/digits, closest to 0 ±1.0e-28, decimal floating-point arithmetic
 
            // Declare some variables
            float floatPI = 3.141592653589793238f;
            double doublePI = 3.141592653589793238;
            // Print the results on the console
            Console.WriteLine("Float PI is: " + floatPI); // Float PI is: 3.141593 only 7 digits
            Console.WriteLine("Double PI is: " + doublePI); // Double PI is: 3.14159265358979  16 digits
            decimal decimalPI = 3.14159265358979323846m;
            Console.WriteLine(decimalPI); // 3.14159265358979323846
 
            // 3. Char
            char myFirstLetter = 'S';
            Console.WriteLine((int)myFirstLetter);
            char symbol = (char)5;
            char myChar = '\u0065';
            Console.WriteLine(myChar);
            myChar = '\uffff';
            Console.WriteLine(myChar);
            // escaping
            char myEscape = '\n'; // \n \t \r \' \\ \" \uXXXX
 
            // 4. String (default null) Reference, value in the heap
            string myName = "Stoyan Cheresharov";
            string path = @"D:\CamtasiaVideos"; // instead of D:\\CamtasiaVideos
 
            Console.WriteLine($"The Path is {path}");
 
            // 5. Bool (default false)
            bool myBool = true; // false | true 
 
            // 6. Object (default null) Reference, value in the heap
            Object myObject = 3;
 
            Nullable<int> i1 = null;
            int? i2 = i1;
 
            // typeof()
            // Console.WriteLine(typeof(i1)); // error
            // Used to obtain the System.Type object for a type. A typeof expression takes the following form:
            System.Type type = typeof(int);
            int i = 0;
            type = i.GetType();
            Console.WriteLine(type);
 
            // sizeof - Used to obtain the size in bytes for an unmanaged type.
            Console.WriteLine(sizeof(sbyte)); // 1
            // Unmanaged types include:
            // The simple types that are listed in the following table:
            // Expression Constant value
            //sizeof(sbyte)   1
            //sizeof(byte)    1
            //sizeof(short)   2
            //sizeof(ushort)  2
            //sizeof(int) 4
            //sizeof(uint)    4
            //sizeof(long)    8
            //sizeof(ulong)   8
            //sizeof(char)    2(Unicode)
            //sizeof(float)   4
            //sizeof(double)  8
            //sizeof(decimal) 16
            //sizeof(bool)    1
 
 
            Console.WriteLine("Hello World! The sum is " + sum);
        }
    }
}

Comments