Wednesday, 23 April 2014

Beginner's C# Course, Part 3

The basics of C#: Static Classes

What is a static class?
A static class is very similar to a non-static class, except for one single difference; you do not instantiate static classes. This means they do not have a constructor.

If you recall back a whole 4 days to our last beginner's C# course post on constructors and classes, I told you that to instantiate a class, you did something along the lines of:

ClassName VariableName = new ClassName(arg1, arg2);

let's dissect this a little.

The first half, before the = is us creating a new variable.

We're defining it's type (ClassName) and giving it a name (VariableName)
It's no different to this:

int i;

The second half is the actual instantiation.

We are giving the Variable called "VariableName", which currently has no value (We've told it what TYPE of value it can have, but not actually given it one yet), a value equal to "new ClassName(arg1, arg2)"

"new" means we're creating a new instance of that class for the value of this variable, ClassName is simply saying what we're creating a new something of, and "(arg1, arg2)"  are simply the arguments required by the classes constructor (See name and age in the student class from the previous part of the course)

With static classes, we don't need to do this.

Static classes are usually used to store functions that are used multiple times in multiple classes.

For example

##File Start##
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//These are some default using statements put in whenever you make a new class

namespace ExampleProgram
//Here's our namespace
    static class Functions
    //Here's our static class
        public static int Add(int a, int b)
        //Note that all methods in a static class must be static.
            return a + b;
            //Yes, all it does is add 2 numbers together
##File End##

Simple enough?
To call the "Add" method in another class we would simply do:

Functions.Add(1, 7);

However, remember that we put "int" rather than void for this method, so what does that mean?
Simple, it means the function returns an int value.

So we could do something like this:

If(Functions.Add(1, 7) == Functions.Add(2, 6))
    Console.WriteLine("The world is functioning correctly");

Which basically says "If 1 + 7 gives the same answer as 2 + 6, the world is working normally"

If you look back to my last post, the first part of the sourcecode posts, I actually used "InitLoad.PreLoading()" (which returns true or false) in an if statement.

So when would you use static, and when wouldn't you?

Well, let's say, for example, you were writing a game.
In this game, you want enemies right?
Okay, make an enemies class (non-static) that stores health, damage, speed etc along with some functions such as OnDamage (a function that lowers the enemy's health when it gets hurt)
Now you can do something like this for the actual enemies:

Enemy baddie1 = new Enemy(argshere);
Enemy baddie2 = new Enemy(argshere);
Enemy baddie3 = new Enemy(argshere);

We have now created 3 separate instances of the Enemy class, if one takes damage, the others are unaffected.


Now baddie2 has less hp  left than baddie1 and baddie3.

However, note that you cannot do:


Because the class itself has no health, only instances of the class, so you can only damage individual instances of the class.
The same applies to ANY non static method within a non static class, you MUST instantiate it and call the method on the instance, rather than the class.

This is where Static classes have the advantage when you just want to call general functions (Functions in a static class usually take 1 or more arguments and return something). Static methods in static classes don't need an instance to be called.

Also note that you can put static methods in non-static classes, and call them without an instance, but usually you wouldn't need to.

Anywho, that's it for today's lesson. Bit longer than usual.

On friday we will cover Partial classes and Extending classes.

No comments:

Post a Comment