Advertisement
wingman007

2018_IntroC#PartTimePrimiteveTypes

Sep 14th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MyAppSTD2018
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // I. Types have: name (e.g. int), size, default value
  14.             // 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
  15.             // built-in types:  
  16.             //1. integer (default = 0)
  17.             byte myByte = 23; // unsigned 8-bis (0 to 255) default = 0
  18.             sbyte mySByte = -128; // signed 8-bit (-128 to 127) default = 0
  19.  
  20.             short myShort = -1000; // signed 16-bit (-32,768 to 32,767)
  21.             ushort myUshort = 2000;  // unsigned 16-bit (0 to 65,535)
  22.  
  23.             int myVar = 4; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
  24.             int myVar2 = 5; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
  25.             int sum = myVar + myVar2; // signed 32-bit (-2,147,483,648 to 2,147,483,647)
  26.             uint myUint = 12000U; // unsigned 32-bit (0 to 4, 294, 967, 295)
  27.             sum = 0xA8F1; // hexadecimal literal
  28.             sum = 0XA8F1; // hexadecimal literal
  29.  
  30.             long myLong = 42432L;// signed 64-bit (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)
  31.             ulong myUlong = 23423423U; // unsigned 64-bit (0 to 18,446,744,073,709,551,615)
  32.             ulong maxIntValue = UInt64.MaxValue;
  33.             Console.WriteLine(maxIntValue); // 18446744073709551615
  34.  
  35.             // 2. Real (default 0.0 for F/f D/d M/m)
  36.             float myFloat = 4.566F; // signed 32-bit (±1.5e-45 to ±3.4e38) up to 7 symbols/digits
  37.             myFloat = 67.8E35f; // litteral with "E/e"
  38.             double myDouble = 34.56d; // signed 64-bit (±5.0e-324 to ±1.7e+308) up to 15-16 symbols/digits
  39.             myDouble = -3.4e-10d;
  40.             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
  41.  
  42.             // Declare some variables
  43.             float floatPI = 3.141592653589793238f;
  44.             double doublePI = 3.141592653589793238;
  45.             // Print the results on the console
  46.             Console.WriteLine("Float PI is: " + floatPI); // Float PI is: 3.141593 only 7 digits
  47.             Console.WriteLine("Double PI is: " + doublePI); // Double PI is: 3.14159265358979  16 digits
  48.             decimal decimalPI = 3.14159265358979323846m;
  49.             Console.WriteLine(decimalPI); // 3.14159265358979323846
  50.  
  51.             // 3. Char
  52.             char myFirstLetter = 'S';
  53.             Console.WriteLine((int)myFirstLetter);
  54.             char symbol = (char)5;
  55.             char myChar = '\u0065';
  56.             Console.WriteLine(myChar);
  57.             myChar = '\uffff';
  58.             Console.WriteLine(myChar);
  59.             // escaping
  60.             char myEscape = '\n'; // \n \t \r \' \\ \" \uXXXX
  61.  
  62.             // 4. String (default null) Reference, value in the heap
  63.             string myName = "Stoyan Cheresharov";
  64.             string path = @"D:\CamtasiaVideos"; // instead of D:\\CamtasiaVideos
  65.  
  66.             Console.WriteLine($"The Path is {path}");
  67.  
  68.             // 5. Bool (default false)
  69.             bool myBool = true; // false | true
  70.  
  71.             // 6. Object (default null) Reference, value in the heap
  72.             Object myObject = 3;
  73.  
  74.             Nullable<int> i1 = null;
  75.             int? i2 = i1;
  76.  
  77.             // typeof()
  78.             // Console.WriteLine(typeof(i1)); // error
  79.             // Used to obtain the System.Type object for a type. A typeof expression takes the following form:
  80.             System.Type type = typeof(int);
  81.             int i = 0;
  82.             type = i.GetType();
  83.             Console.WriteLine(type);
  84.  
  85.             // sizeof - Used to obtain the size in bytes for an unmanaged type.
  86.             Console.WriteLine(sizeof(sbyte)); // 1
  87.             // Unmanaged types include:
  88.             // The simple types that are listed in the following table:
  89.             // Expression Constant value
  90.             //sizeof(sbyte)   1
  91.             //sizeof(byte)    1
  92.             //sizeof(short)   2
  93.             //sizeof(ushort)  2
  94.             //sizeof(int) 4
  95.             //sizeof(uint)    4
  96.             //sizeof(long)    8
  97.             //sizeof(ulong)   8
  98.             //sizeof(char)    2(Unicode)
  99.             //sizeof(float)   4
  100.             //sizeof(double)  8
  101.             //sizeof(decimal) 16
  102.             //sizeof(bool)    1
  103.  
  104.  
  105.             Console.WriteLine("Hello World! The sum is " + sum);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement