Posts

Showing posts from July, 2022

15. C# - Structs

Structs hold values and can have constructors (a thing that takes input and changes default value of a struct). Struct is value type, class is a reference type. They are somewhat similar except that difference. Creating a basic struct: struct Human {          public String Name;          public Int32 Age; } Initializing a human struct with a variable: Human Todd; Todd.Name = "Todd"; Todd.Age = 32; Creating a struct with constructors: struct Human {         public String Name;         public Int32 Age;          public Human(String N, Int32 A) {                    Name = N;                    Age = A;           } } Initializing a human struct with variable using constructors: Human Todd = new Human("Todd",32);

14. C# - Main Method

Main method is entry point for console and windows apps. Allows running code within a Main() function automatically. Main method/function must be inside a class. Example: structs or whatever above class Program {          static void Main() {               whatever here           } } // using main method can get by the top level statements error. Types of Main methods: //parameterless Main() methods public static void Main ( ) { } public static int Main ( ) { } public static async Task Main ( ) { } public static async Task < int > Main ( ) { } //Main() methods with string[] parameter public static void Main ( string [ ] args ) { } public static int Main ( string [ ] args ) { } public static async Task Main ( string [ ] args ) { } public static async Task < int > Main ( string [ ] args ) { }

13. C# - Public Access Modifier

 Access modifier allows or restricts access to types or variables etc. Adding a "public" in front of a type,method,statement will let it be access outside of the scope it is within. Format: [ACCESS MODIFIER] [TYPE] [VARIABLE_NAME] Example for Variable: public String Dog; Dog = "Benny"; Example for Methods/Functions: public void MethodName(){     whatever inside } // methods/functions need to return a value. If dont need to return, then add "void" in front of method's name.

12. C# - Namespaces

Namespaces are like folders on a computer. They help organize or put similar code in one area. Not necessary to have a namespace, but can help with organization. Things inside a namespace, like a variable, statement, or method, must be grouped inside a class first. Creating a namespace: namespace MyNameSpace{     whatever in here, cant be Fields, methods, or statements. Meaning if want to put a variable, it must be placed inside a class first. } Nesting namespace: // can nest namespaces inside a namespace inside more namespaces. namespace NameSpaceA{          namespace NameSpaceB{                    whatever here           } } Accessing things within a namespace: Create a sample namespace, namespace SampleNameSpace{          String dog = "Benny"; } Printing the dog, Console.WriteLine(SampleNameSpace.dog);

11. C# - Classes

Classes are like structs. Classes can hold functions, variables, things. To use classes in other namespaces, need to declare it as public in front. EX: public class MyClassName {} Creating a class: class MyClassName {     void MyFunction(){           Console.WriteLine("Hello World");      } } // this sample class above just has one function that prints Hello World. Initiating a variable to be a class: MyClassName myClassA = new MyClassName(); Initiating a variable to be a class if class is within a namespace: NameSpaceName.MyClassName myClassA = new NameSpaceName.MyClassName(); Class Constructors: Classes can have constructors like a struct. Weird too, since you can just use structs with constructors instead of a class constructor. Example: class Human{          public String Name;          public Int16 Age;          public Program(String N, Int16 A) {                    Name = N;                    Age = A;           }          static void Main() {                    Human T