/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/programming-guide/classes-and-structs/snippets/properties/accessors.cs
153 строки
3 KB
Bill Wagner
Add conceptual content for partial properties (#42177)
22 авг 2024, 17:38
Не верифицирован
22 авг 2024, 17:38
985d4e9
Код
Авторство
О чём код?
using System.Security.Cryptography; using System.Xml.Linq; namespace Accessors; class Test { //<Accessor> private string _name = "Hello"; public string Name { get { return _name; } protected set { _name = value; } } //</Accessor> } //<Hierarchy> public class Parent { public virtual int TestProperty { // Notice the accessor accessibility level. protected set { } // No access modifier is used here. get { return 0; } } } public class Kid : Parent { public override int TestProperty { // Use the same accessibility level as in the overridden accessor. protected set { } // Cannot use access modifier here. get { return 0; } } } //</Hierarchy> //<InterfaceAccessor> public interface ISomeInterface { int TestProperty { // No access modifier allowed here // because this is an interface. get; } } public class TestClass : ISomeInterface { public int TestProperty { // Cannot use access modifier here because // this is an interface implementation. get { return 10; } // Interface property does not have set accessor, // so access modifier is allowed. protected set { } } } //</InterfaceAccessor> //<Example> public class BaseClass { private string _name = "Name-BaseClass"; private string _id = "ID-BaseClass"; public string Name { get { return _name; } set { } } public string Id { get { return _id; } set { } } } public class DerivedClass : BaseClass { private string _name = "Name-DerivedClass"; private string _id = "ID-DerivedClass"; new public string Name { get { return _name; } // Using "protected" would make the set accessor not accessible. set { _name = value; } } // Using private on the following property hides it in the Main Class. // Any assignment to the property will use Id in BaseClass. new private string Id { get { return _id; } set { _id = value; } } } class MainClass { static void Main() { BaseClass b1 = new BaseClass(); DerivedClass d1 = new DerivedClass(); b1.Name = "Mary"; d1.Name = "John"; b1.Id = "Mary123"; d1.Id = "John123"; // The BaseClass.Id property is called. System.Console.WriteLine("Base: {0}, {1}", b1.Name, b1.Id); System.Console.WriteLine("Derived: {0}, {1}", d1.Name, d1.Id); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: Base: Name-BaseClass, ID-BaseClass Derived: John, ID-BaseClass */ //</Example>