Classes for C#
In previous programming studies, we have come across classes and objects. Classes and objects in C# are similar to classes and objects in other languages.
A Minimal Class and Object
Classes may contain fields and methods. Fields contain the data of a class and methods define actions a class can take. We say that fields and methods are members of a class.
Let’s create a class called HelloWorld
with one field, message
, and one method, SayHello()
.
message
will be a string and have a value of "Hello World"
.
SayHello()
will not return a specific value and instead print out the value of message
.
|
|
The only field in the HelloWorld
class is the string message
, while the
only method is SayHello()
, which prints the value of the message
field
and doesn’t return anything.
There is no main
method, which is required to run a C# program.
Without it, we have to do some additional work to get our program to run!
To execute SayHello()
, we’ll need to create an instance of the
class HelloWorld
. We refer to an object created from a particular class as
an instance of that class.
Here’s how this might look with our HelloWorld
class:
|
|
In order to call the SayHello()
method of HelloWorld
, we must
first have an instance of HelloWorld
, which we create using the
syntax new HelloWorld()
. As with built-in classes, classes that we
create define their own types. So the object hello
is a variable of
type HelloWorld
.
We introduced this HelloWorld
class as a means of illustrating the simplest
representation of some basic concepts in C#. The goal of the next few
lessons is to build up the machinery to create a wide variety of
interesting classes that can be used to create complex programs and
elegantly solve difficult problems.
The this
Keyword
In HelloWorld
above, we could have written SayHello
this way,
with the same net effect:
|
|
In this context, inside of the class, we can refer to fields (and
methods) that belong to the class using the special object, this
.
Whenever you use this
, it always refers to the object that the
given code is currently within. In other words, this
will always be
an instance of the given class. Since it is not legal to create code
outside of a class in C#, this
nearly always makes sense to use
(there’s one exception, that we’ll encounter soon).
You are allowed to create local variables (variables declared
within a method) with the same name as a field of the given class. In
this case, in order to refer to the field, we must use this
.
Let’s look at how this works with our HelloWorld
class:
|
|
When a local variable has the same name as a field, we say that the local variable shadows the field. Errors caused by shadowing can be tricky to spot, so it’s best to avoid doing this in your code.
If you want to learn more about this subject, check out the documentation on using the this keyword .
Check Your Understanding
The following code block contains several bugs. Mark all of the lines that contain a bug in the code.
|
|
- line 9
- line 4
- line 8
- line 1