Test Your Skills in C# Programming by 2022

Test Your Skills in C# Programming by 2022

Author:2022
Language: eng
Format: epub


Chapter 7 enCapsulation using properties and indexers

return "rectangle";

}

}

Answer:

Here is the output:

Experimenting with an abstract property in C#.

The area of the rectangle is 100 square units.

Explanation:

This program shows how to use an abstract property. In addition, the shape variable

is used polymorphically.

Author’s Note: I said earlier that you can create a property with different types of

modifiers. In this section, we have experimented with two of them: virtual properties and

abstract properties.

The Usage of the init Keyword

7.P7 Can you compile the following code?

Console.WriteLine("Experiment with the 'init' keyword.");

Game game = new();

Console.WriteLine($"Game name: {game.Name}");

class Game

{

private string _name;

public Game() { _name = "SuperGame"; }

public string Name

{

get => _name;

init => _name = value;

}

}

199

Chapter 7 enCapsulation using properties and indexers

Answer:

This code can compile and run successfully. You’ll get the following output:

Experiment with the 'init' keyword.

Game name: SuperGame

Explanation:

You have seen a simple usage of the init accessor for the property called Name. Here

I use a single statement to assign a value; so, I was able to implement the init accessor

as an expression-bodied member.

7.P8 Can you compile the following code?

Console.WriteLine("Experiment with the 'init' keyword.");

Game game = new();

game.Name = "SuperGame2";

class Game

{

private string _name;

public Game() { _name = "SuperGame"; }

public string Name

{

get => _name;

init => _name = value;

}

}

Answer:

No. You’ll receive the following compile-time error that is self-explanatory:

CS8852 Init-only property or indexer 'Game.Name' can only be assigned in

an object initializer, or on 'this' or 'base' in an instance constructor or

an 'init' accessor.

200

Chapter 7 enCapsulation using properties and indexers

7.P9 Can you predict the output of the following code segment?

Game game = new();

Console.WriteLine($"The game name: {game.Name}");

game = new() { Name="Airplane Simulator"};

Console.WriteLine($"The new game name: {game.Name}");

class Game

{

private string _name;

public Game() { _name = "Car Simulator"; }

public string Name

{

get => _name;

init => _name = value;

}

}

Answer:

This code can compile and run successfully. You’ll get the following output:

The game name: Car Simulator

The new game name: Airplane Simulator

Author’s Note: This program demonstrates the usage of object initializer syntax with the init accessor.

7.P10 Can you compile the following code?

Console.WriteLine("A usage of auto-implemented property with

the 'init' keyword.");

Game game = new();

Console.WriteLine($"Game name: {game.Name}");

class Game

{

public string Name { get; init; } = "SuperGame";

}

201



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.