Skip to content
Permalink
ac61fd175b
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
54 lines (35 sloc) 2.99 KB

Classes

Overview

Classes are a way to organize your code into representations of real-world objects. C# is an object-oriented language, meaning that everything in the language ultimately is considered an object. This means that if you're writing a program focused on cars, you can define a class that represents the pieces of a car you're interested in. Classes primarily contain properties and methods. Properties store data about an object, i.e. the make of the car, model of the car, etc. Methods are functions that define what your objects can do. Your car object may contain a method called "Start" which starts the engine, or perhaps a method called "Accelerate" which increases its speed.

These classes are considerd "non-static" (as opposed to static, covered in the next section). This means that you must initialize them by using the "new" keyword, and you'll be given an instance of the class to work with. You can have multiple copies of this class, which is good if you need to represent a Honda Accord and a Toyota Camry.

Properties and methods inside your functions can be defined as either private (the default) or public. If you create your class with only private members, you will not be able to access any properties or methods from other classes (i.e. your Main function). By marking your properties as public, you can access them from any other class, making them much more useful to your programs. If you're unsure which to choose, ask yourself the question, "Do I need to use this property/method anywhere besides inside this specific class?"

Classes can be defined as public or internal, depending on how they need to be used. By default, classes are considered "internal" which means they are inaccessible to other assemblies. For example, there are classes defined in the System assembly that we cannot use in our programs because they are marked as internal to the System namespace. By explicitly marking a class as public, you allow other assemblies to use your classes. You will not be expected to know when to use internal vs public in this class.

Example

// Declare a public, non-static class to represent a car
class Car {

    // Public properties (accessible from outside of the Car class)
    public string Make;
    public string Model;
    public string Year;
    public double Speed;

    // Private property that can only be accessed from within this Car class.
    private string VINNumber;

    // Methods
    public void Accelerate()
    {
        Speed = Speed + 5;
    }
}

// Instantiate the class
Car myToyota = new Car();

// Set values on the instance
myToyota.Make = "Toyota";

Projects

EggHunt

ThanksgivingSearch

Person Generator Project

Car Inventory

Additional Information

Classes