FANDOM


Overview Edit

Creating classes is very useful in C#. They allow you to store the current state, as well as use special methods and reuse code.

Our example will be a Person class. We'll need to create properties, methods, and a constructor.
Here is the class creation:

class Person
{
}

Requirements Edit

Knowledge of:

  • Strings, ints, and chars
  • Integer operations

The Constructor (first glance) Edit

Now, let's add a constuctor. A constructor is a special method that is called when a class is instantiated. You can instantiate a class like this:

Class class = new Class();

Let's take a look at the constructor and what it does.

class Person
{
     public Person (args)
     {
     }
}

Now, there is a part that says "args". This is for the parameters (or arguments) for the Person class. Just like every other method, the constructor can take parameters.
You may have been confised with the name of the constructor. Its simply public <classname>.

Fields and Properties Edit

Now, what are some variables, or fields, that we should have for our class? First, a name. This will be a string. Then perhaps a last name as well, and a middle initial. Last name will be a string, and the middle initial will be a char (like a string, but stores exactly one character). There should also be an age, which will be an int (it could by a bute instead, but we aren't worrying about memory when working with actual desktop or laptop computers). Time to list the fields!

class Person 
{ 
     string firstName;
     char middleInitial;
     string lastName;
     int age;
     public Person ()
     {
     }
}

Okay, now we have that done. But what if we want our fields to be publicly accessable? They need to be properties. By default, all the fields above are private. But properties are public. How do we do this?
Simply add "public" before the type! It's that easy.
But, say we don't want anyone outside the Person class editing the age. How do we stop this?
Type this:

public int age { get; }

The get accessor only allows outside classes to get the value of "age". You can add set after "get" as well, allowing others to set the age.

There is another way to create properties: The prop shortcut. Visual Studio shortcuts allow for users to access code snippets for easier writing. The code snippets often include info that is usually written, and make it easier to type code. Type "prop" then hit tab twice. Using tab, switch between the different values you must put in: the variable type and property name. Hit enter to exit this behavior and unhighlight the yellow words, and hit enter again to go to a new line.

Methods Edit

Okay, now we want some methods! Let's have an "IncreaseAge" method to increase the age of the Person.
Methods can be public, private, protected, internal, and protected internal. We'll only talk about public and private today; the others wilm be used with inheritence and advanced topics.

Public Edit

Public means that anyone outside of the class can call the method, as long as there is an instance of the class. Here's how to create a public method:

public returntype methodname(args)
{
}

Private Edit

Private methods, then, can only be used from within the class:

private returntype methodname (args)
{
}

Writing the Method Edit

Now let's write the method. Methods, like constructors, can have arguments. We'll have an int for the amount to increase the age by. Also, you might be wondering: What's the returntype mean? For now, we'll use void, and discuss returntype in an advanced method-writing tutorial later. Now, here's the final method:

public void AddAge (int age)
{
     this.age += age;
}

You may have noticed the "this" keyword. Since the field, age, and the parameter, age, have the same name, we use this to access the class' field.

Return to the Constructor Edit

Now we need to write out constuctor. First, we need to know a few things:

  1. What variables does the class have?
  2. What does the class need to do to prepare itself for use?

Now that we know thw answers to these questions, we can write our constructor:

public Person (string firstName, char middleInitial, string lastName)
{
     this.firstName = firstName;
     this.lastName = lastName;
     this.middleInitial = middleInitial;
}

And we're done!

Full Class Edit

The full class is below:

class Person
{
    string firstName;
    string lastName;
    char middleInitial;
    int age;
    public Person (string firstName, char middleInitial, string lastName)
    {
         this.firstName = firstName;
         this.middleInitial = middleInitial; 
         this.lastName = lastName;
    }
 
    public void AddAge (int age)
    {
         this.age += age;
    }
}

Alright, now we have our first class!

See Also Edit