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

Value semantics

Virtual, sealed, override, and abstract accessors | Field-like events | Event accessors | Virtual, sealed, override, and abstract accessors | Indexer overloading | Conversion operators | Instance constructors | Default constructors | Static constructors | The MoveNext method |


Читайте также:
  1. A FEW COMPONENTS OF CUSTOMER VALUE TODAY
  2. B) to preserve spiritual values and to form scientific worldview (philosophy).
  3. Chapter 10 Pricing Products: Understanding and Capturing Customer Value
  4. Common Brand Values
  5. Differences between cultures on the values dimensions
  6. Enterprise value and benefits of Financial Management
  7. ERROR - 14 : 41 - cvc-attribute.3: The value 'mit' of attribute 'login' on element 'student' is not valid with respect to its type, 'login'.

Structs are value types (§4.1) and are said to have value semantics. Classes, on the other hand, are reference types (§4.2) and are said to have reference semantics.

A variable of a struct type directly contains the data of the struct, whereas a variable of a class type contains a reference to the data, the latter known as an object. When a struct B contains an instance field of type A and A is a struct type, it is a compile-time error for A to depend on B. A struct X directly depends on a struct Y if X contains an instance field of type Y. Given this definition, the complete set of structs upon which a struct depends is the transitive closure of the directly depends on relationship. For example

struct Node
{
int data;

Node next; // error, Node directly depends on itself

}

is an error because Node contains an instance field of its own type. Another example

struct A { B b; }

struct B { C c; }

struct C { A a; }

is an error because each of the types A, B, and C depend on each other.

With classes, it is possible for two variables to reference the same object, and thus possible for operations on one variable to affect the object referenced by the other variable. With structs, the variables each have their own copy of the data (except in the case of ref and out parameter variables), and it is not possible for operations on one to affect the other. Furthermore, because structs are not reference types, it is not possible for values of a struct type to be null.

Given the declaration

struct Point
{
public int x, y;

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

the code fragment

Point a = new Point(10, 10);
Point b = a;
a.x = 100;
System.Console.WriteLine(b.x);

outputs the value 10. The assignment of a to b creates a copy of the value, and b is thus unaffected by the assignment to a.x. Had Point instead been declared as a class, the output would be 100 because a and b would reference the same object.

Inheritance

All struct types implicitly inherit from the class System.ValueType, which, in turn, inherits from class object. A struct declaration may specify a list of implemented interfaces, but it is not possible for a struct declaration to specify a base class.

Struct types are never abstract and are always implicitly sealed. The abstract and sealed modifiers are therefore not permitted in a struct declaration.

Since inheritance isn’t supported for structs, the declared accessibility of a struct member cannot be protected or protected internal.

Function members in a struct cannot be abstract or virtual, and the override modifier is allowed only to override methods inherited from System.ValueType.

Assignment

Assignment to a variable of a struct type creates a copy of the value being assigned. This differs from assignment to a variable of a class type, which copies the reference but not the object identified by the reference.

Similar to an assignment, when a struct is passed as a value parameter or returned as the result of a function member, a copy of the struct is created. A struct may be passed by reference to a function member using a ref or out parameter.

When a property or indexer of a struct is the target of an assignment, the instance expression associated with the property or indexer access must be classified as a variable. If the instance expression is classified as a value, a compile-time error occurs. This is described in further detail in §7.17.1.


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


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

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