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
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.
Deep Learning with Python by François Chollet(12528)
Hello! Python by Anthony Briggs(9873)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9763)
The Mikado Method by Ola Ellnestam Daniel Brolund(9754)
Dependency Injection in .NET by Mark Seemann(9300)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8264)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7748)
Grails in Action by Glen Smith Peter Ledbrook(7673)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7523)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6767)
Microservices with Go by Alexander Shuiskov(6536)
Practical Design Patterns for Java Developers by Miroslav Wengner(6428)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6406)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6385)
Angular Projects - Third Edition by Aristeidis Bampakos(5795)
The Art of Crafting User Stories by The Art of Crafting User Stories(5320)
NetSuite for Consultants - Second Edition by Peter Ries(5260)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5081)
Kotlin in Action by Dmitry Jemerov(5025)
