Professional C# 학습 노트(3c# 객체 및 유형)
2735 단어 학습 노트
The Differences between classes and structs
Properties
1) read-only and write-only properties:
create a read-only property by simply omitting the set accessor from the property definition;
create a write-only property by omitting the get accessor.
2)access modifers for properties:
e.g. (get is public and set is private)
public string Name
{
get
{
return _name;
}
private set
{
_name = value;
}
}
3) auto implimented propertiespublic string ForeName {get; set;}public string ForeName {get;} public string ForeName {get; private set;}
Static Constructors
a static no - parameter constructor for a class will be executed only once, as opposed to the constructors written so far, which areinstance constructors that are executed whenever an object of that class is created.
e.g. public MyClass
{
static MyClass()
{
//initialization code
}
//rest of the class definition
}
Notice:
i) the static constructor does not have any access modifiers. It ’ s never called by any other C# code, but always by the .NET runtime when the class is loaded
ii) the static constructor can never take any parameters, and there can be only one static constructor for a class.
iii) a static constructor can access only static members, not instance members, of the class
ReadOnly Fields
constants don ’ t necessarily meet all requirements.On occasion, you may have some variable whose value shouldn ’ t be changed, but where the value is not known until runtime. C# provides another type of variable that is useful in this scenario: the readonly fieldThe rule is that you can assign values to a readonly field inside a constructor, but not anywhere else. It ’ s also possible for a readonly field to be an instance rather than a static field, having a different value for each instance of a class.This means that, unlike a const field, if you want a readonly field to be static, you have to declare it as such.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
axios 요청 차단, 응답 차단,router 내비게이션 수위axios 요청 차단: 요청 헤더에 token 등을 통일적으로 추가할 수 있습니다 axios 응답 차단: 로그인 판단 내비게이션 선행 수위beforeEach: 로그인 여부를 판단할 수 있지만, 응답으로 차단하는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.