Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Struct Methods | Structs & Enumerators
course content

Зміст курсу

C# Beyond Basics

Struct MethodsStruct Methods

A structure can also have methods apart from data fields. In a Struct, we create a method similar to how we create a method in general:

cs

index.cs

For-example:

cs

index.cs

In the Player structure we have a method called displayScore which displays a formatted output message containing the name and score. It is important to note that the name and score values will be different for each object (instance), which means that the displayScore method will show a different output for each object.

We can access and execute the method using the variableName.methodName() syntax. Let's look at how we will use the Player structure:

cs

index.cs

In the example, two objects were created using the Player class and some data was assigned to them. The displayScore method was called using each object and the output turned out to be different for each of them.

We can also have methods with some parameters. Let's add a new method called "addScore" which increases the score by a specified amount and try using it:

cs

index.cs

You can see from the output that the method updates the score by 70 points.

1. Which is a valid keyword for declaring a method?
2. What will be the output of the following code?

Which is a valid keyword for declaring a method?

Виберіть правильну відповідь

What will be the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 5
course content

Зміст курсу

C# Beyond Basics

Struct MethodsStruct Methods

A structure can also have methods apart from data fields. In a Struct, we create a method similar to how we create a method in general:

cs

index.cs

For-example:

cs

index.cs

In the Player structure we have a method called displayScore which displays a formatted output message containing the name and score. It is important to note that the name and score values will be different for each object (instance), which means that the displayScore method will show a different output for each object.

We can access and execute the method using the variableName.methodName() syntax. Let's look at how we will use the Player structure:

cs

index.cs

In the example, two objects were created using the Player class and some data was assigned to them. The displayScore method was called using each object and the output turned out to be different for each of them.

We can also have methods with some parameters. Let's add a new method called "addScore" which increases the score by a specified amount and try using it:

cs

index.cs

You can see from the output that the method updates the score by 70 points.

1. Which is a valid keyword for declaring a method?
2. What will be the output of the following code?

Which is a valid keyword for declaring a method?

Виберіть правильну відповідь

What will be the output of the following code?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 5
some-alt