Exercise – Call the methods of a .NET Class
Whether you realized it or not, you’ve been calling C# methods ever since your first “Hello, World!” application. That application uses the WriteLine()
method of the Console
class to display the “Hello, World!” message.
However, not all classes and methods are implemented the same way. This unit covers some of the most common variants that you’ll need to understand when using methods from the .NET Class Library. More importantly, you’ll learn how to find and use the documentation to better understand more about each method.
How to call methods in the .NET Class Library
From your previous experience with the Console.WriteLine()
method, you should already know the basics:
- Start by typing the class name. In this case, the class name is
Console
. - Add the member access operator, the
.
symbol. - Add the method’s name. In this case, the method’s name is
WriteLine
. - Add the method invocation operator, which is a set of parentheses
()
. - Finally, specify the arguments that are passed to the method, if there are any, between the parentheses of the method invocation operator. In this case, you specify the text that you want the
Console.WriteLine()
method to write to the console (for example,"Hello World!"
).
Optionally, depending on how the developers designed and implemented the given method, you may also need to:
- Pass additional values as input parameters.
- Accept a return value.
In the next unit, you’ll examine how to pass input values to a method, and how a method can be used to return a value to the calling routine.
While some methods can be called the same way that you called Console.WriteLine()
, there are other methods in the .NET Class Library that require a different approach.