Basic Classes


In C#, everything is an object, made from a class. Classes are defined with curly brackets, like this:

class myClass{
}

The above example is a very basic class definition, containing no properties or methods. Now, we have a class, but how do we use it? The answer is create an instance of that class. You can do this with the new keyword, like this:

class myClass{
}
class MainClass{
  public static void Main(){
    myClass myObject = new myClass();
  }
}

Now, the class doesn't do anything, so why use it? Next, we will give the class some properties like this:

using System;
class myClass{
  public string test = "This works!";
}
class MainClass{
  public static void Main(){
    myClass myObject = new myClass();
    Console.WriteLine(myObject.test);
  }
}

The above code will print "This works!" to the screen. First, in myClass, we declare a string variable named "test" with the public modifier. Until you learn more advanced programming, you will have to use to public modifier in separate classes. Then we make an instance of myClass. Finally, we write out the value of test using the dot operator on the instance of myClass.

Exercise

Make a class car with the properties numTires = 4, year = 2000, and runs = true, and create three instances of it: car1, car2, and car3.