Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Struct Methods | Structs & Enumerators
C# Beyond Basics

book
Struct 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

copy
struct structureName
{
// some fields here (optional)

public returnType methodName(parameter1, parameter2, ...)
{
// code
}
}
123456789
struct structureName { // some fields here (optional) public returnType methodName(parameter1, parameter2, ...) { // code } }

For-example:

cs

index

copy
struct Player
{
public string name;
public int score;

public void displayScore()
{
Console.WriteLine($"{name} has {score} score");
}
}
12345678910
struct Player { public string name; public int score; public void displayScore() { Console.WriteLine($"{name} has {score} score"); } }

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

copy
using System;

struct Player
{
public string name;
public int score;

public void displayScore()
{
Console.WriteLine($"{name} has {score} score");
}
}

class ConsoleApp
{
static void Main(string[] args)
{
Player player1 = new Player();
Player player2 = new Player();

player1.name = "SuperKillerPro";
player1.score = 100;

player2.name = "Cyrex";
player2.score = 77;

player1.displayScore();
player2.displayScore();
}
}
123456789101112131415161718192021222324252627282930
using System; struct Player { public string name; public int score; public void displayScore() { Console.WriteLine($"{name} has {score} score"); } } class ConsoleApp { static void Main(string[] args) { Player player1 = new Player(); Player player2 = new Player(); player1.name = "SuperKillerPro"; player1.score = 100; player2.name = "Cyrex"; player2.score = 77; player1.displayScore(); player2.displayScore(); } }

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

copy
using System;

struct Player
{
public string name;
public int score;

public void displayScore()
{
Console.WriteLine($"{name} has {score} score");
}

public void addScore(int add)
{
score += add;
}
}

class ConsoleApp {
static void Main(string[] args)
{
Player player1 = new Player();

player1.name = "SuperKillerPro";
player1.score = 100;

player1.displayScore();
player1.addScore(70);
player1.displayScore();
}
}
12345678910111213141516171819202122232425262728293031
using System; struct Player { public string name; public int score; public void displayScore() { Console.WriteLine($"{name} has {score} score"); } public void addScore(int add) { score += add; } } class ConsoleApp { static void Main(string[] args) { Player player1 = new Player(); player1.name = "SuperKillerPro"; player1.score = 100; player1.displayScore(); player1.addScore(70); player1.displayScore(); } }

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?

question mark

Which is a valid keyword for declaring a method?

Select the correct answer

question mark

What will be the output of the following code?

struct Coordinate
{
public int x;
public int y;

public void setValue(int newX, int newY)
{
x = newX;
y = newY;
}

public void displayValue()
{
Console.WriteLine($"({x}, {y})");
}
}

static void Main(string[] args)
{
Coordinate coord = new Coordinate();

coord.setValue(5, 7);
coord.displayValue();
}

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

We use cookies to make your experience better!
some-alt