Objects and Methods, a Primer
There is a lot to say about objects in C# that we’ll cover in due time. Here, we highlight some introductory concepts of how C# works as an object-oriented programming language.
Objects
In C#, objects are structures that have a state and a set of behaviors. The state of an object includes properties/data that the coder can define and modify. Behaviors are actions that run when requested, and they can be used to evaluate, manipulate, or return data.
As we’ve said several times now, every variable in C# refers to an object.
The string
data type is an object. For
string language = "C#"
, the data would be the characters. The
String Manipulation section gives several of the
behaviors available to the language
object. For example,
language.Length
is 2
, which tells us how many
characters are present in the string.
An array is also an example of an object. It contains data, which are the values stored as the individual elements. The behaviors are methods not unlike those listed in the string manipulation table that perform actions related to the elements in the array. We haven’t provided an array methods table, but you can explore array methods in the docs .
Static Methods
In pure object-oriented languages like C# and Java, we don’t have functions in the sense you may be used to. Functions may not be declared outside of a class. Within the context of a class, functions are referred to as methods. We’ll adopt this terminology from now on.
We’ll dive into learning about classes and objects in C# soon enough, but first let’s learn a little about static methods, which behave somewhat similarly to stand-alone functions. A static method is one that can be called without creating an object instance of the class to which it belongs.
Define the class Cat
and include the static
keyword before the
makeNoise
method name:
|
|
Since makeNoise
is static
, we do NOT need to create a Cat
object to
access it.
Instead of doing this:
Cat myCat = new Cat(); // Create a new Cat object.
myCat.MakeNoise("purr"); // Call the MakeNoise method.
We can call the method directly:
Cat.MakeNoise("roar");
Until we get further into object oriented programming, every method you write
should use the static
keyword. Leaving off static
will prevent or
complicate the process of calling the methods you defined.
We will explore exactly what static
does in more detail in later lessons.
HelloMethods
Let’s examine two classes in C# to explore defining and using methods. Open the
HelloMethods
project in csharp-web-development-examples
.
The first class is defined in the HelloMethods/Program.cs
file. The second class is defined in a separate HelloMethods/Message.cs
file, and it contains a GetMessage
method that we want to call from within
Main
.
HelloMethods/Program.cs
:
|
|
HelloMethods/Message.cs
:
|
|
We won’t explore every new aspect of this example, but rather will focus
on the GetMessage()
method.
- Take a look at the
Message
class. It has one method that isn’t called within the class. Code within theMessage
class must be called from elsewhere in order to execute. - The
Message
class contains theGetMessage()
method. It has thestatic
keyword and a return type ofstring
. GetMessage()
takes a singlestring
parameter,lang
.
Since C# is statically typed, each method must declare its return type -
that is, the data type of what it will return - along with the type of
each parameter. One consequence of this that may not be immediately
obvious is that methods in C# may not return different types of data.
For example, we would not be able to replace the last return
statement of GetMessage()
with something like return 42;
. This
would be flagged as a compiler error.
Main()
Methods
After Microsoft released .NET 6, the Program.cs
class looked different. Prior to this release, Program.cs
contained a Main()
method and code was written inside the Main()
method. Now at the time of compilation, your code in Program.cs
is synthesized with a Main()
method. This is important to know because in a C# project, only one Main
method is allowed. When the project is compiled and run, the Main
method indicates what should be executed,
and if there were multiple Main
methods this would be ambiguous. What you write in Program.cs
becomes the body of the Main()
method for the project.
To get a sense for this, let’s revisit the code in Program.cs
for the TempConverter project.
|
|
In older C# code, with the Main()
method made available to the developer, the same Program.cs
file would look like:
|
|
Public Methods
Finally, let’s note how a static method is called. The first line of the Program
class is:
Message.GetMessage("fr");
To call a static method, we must use the name of the class in which it is
defined, followed by .
, followed by the name of the method.
ClassName.methodName(arguments);
We are able to call this method from another class because it is
declared to be public
. If we wanted to restrict the method from
being called by another class, we could instead use the private
modifier. We’ll explore access modifiers in more depth in coming
lessons.
As you have been following along with this example, you may have noticed
that the class file, Message.cs
, is named exactly the same as the class it holds.
There is NOT a rule in C# dictating that a file must be named the same as the class it contains, but it is considered best practice.
Try It
Poke around with the HelloMethods
project in Visual Studio and experiment with the
following:
- Figure out how to alter the
HelloMethods
code to change the message returned. - Add another “Hello, World” language option.
- Change one
public
keyword toprivate
to see what happens. Repeat for each occurrence ofpublic
.
Check Your Understanding
Which of the following defines a method that takes an integer as a parameter and returns a string value?
public static void MethodName(string parameterName)
public static void MethodName(int parameterName)
public static int MethodName(string parameterName)
public static string MethodName(int parameterName)
True/False: A C# project may contain more than one Main
method, as long as at least one of those methods is marked private
.