Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 87: Initializing Properties

Section 87.1: C# 6.0: Initialize an Auto-Implemented Property

Create a property with getter and/or setter and initialize all in one line:

public string Foobar { get; set; } = "xyz";

Section 87.2: Initializing Property with a Backing Field

public string Foobar {

get { return _foobar; } set { _foobar = value; }

}

private string _foobar = "xyz";

Section 87.3: Property Initialization during object instantiation

Properties can be set when an object is instantiated.

var redCar = new Car

{

Wheels = 2,

Year = 2016, Color = Color.Red

};

Section 87.4: Initializing Property in Constructor

class Example

{

public string Foobar { get; set; } public List<string> Names { get; set; } public Example()

{

Foobar = "xyz";

Names = new List<string>(){"carrot","fox","ball"};

}

}

GoalKicker.com – C# Notes for Professionals

490