Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Static and instance fields

Class base specification | Type parameter constraints | Partial types | Type parameters and constraints | Partial methods | Class members | The instance type | Static and instance members | Fully qualified name | Access to private and protected members of the containing type |


Читайте также:
  1. Attribute instances
  2. Benign prostatic hyperplasia
  3. FIELDS of BUSINESS
  4. Instance constructors
  5. INSTANCE_NUMBER
  6. Invocations on boxed instances
  7. Ip nat inside source static tcp 10.18.7.5 443 209.165.200.225 443ip nat inside source static udp 10.18.7.5 4365 209.165.200.225 4365

When a field declaration includes a static modifier, the fields introduced by the declaration are static fields. When no static modifier is present, the fields introduced by the declaration are instance fields. Static fields and instance fields are two of the several kinds of variables (§5) supported by C#, and at times they are referred to as static variables and instance variables, respectively.

A static field is not part of a specific instance; instead, it is shared amongst all instances of a closed type (§4.4.2). No matter how many instances of a closed class type are created, there is only ever one copy of a static field for the associated application domain.

For example:

class C<V>
{
static int count = 0;

public C() {
count++;
}

public static int Count {
get { return count; }
}
}

 

class Application
{
static void Main() {
C<int> x1 = new C<int>();
Console.WriteLine(C<int>.Count); // Prints 1

C<double> x2 = new C<double>();
Console.WriteLine(C<int>.Count); // Prints 1

C<int> x3 = new C<int>();
Console.WriteLine(C<int>.Count); // Prints 2
}
}

An instance field belongs to an instance. Specifically, every instance of a class contains a separate set of all the instance fields of that class.

When a field is referenced in a member-access (§7.6.4) of the form E.M, if M is a static field, E must denote a type containing M, and if M is an instance field, E must denote an instance of a type containing M.

The differences between static and instance members are discussed further in §10.3.7.

Readonly fields

When a field-declaration includes a readonly modifier, the fields introduced by the declaration are readonly fields. Direct assignments to readonly fields can only occur as part of that declaration or in an instance constructor or static constructor in the same class. (A readonly field can be assigned to multiple times in these contexts.) Specifically, direct assignments to a readonly field are permitted only in the following contexts:

· In the variable-declarator that introduces the field (by including a variable-initializer in the declaration).

· For an instance field, in the instance constructors of the class that contains the field declaration; for a static field, in the static constructor of the class that contains the field declaration. These are also the only contexts in which it is valid to pass a readonly field as an out or ref parameter.

Attempting to assign to a readonly field or pass it as an out or ref parameter in any other context is a compile-time error.


Дата добавления: 2015-11-16; просмотров: 43 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
Reserved member names| Volatile fields

mybiblioteka.su - 2015-2024 год. (0.006 сек.)