Course Home | What are Classes and Objects? | Code Example(s)
With C# being an Object-orientated programming (OOP/OO) language, classes and objects are what it’s all about. Everything when coding with C# is an object.
Within VS, it’s very easy to create a new class, in the solutions window we right-click where we want the class to be and click Add > New File, and we want to add an empty class. VS also gives us the option to give our class a name. As mentioned in the Basic Programming course an object is an instance of a class. So we need to give the class a name that will make sense when using it in our code.
Here is the bare minimum we need to create a new class
public class MyClass
{
//Make our class do things here
}
We can use this class in our other classes by creating a new instance of it. We do this using the new keyword.
public class MySecondClass
{
MyClass instanceOfMyClass = new MyClass();
}
We can then use that instance, which we call an object, in our code. In a complete solution, it will have methods and values I wish to use.
As you start writing more code in C# you’ll most certainly create many classes, and you’ll also use lots of objects from libraries that you add to your project.
Comments