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 fields, 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 a field 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.

Partial Classes and Partial Types:

 The declaration of a class can be partitioned among several partial class declarations.

• Each of the partial class declarations contains the declarations of some of the class members.

• The partial class declarations of a class can be in the same file or in different files. Each partial declaration must be labeled as partial class, in contrast to the single keyword class.

The declaration of a partial class looks the same as the declaration of a normal class, other than the addition of the type modifier partial.

    partial class MyPartialClass {

         int w  // member1 declaration
         int x  // member2 declaration
    }
    partial class MyPartialClass {

         int y  // member1 declaration
         int z  // member2 declaration
    }

Exercise

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


Copyright © learncs.org. Read our Terms of Use and Privacy Policy