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

Meaning of this

Event accessors | Virtual, sealed, override, and abstract accessors | Indexer overloading | Conversion operators | Instance constructors | Default constructors | Static constructors | The MoveNext method | Implementation example | Value semantics |


Читайте также:
  1. A meaningful definition of negotiation
  2. A) Explain their meanings;
  3. A. Look through the descriptions of things you can do with music and try to guess the meaning of the words in bold type.
  4. A. Translate the terms in the table below paying attention to their contextual meaning.
  5. Add a prefix from the table to the words below. Explain their meaning.
  6. Adverbs which have different meanings
  7. Art and Meaning

Within an instance constructor or instance function member of a class, this is classified as a value. Thus, while this can be used to refer to the instance for which the function member was invoked, it is not possible to assign to this in a function member of a class.

Within an instance constructor of a struct, this corresponds to an out parameter of the struct type, and within an instance function member of a struct, this corresponds to a ref parameter of the struct type. In both cases, this is classified as a variable, and it is possible to modify the entire struct for which the function member was invoked by assigning to this or by passing this as a ref or out parameter.

Field initializers

As described in §11.3.4, the default value of a struct consists of the value that results from setting all value type fields to their default value and all reference type fields to null. For this reason, a struct does not permit instance field declarations to include variable initializers. This restriction applies only to instance fields. Static fields of a struct are permitted to include variable initializers.

The example

struct Point
{
public int x = 1; // Error, initializer not permitted
public int y = 1; // Error, initializer not permitted
}

is in error because the instance field declarations include variable initializers.

Constructors

Unlike a class, a struct is not permitted to declare a parameterless instance constructor. Instead, every struct implicitly has a parameterless instance constructor which always returns the value that results from setting all value type fields to their default value and all reference type fields to null (§4.1.2). A struct can declare instance constructors having parameters. For example

struct Point
{
int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}
}

Given the above declaration, the statements

Point p1 = new Point();

Point p2 = new Point(0, 0);

both create a Point with x and y initialized to zero.

A struct instance constructor is not permitted to include a constructor initializer of the form base(...).

If the struct instance constructor doesn’t specify a constructor initializer, the this variable corresponds to an out parameter of the struct type, and similar to an out parameter, this must be definitely assigned (§5.3) at every location where the constructor returns. If the struct instance constructor specifies a constructor initializer, the this variable corresponds to a ref parameter of the struct type, and similar to a ref parameter, this is considered definitely assigned on entry to the constructor body. Consider the instance constructor implementation below:

struct Point
{
int x, y;

public int X {
set { x = value; }
}

public int Y {
set { y = value; }
}

public Point(int x, int y) {
X = x; // error, this is not yet definitely assigned
Y = y; // error, this is not yet definitely assigned
}
}

No instance member function (including the set accessors for the properties X and Y) can be called until all fields of the struct being constructed have been definitely assigned. Note, however, that if Point were a class instead of a struct, the instance constructor implementation would be permitted.

Destructors

A struct is not permitted to declare a destructor.


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


<== предыдущая страница | следующая страница ==>
Boxing and unboxing| Database integer type

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