Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // To backslashes for a single-line developer comment
- /*
- * This
- * is
- * a
- * multi
- * line
- * dev
- * comment
- */
- // C# Programming Structure https://imgur.com/kdMz5OK
- using System;
- namespace Basics
- {
- class Program
- {
- static void Main(string[] args) // Main() is ran first and automatically
- {
- // Console.OPERATION(ARGS) is the general context for "Console" functions
- Console.WriteLine("Hello World!"); // Make sure to always end statements with a semicolon ( ; )
- /* 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)
- *
- * Primitive Variable Types (Predefined) - https://imgur.com/EPxl0Md
- * Integral Numbers
- * byte
- * short
- * int
- * long
- * Real Numbers
- * float
- * double
- * decimal
- * Character
- * char
- * Boolean
- * bool
- *
- * Non-Primitive Variable Types (User Defined) - http://www.jeremyshanks.com/c-variables-primitive-nonprimitive-types/ (Non-primative is the second section)
- * string
- * array
- * enum
- * class
- * struct
- */
- // Creating variables
- var Number = 2;
- byte number = 3; // Notice caps. vs non-caps. (C# is very critical on capitalization)
- int count = 10;
- float totalPrice = 20.95;
- char character = 'A'; // Characters use single quotes (')
- string firstName = "Brandon"; // Strings use double quotes (")
- bool isWorking = false;
- // Printing variables to the console
- Console.WriteLine(Number);
- Console.WriteLine(number);
- Console.WriteLine(count);
- 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)
- Console.WriteLine(character);
- Console.WriteLine(firstName);
- Console.WriteLine(isWorking);
- /* Basic C# Operators:
- * Arithmetic
- * - Computation with numbers
- * +, -, *, /, %
- * ++, --
- * Comparison
- * - True/False when comparing numbers
- * ==, !=, >, >=, <, <=
- * Assignment
- * - To set equal to
- * =, +=, -=, *=, /=
- * Logical
- * - Compare/contrast for conditional statements
- * && (AND), || (OR), ! (NOT)
- * Bitwise
- * - Used in low level programming, such as encryption, so it will not be covered in this basic C# guide
- * & (and), | (or)
- */
- // Arithmetic Operations
- Console.WriteLine(2 + 3 * 10); // Prints "32" because order of operations (PEMDAS) is followed
- // Comparison Operations
- Console.WriteLine(6 == 2 * 3); // Prints true becuase 6 is equal to 6
- Console.WriteLine(17 != 7); // Prints true because 17 is not equal to 7
- Console.WriteLine(9 <= 5); // Prints false because 9 is not less than or equal to 5
- // Assignment Operations
- int starfruit = 9; // The variable "starfruit" is assigned the value 9
- starfruit += 2; // The var now equals 11 because 9 + 2 = 11
- starfruit -= 8; // Var is now 3 because 11 - 8 = 3
- starfruit *= 2; // Var is now 6 because 3 * 2 = 6
- starfruit /= 6; // Var is now 1 because 6 / 6 = 1
- Console.WriteLine(starfruit == 1); // If it prints "True", then we know the assignment operators worked as planned
- // Logical Operations
- Console.WriteLine(!(true && false || true)); // Prints false
- // Interpolating strings
- Console.WriteLine("{0} {1}", byte.MinValue, byte.MaxValue);
- Console.WriteLine("{0} {1}", float.MinValue, float.MaxValue);
- // Constant (const) variables cannot change once declared
- const float Pi = 3.14f;
- Pi = 1;
- // Implicit type conversion - types are compatible
- byte b = 1; // 00000001
- int i = b; // 00000000 00000000 00000000 00000001
- long l = i; // Types can be converted going up (implicitly) because there is no chance of data loss
- // Explicit type conversion - types are not compatible
- i = l; // C# is preventing possible data loss
- i = (int)l; // Forcing the conversion
- // Non-compatible type conversion
- string s = "1";
- i = (int)s; // Primitive and non-primitive types don't like being converted
- i = int.Parse(s); // Parse only works with primitive types
- i = Convert.ToInt32(s); // Requires system namespace
- /* Using Convert.*();
- *
- * ToByte() converts to a byte
- * ToInt16() converts to a short
- * ToInt32() converts to an int
- * ToInt64() converts to a long
- * ToBoolean() converts to boolean (be careful)
- */
- // The problem with forcing conversions
- var stringA = "1234";
- byte byteA = Convert.ToByte(number);
- Console.WriteLine(b); // This crashes because we didn't handle the exception
- try // We can handle the exception by using a try-catch method
- {
- var stringB = "1234"; // This code in the "try" block won't be ran if an error occurs
- byte byteB = Convert.ToByte(number);
- Console.WriteLine(b);
- }
- catch (Exception) // This will be ran if the "try" block of code outputs an error
- {
- Console.WriteLine("The number could not be converted to a byte.");
- }
- // Special cases with conversions
- char apple = 'a';
- int amount = (int)apple;
- Console.WriteLine(amount);
- bool response = false;
- int value = Convert.ToInt32(response);
- 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)
- int truth = 1; // Another boolean example
- int falsehood = 0;
- bool m = Convert.ToBoolean(truth);
- bool n = Convert.ToBoolean(falsehood);
- Console.WriteLine(m + " " + n);
- // ***TODO move this section to a later point
- // 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)
- checked
- {
- byte num = 255;
- num++; // "++" adds one to the current value, "--" subtracts one from the current value
- }
- // C# Scope of Inheritance: The current block may inherit from the "parent" block, so an inheritance chain is possible - There are 3 blocks below
- { // Block 1 (Blocks are contained within a {}, where "{" is the beginning and "}" is the end)
- int first = 1;
- Console.WriteLine(first + second + third); // The variables (vars) "second" and "third" are not inherited because they are "child" blocks
- { // Block 2
- int second = 2;
- Console.WriteLine(first + second + third); // The var "first" is inherited because Block 2 is a child of Block 1
- { // Block 3
- int third = 3;
- Console.WriteLine(first + second + third); // The vars "first" and "second" are inherited because their blocks, respectively,
- }
- }
- }
- }
- }
- }
- /* Congrats! You've done quite a bit.
- * However, there's still a lot more to learn and mess around with; one of these things is classes.
- * Take a break from this document and open up the "1. Classes" to learn more about how it all works.
- * [https://gist.github.com/Kotauror/8fd13d1590d935b4f1211a2d640c3142]
- * [https://beginnerscsharp.wordpress.com/category/c-basics]
- * [https://web.csulb.edu/~pnguyen/cecs475/notes/csharpbasic.pdf]
- * [https://repl.it/languages/csharp]
- * [http://zetcode.com/lang/csharp/basic]
- * [https://www.tutorialspoint.com/csharp/csharp_quick_guide.htm]
- * [https://ideone.com/Hz9vPG]
- * [https://www.onlinegdb.com/online_csharp_compiler]
- * [https://www.w3schools.com/cs]
- * [https://docs.microsoft.com/en-us/dotnet/csharp]
- * [https://www.tutorialspoint.com/csharp/index.htm]
- * [https://csharp.net-tutorials.com]
- * [https://www.sololearn.com/Course/CSharp]
- * [https://www.geeksforgeeks.org/introduction-to-c-sharp]
- * [https://www.cprogramming.com/tutorial/csharp.html]
- * [https://www.csharp-examples.net]
- * [https://sharplab.io]
- * [https://csharp.today]
- * [https://www.programiz.com/csharp-programming]
- * [https://stride3d.net]
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement