Created
September 29, 2023 18:07
-
-
Save lifeinthegrid/fe38d7c38aaac8a6051f0953d1971e0f to your computer and use it in GitHub Desktop.
GridLife.io-Learn-Tutorial-CSharp30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// // See https://aka.ms/new-console-template for more information | |
// Console.WriteLine("Hello, World! From C#"); | |
// //========================= | |
// //COMMENTS | |
// //Single Line Comments | |
// /* | |
// Multiple line comments | |
// */ | |
// //========================= | |
// //VARIABLES - PRIMITIVE (except for string) | |
// byte myByte = 100; | |
// int five = 5; | |
// double onepointone = 1.1; | |
// bool ImTrue = true; | |
// char myChar = 'X'; | |
// string myString = "A Set of Characters"; //Not a reference type | |
// Console.WriteLine($"Hello here are my primitive variable types {myByte} {five}, {onepointone}, {ImTrue}, {myChar}, {myString}"); | |
// //========================= | |
// //OPERATORS: | |
// //Arithmetic | |
// int resultAddition = 5 + 3; // resultAddition is 8 | |
// int resultSubtraction = 10 - 4; // resultSubtraction is 6 | |
// int resultMultiplication = 7 * 6; // resultMultiplication is 42 | |
// Console.WriteLine($"Addition: {resultAddition}"); | |
// Console.WriteLine($"Subtraction: {resultSubtraction}"); | |
// Console.WriteLine($"Multiplication: {resultMultiplication}"); | |
// // Assignment | |
// int value = 10; | |
// int resultAssignment = value; // resultAssignment is 10 | |
// value += 5; // value is now 15 | |
// value -= 3; // value is now 12 | |
// Console.WriteLine($"Assignment: {resultAssignment}"); | |
// Console.WriteLine($"Addition Assignment: {value}"); | |
// Console.WriteLine($"Subtraction Assignment: {value}"); | |
// // Comparison | |
// int a = 10; | |
// int b = 5; | |
// bool isEqual = (a == b); // isEqual is false | |
// bool isNotEqual = (a != b); // isNotEqual is true | |
// Console.WriteLine($"Equality: {isEqual}"); | |
// Console.WriteLine($"Inequality: {isNotEqual}"); | |
//========================= | |
//STRUCTS: | |
// Structs help us improve how we organize data | |
// Instead of this we can use a struct | |
// int PointX = 5; | |
// int PointY = 5; | |
// public struct Point | |
// { | |
// public int X; | |
// public int Y; | |
// } | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// Point myPoint = new Point(); // Create a Point struct and initialize its fields | |
// myPoint.X = 10; | |
// myPoint.Y = 20; | |
// // Write the data from the Point struct to the console | |
// Console.WriteLine($"X: {myPoint.X}, Y: {myPoint.Y}"); | |
// } | |
// } | |
//========================= | |
//REF-TYPES: CLASSES | |
// Define a simple class | |
// class Person | |
// { | |
// // Fields | |
// public string Name; | |
// public int Age; | |
// // Method | |
// public void SayHello(string personA) | |
// { | |
// Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old."); | |
// } | |
// } | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// // Create an instance of the Person class | |
// Person person1 = new Person(); | |
// person1.Name = "Alice"; | |
// person1.Age = 30; | |
// Person person2 = new Person(); | |
// person2.Name = "Cory"; | |
// person2.Age = 48; | |
// // Call the SayHello method | |
// person1.SayHello('Cory'); | |
// } | |
// } | |
//========================= | |
//REF-TYPES: INTERFACE | |
// interface IDrawable | |
// { | |
// void Draw(); | |
// void Foo(); | |
// } | |
// // Implement the interface in a class | |
// class Circle : IDrawable | |
// { | |
// public void Draw() | |
// { | |
// Console.WriteLine("Drawing a circle."); | |
// } | |
// public void Foo() | |
// { | |
// Console.WriteLine("Drawing a circle."); | |
// } | |
// } | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// // Create an instance of the Circle class | |
// Circle circle = new Circle(); | |
// // Call the Draw method defined in the interface | |
// circle.Draw(); | |
// } | |
// } | |
//========================= | |
//REF-TYPES: DELEGATE | |
// Define a delegate type | |
// delegate void MyDelegate(string message); | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// // Create an instance of the delegate | |
// MyDelegate delegateInstance = DisplayMessage; | |
// // Call the delegate | |
// delegateInstance("Hello from the delegate!"); | |
// } | |
// // Define a method with the same signature as the delegate | |
// static void DisplayMessage(string message) | |
// { | |
// Console.WriteLine(message); | |
// } | |
// } | |
//========================= | |
//REF-TYPES: ARRAY | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// // Create an array of integers | |
// int[] numbers = { 1, 2, 3, 4, 5 }; | |
// string[] strings = { "test", "foo" }; | |
// // Access and display array elements | |
// Console.WriteLine("Array Elements:"); | |
// foreach (string s in strings) | |
// { | |
// Console.WriteLine(s); | |
// } | |
// } | |
// } | |
//========================= | |
//CONTROL STRUCTURE: IF | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// int num = 42; | |
// // Using an if statement to check a condition | |
// if (num > 50) | |
// { | |
// Console.WriteLine("The number is greater than 50."); | |
// } | |
// else if (num == 50 && num > 49) | |
// { | |
// Console.WriteLine("The number is equal to 50."); | |
// } | |
// else | |
// { | |
// Console.WriteLine("The number is less than 50."); | |
// } | |
// } | |
// } | |
//========================= | |
//CONTROL STRUCTURE: SWITCH | |
// class Program | |
// { | |
// static void Main() | |
// { | |
// int dayOfWeek = 15; | |
// string dayName; | |
// // Using a switch statement to match a value | |
// switch (dayOfWeek) | |
// { | |
// case 1: | |
// dayName = "Monday"; | |
// break; | |
// case 2: | |
// dayName = "Tuesday"; | |
// break; | |
// case 3: | |
// dayName = "Wednesday"; | |
// break; | |
// case 4: | |
// dayName = "Thursday"; | |
// break; | |
// case 5: | |
// dayName = "Friday"; | |
// break; | |
// default: | |
// dayName = "Weekend"; | |
// break; | |
// } | |
// Console.WriteLine($"Today is {dayName}."); | |
// } | |
// } | |
//========================= | |
//LOOP STRUCTURES: | |
class Program | |
{ | |
static void Main() | |
{ | |
// for loop | |
Console.WriteLine("Using a for loop:"); | |
for (int i = 0; i < 5; i++) | |
{ | |
Console.WriteLine($"Iteration {i + 1}"); | |
} | |
// while loop | |
Console.WriteLine("\nUsing a while loop:"); | |
int count = 0; | |
while (count < 3) | |
{ | |
Console.WriteLine($"Count: {count}"); | |
count++; | |
} | |
// do-while loop | |
Console.WriteLine("\nUsing a do-while loop:"); | |
int x = 5; | |
do | |
{ | |
Console.WriteLine($"Value of x: {x}"); | |
x--; | |
} | |
while (x > 0); | |
// foreach loop | |
Console.WriteLine("\nUsing a foreach loop:"); | |
int[] numbers = { 1, 2, 3, 4, 5 }; | |
foreach (int number in numbers) | |
{ | |
Console.WriteLine($"Number: {number}"); | |
} | |
// break and continue | |
Console.WriteLine("\nUsing break and continue:"); | |
for (int i = 0; i < 5; i++) | |
{ | |
if (i == 2) | |
{ | |
Console.WriteLine("Skipping iteration 2"); | |
continue; | |
} | |
if (i == 4) | |
{ | |
Console.WriteLine("Breaking loop at iteration 4"); | |
break; | |
} | |
Console.WriteLine($"Iteration {i + 1}"); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use this file simply uncomment each class Program { ... } section and build and run your project.
Additional Information can be found here: gridlife.io/csharp30