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 Todd = new Human("Todd", 32);

                Console.WriteLine(Todd.Name);

        }

}

Comments

Popular posts from this blog

2. FreeCodeCamp - Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges

20. Data Analytics - Analyze Data to Answer Questions - Week 1

3. Algorithms - Selection Sort