public class Lesson4_Classes

{

void WeNeedMoreClass()

{

While it’s nice to see all of your code in one place, as programs get larger, organization can become a hassle with long files. Just as we split up our Program class into multiple methods, we can also split up our code into multiple class files.

Actually, a class is just another OOP encapsulation, and multiple classes could live in the same file.

 

This allows me to show you two classes functioning in dotnetfiddle (which is limited to one file for console apps), but it mostly defeats the purpose of separating the code into classes. Traditionally for larger projects, classes are saved each to their own file. In VS or VS Code, you can manage these files just like any other file-based application. Right-click over the Explorer sidebar and choose New C# Class from the dropdown.

 
Once you’ve named your new file, it should open for you in the editor, and you will see tabs at the top to switch between files.

 

Note that although the classes are in separate files, as long as the Namespace at the top of each file is the same, and they are in the same folder, they will have no trouble communicating with each other.

}
 

void InstantiatingAClass()

{

Once you have created a class file, in order to access the code in that file from another class, such as Program, you must instantiate, or create, an object of that class type. The class itself is a description, but you could have as many objects created with that type as you wish.

To instantiate an object, you use the new statement, and save the result to a variable.

 

var classObject = new ExtraClassy();

 

Then, to access features (methods, properties) on that object, use the dot syntax.

 

classObject.ClassyString = "Set the string";

 
}
 

void Constructors()

{

In the example above, the parentheses following ExtraClassy represent an empty Constructor. Every class has an empty constructor by default. However, sometimes we want to pass in parameters when instantiating an object, just like we pass data to methods.

 

Notice that a constructor, unlike a method, has no return type, and its name is the same as that of the class.

}
 

void Properties()

{

Above, you’ve seen several examples of a new item called a property. Properties are designed as accessible variables that can be set or get (retrieved) from outside the class. This is in contrast to fields, which are used primarily within a class.

A property always requires either a getter or setter, and can have both as well. An auto-property is one with the simplest form of these accessors, { get; set; }. This tells us that the property can be simply written to and read from. Sometimes, more complex logic might be used in either the get or set block, in which case they are encapsulated in curly braces and include a return like a method. These properties are often paired with a private field, which actually stores the data.

}
 

void DesignPrincipals()

{

Many amazing books and blog posts have been written about Object-Oriented Design principals, and it is not the purpose of this site to make you an expert in these. However, one simple design principal I have found extremely useful is to separate my classes into two groups: Data Transfer Objects (DTOs) and Business Logic classes. The examples on this page so far have been DTOs. That is, they exist primarily as a repository to store, transfer, and retrieve data, in the form of properties.

A Business Logic class, on the other hand, is one that does something, in the form of methods. In a short program, this might be all handled by the Program class, but it should be separated out into other classes for more complex designs.

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *