Advertisement
here2share

-- C# Basics

May 13th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.36 KB | None | 0 0
  1. // To backslashes for a single-line developer comment
  2.  
  3. /*
  4.  * This
  5.  * is
  6.  * a
  7.  * multi
  8.  * line
  9.  * dev
  10.  * comment
  11.  */
  12.  
  13. // C# Programming Structure https://imgur.com/kdMz5OK
  14. using System;
  15.  
  16. namespace Basics
  17. {
  18.     class Program
  19.     {
  20.         static void Main(string[] args) // Main() is ran first and automatically
  21.         {
  22.             // Console.OPERATION(ARGS) is the general context for "Console" functions
  23.             Console.WriteLine("Hello World!"); // Make sure to always end statements with a semicolon ( ; )
  24.  
  25.             /* C# Data/Variable Types - http://www.dotnetfunda.com/articles/show/1491/datatypes-in-csharp (Link doesn't cover everything but it is a good resource)
  26.              *
  27.              * Primitive Variable Types (Predefined) - https://imgur.com/EPxl0Md
  28.              *  Integral Numbers
  29.              *     byte
  30.              *     short
  31.              *     int
  32.              *     long
  33.              *  Real Numbers
  34.              *      float
  35.              *      double
  36.              *      decimal
  37.              *  Character
  38.              *      char
  39.              *  Boolean
  40.              *      bool
  41.              *
  42.              * Non-Primitive Variable Types (User Defined) - http://www.jeremyshanks.com/c-variables-primitive-nonprimitive-types/ (Non-primative is the second section)
  43.              *      string
  44.              *      array
  45.              *      enum
  46.              *      class
  47.              *      struct
  48.              */
  49.  
  50.             // Creating variables
  51.             var Number = 2;
  52.             byte number = 3; // Notice caps. vs non-caps. (C# is very critical on capitalization)
  53.             int count = 10;
  54.             float totalPrice = 20.95;
  55.             char character = 'A'; // Characters use single quotes (')
  56.             string firstName = "Brandon"; // Strings use double quotes (")
  57.             bool isWorking = false;
  58.  
  59.             // Printing variables to the console
  60.             Console.WriteLine(Number);
  61.             Console.WriteLine(number);
  62.             Console.WriteLine(count);
  63.             Console.WriteLine(totalPrice); // Tip: Press SHIFT+ENTER anywhere on a line to create a new line below (Doesn't work on lines that are ONLY comments)
  64.             Console.WriteLine(character);
  65.             Console.WriteLine(firstName);
  66.             Console.WriteLine(isWorking);
  67.  
  68.             /* Basic C# Operators:
  69.              *  Arithmetic
  70.              *      - Computation with numbers
  71.              *          +, -, *, /, %
  72.              *          ++, --
  73.              *  Comparison
  74.              *      - True/False when comparing numbers
  75.              *          ==, !=, >, >=, <, <=
  76.              *  Assignment
  77.              *      - To set equal to
  78.              *          =, +=, -=, *=, /=
  79.              *  Logical
  80.              *      - Compare/contrast for conditional statements
  81.              *          && (AND), || (OR), ! (NOT)
  82.              *  Bitwise
  83.              *      - Used in low level programming, such as encryption, so it will not be covered in this basic C# guide
  84.              *          & (and), | (or)
  85.              */
  86.  
  87.             // Arithmetic Operations
  88.             Console.WriteLine(2 + 3 * 10); // Prints "32" because order of operations (PEMDAS) is followed
  89.  
  90.             // Comparison Operations
  91.             Console.WriteLine(6 == 2 * 3); // Prints true becuase 6 is equal to 6
  92.             Console.WriteLine(17 != 7); // Prints true because 17 is not equal to 7
  93.             Console.WriteLine(9 <= 5); // Prints false because 9 is not less than or equal to 5
  94.  
  95.             // Assignment Operations
  96.             int starfruit = 9; // The variable "starfruit" is assigned the value 9
  97.             starfruit += 2; // The var now equals 11 because 9 + 2 = 11
  98.             starfruit -= 8; // Var is now 3 because 11 - 8 = 3
  99.             starfruit *= 2; // Var is now 6 because 3 * 2 = 6
  100.             starfruit /= 6; // Var is now 1 because 6 / 6 = 1
  101.             Console.WriteLine(starfruit == 1); // If it prints "True", then we know the assignment operators worked as planned
  102.  
  103.             // Logical Operations
  104.             Console.WriteLine(!(true && false || true)); // Prints false
  105.  
  106.             // Interpolating strings
  107.             Console.WriteLine("{0} {1}", byte.MinValue, byte.MaxValue);
  108.             Console.WriteLine("{0} {1}", float.MinValue, float.MaxValue);
  109.  
  110.             // Constant (const) variables cannot change once declared
  111.             const float Pi = 3.14f;
  112.             Pi = 1;
  113.  
  114.             // Implicit type conversion - types are compatible
  115.             byte b = 1; //                            00000001
  116.             int i = b;  // 00000000 00000000 00000000 00000001
  117.             long l = i; // Types can be converted going up (implicitly) because there is no chance of data loss
  118.  
  119.             // Explicit type conversion - types are not compatible
  120.             i = l; // C# is preventing possible data loss
  121.             i = (int)l; // Forcing the conversion
  122.  
  123.             // Non-compatible type conversion
  124.             string s = "1";
  125.             i = (int)s; // Primitive and non-primitive types don't like being converted
  126.  
  127.             i = int.Parse(s); // Parse only works with primitive types
  128.             i = Convert.ToInt32(s); // Requires system namespace
  129.  
  130.             /* Using Convert.*();
  131.              *
  132.              * ToByte() converts to a byte
  133.              * ToInt16() converts to a short
  134.              * ToInt32() converts to an int
  135.              * ToInt64() converts to a long
  136.              * ToBoolean() converts to boolean (be careful)
  137.              */
  138.  
  139.             // The problem with forcing conversions
  140.             var stringA = "1234";
  141.             byte byteA = Convert.ToByte(number);
  142.             Console.WriteLine(b); // This crashes because we didn't handle the exception
  143.  
  144.             try // We can handle the exception by using a try-catch method
  145.             {
  146.                 var stringB = "1234"; // This code in the "try" block won't be ran if an error occurs
  147.                 byte byteB = Convert.ToByte(number);
  148.                 Console.WriteLine(b);
  149.             }
  150.             catch (Exception) // This will be ran if the "try" block of code outputs an error
  151.             {
  152.                 Console.WriteLine("The number could not be converted to a byte.");
  153.             }
  154.  
  155.             // Special cases with conversions
  156.             char apple = 'a';
  157.             int amount = (int)apple;
  158.             Console.WriteLine(amount);
  159.  
  160.             bool response = false;
  161.             int value = Convert.ToInt32(response);
  162.             Console.WriteLine(value); // false = 0, true = 1 (Think of I/O where I is a 1, meaning on, and O is a 0, meaning off)
  163.  
  164.             int truth = 1; // Another boolean example
  165.             int falsehood = 0;
  166.             bool m = Convert.ToBoolean(truth);
  167.             bool n = Convert.ToBoolean(falsehood);
  168.             Console.WriteLine(m + " " + n);
  169.  
  170.             // ***TODO move this section to a later point
  171.             // How to check if an overflow occurs (For example, the code below overflows and the value is now 0 because a byte goes up to 255)
  172.             checked
  173.             {
  174.                 byte num = 255;
  175.                 num++; // "++" adds one to the current value, "--" subtracts one from the current value
  176.             }
  177.  
  178.             // C# Scope of Inheritance: The current block may inherit from the "parent" block, so an inheritance chain is possible - There are 3 blocks below
  179.             { // Block 1 (Blocks are contained within a {}, where "{" is the beginning and "}" is the end)
  180.                 int first = 1;
  181.                 Console.WriteLine(first + second + third); // The variables (vars) "second" and "third" are not inherited because they are "child" blocks
  182.                 { // Block 2
  183.                     int second = 2;
  184.                     Console.WriteLine(first + second + third); // The var "first" is inherited because Block 2 is a child of Block 1
  185.                     { // Block 3
  186.                         int third = 3;
  187.                         Console.WriteLine(first + second + third); // The vars "first" and "second" are inherited because their blocks, respectively,
  188.                     }
  189.                 }
  190.             }
  191.         }
  192.     }
  193. }
  194.  
  195. /* Congrats! You've done quite a bit.
  196.  * However, there's still a lot more to learn and mess around with; one of these things is classes.
  197.  * Take a break from this document and open up the "1. Classes" to learn more about how it all works.
  198.  
  199.  * [https://gist.github.com/Kotauror/8fd13d1590d935b4f1211a2d640c3142]
  200.  * [https://beginnerscsharp.wordpress.com/category/c-basics]
  201.  * [https://web.csulb.edu/~pnguyen/cecs475/notes/csharpbasic.pdf]
  202.  * [https://repl.it/languages/csharp]
  203.  * [http://zetcode.com/lang/csharp/basic]
  204.  * [https://www.tutorialspoint.com/csharp/csharp_quick_guide.htm]
  205.  * [https://ideone.com/Hz9vPG]
  206.  * [https://www.onlinegdb.com/online_csharp_compiler]
  207.  * [https://www.w3schools.com/cs]
  208.  * [https://docs.microsoft.com/en-us/dotnet/csharp]
  209.  * [https://www.tutorialspoint.com/csharp/index.htm]
  210.  * [https://csharp.net-tutorials.com]
  211.  * [https://www.sololearn.com/Course/CSharp]
  212.  * [https://www.geeksforgeeks.org/introduction-to-c-sharp]
  213.  * [https://www.cprogramming.com/tutorial/csharp.html]
  214.  * [https://www.csharp-examples.net]
  215.  * [https://sharplab.io]
  216.  * [https://csharp.today]
  217.  * [https://www.programiz.com/csharp-programming]
  218.  * [https://stride3d.net]
  219.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement