Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions docs/csharp/language-reference/keywords/value-types.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "Value Types (C# Reference)"
ms.date: 07/20/2015
title: "Value types (C# Reference)"
ms.date: 11/26/2018
f1_keywords:
- "cs.valuetypes"
helpviewer_keywords:
Expand All @@ -9,44 +9,48 @@ helpviewer_keywords:
- "C# language, value types"
ms.assetid: 471eb994-2958-49d5-a6be-19b4313f80a3
---
# Value Types (C# Reference)
The value types consist of two main categories:

- [Structs](../../../csharp/language-reference/keywords/struct.md)

- [Enumerations](../../../csharp/language-reference/keywords/enum.md)

Structs fall into these categories:

- Numeric types

- [Integral types](../../../csharp/language-reference/keywords/integral-types-table.md)

- [Floating-point types](../../../csharp/language-reference/keywords/floating-point-types-table.md)

- [bool](../../../csharp/language-reference/keywords/bool.md)

- User defined structs.

## Main Features of Value Types
Variables that are based on value types directly contain values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference to the object but not the object itself.

All value types are derived implicitly from the <xref:System.ValueType?displayProperty=nameWithType>.

Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.

Unlike reference types, a value type cannot contain the `null` value. However, the [nullable types](../../../csharp/programming-guide/nullable-types/index.md) feature does allow for value types to be assigned to `null`.
# Value types (C# Reference)

There are two kinds of value types:

- [Structs](struct.md)

- [Enumerations](enum.md)

## Main features of value types

A variable of a value type contains a value of the type. For example, a variable of the `int` type might contain the value `42`. This differs from a variable of a reference type, which contains a reference to an instance of the type, also known as an object. When you assign a new value to a variable of a value type, that value is copied. When you assign a new value to a variable of a reference type, the reference is copied, not the object itself.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richlander @BillWagner please check this one rigorously


All value types are derived implicitly from the <xref:System.ValueType?displayProperty=nameWithType>.

Each value type has an implicit default constructor that initializes the default value of that type. For information about default values of value types, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md).
Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.

## Main Features of Simple Types
All of the simple types -- those integral to the C# language -- are aliases of the .NET Framework System types. For example, [int](../../../csharp/language-reference/keywords/int.md) is an alias of <xref:System.Int32?displayProperty=nameWithType>. For a complete list of aliases, see [Built-In Types Table](../../../csharp/language-reference/keywords/built-in-types-table.md).
Value type variables cannot be `null` by default. However, variables of the corresponding [nullable types](../../../csharp/programming-guide/nullable-types/index.md) can be `null`.

Constant expressions, whose operands are all simple type constants, are evaluated at compilation time.
Each value type has an implicit default constructor that initializes the default value of that type. For information about default values of value types, see [Default values table](default-values-table.md).

Simple types can be initialized by using literals. For example, 'A' is a literal of the type `char` and 2001 is a literal of the type `int`.
## Simple types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • It seems like all of the following text should go into the structs topic not the more general value types topic. All of this is specific to structs, right?
  • The "simple types" section seems to beg for a matching user-defined types section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "simple types" section seems to beg for a matching user-defined types section

What would such a section contain? Is there any property of user-defined structs that is not valid for a simple type?

It seems like all of the following text should go into the structs topic not the more general value types topic. All of this is specific to structs, right?

Though I agree with this, I don't think this PR should handle it. Simply because what is the structs topic? There are several candidates:

#966 is tracking the duplication issue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would such a section contain? Is there any property of user-defined structs that is not valid for a simple type?

There may not be differences. It's more that there is a section dedicated to one form of structs and not to the other.

Though I agree with this, I don't think this PR should handle it.

Sounds good.

Copy link
Contributor Author

@pkulikov pkulikov Nov 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more that there is a section dedicated to one form of structs and not to the other.

I see the whole picture as one set of structs (simple types) is a special subset of the set of all struct types. So, what should be described is (1) structs in general; (2) simple types. Though, it's good idea to say that a user-defined struct is a struct, which is not a simple type. I don't know where to put it yet in docs. So, come back to it later (together with another review of this topic, which can still be improved).


The *simple types* are a set of predefined struct types provided by C# and comprise the following types:

- [Integral types](integral-types-table.md): integer numeric types and the [char](char.md) type
- [Floating-point types](floating-point-types-table.md)
- [bool](bool.md)

The simple types are identified through keywords, but these keywords are simply aliases for predefined struct types in the <xref:System> namespace. For example, [int](int.md) is an alias of <xref:System.Int32?displayProperty=nameWithType>. For a complete list of aliases, see [Built-in types table](built-in-types-table.md).

The simple types differ from other struct types in that they permit certain additional operations:

- Simple types can be initialized by using literals. For example, `'A'` is a literal of the type `char` and `2001` is a literal of the type `int`.

- You can declare constants of the simple types with the [const](const.md) keyword. It's not possible to have constants of other struct types.

- Constant expressions, whose operands are all simple type constants, are evaluated at compile time.

For more information, see the [Simple types](~/_csharplang/spec/types.md#simple-types) section of the [C# language specification](../language-specification/index.md).

## Initializing Value Types
## Initializing value types

Local variables in C# must be initialized before they are used. For example, you might declare a local variable without initialization as in the following example:

```csharp
Expand Down Expand Up @@ -77,26 +81,26 @@ int myInt = new int();
int myInt = 0;
```

Using the [new](../../../csharp/language-reference/keywords/new.md) operator calls the default constructor of the specific type and assigns the default value to the variable. In the preceding example, the default constructor assigned the value `0` to `myInt`. For more information about values assigned by calling default constructors, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md).
Using the [new](new.md) operator calls the default constructor of the specific type and assigns the default value to the variable. In the preceding example, the default constructor assigned the value `0` to `myInt`. For more information about values assigned by calling default constructors, see [Default values table](default-values-table.md).

With user-defined types, use [new](../../../csharp/language-reference/keywords/new.md) to invoke the default constructor. For example, the following statement invokes the default constructor of the `Point` struct:
With user-defined types, use [new](new.md) to invoke the default constructor. For example, the following statement invokes the default constructor of the `Point` struct:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should new be in a code block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I observe and remember, code fencing is not applied in the link texts. though sometimes one can notice the link text with code fencing. @mairaw what is the rule here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. We shouldn't use code fencing for links (it looks strange), however, I think it is important for keywords to be code fenced. I noticed another usage where the first entry was code-fenced and then a later entry was linked. I found that to be the best model as a reader.


```csharp
Point p = new Point(); // Invoke default constructor for the struct.
```

After this call, the struct is considered to be definitely assigned; that is, all its members are initialized to their default values.

For more information about the new operator, see [new](../../../csharp/language-reference/keywords/new.md).
For more information about the `new` operator, see [new](new.md).

For information about formatting the output of numeric types, see [Formatting Numeric Results Table](../../../csharp/language-reference/keywords/formatting-numeric-results-table.md).
For information about formatting the output of numeric types, see [Formatting numeric results table](formatting-numeric-results-table.md).

## See Also
## See also

- [C# Reference](../../../csharp/language-reference/index.md)
- [C# Programming Guide](../../../csharp/programming-guide/index.md)
- [C# Keywords](../../../csharp/language-reference/keywords/index.md)
- [Types](../../../csharp/language-reference/keywords/types.md)
- [Reference Tables for Types](../../../csharp/language-reference/keywords/reference-tables-for-types.md)
- [Reference Types](../../../csharp/language-reference/keywords/reference-types.md)
- [C# Reference](../index.md)
- [C# Programming Guide](../../programming-guide/index.md)
- [C# Keywords](index.md)
- [Types](types.md)
- [Reference tables for types](reference-tables-for-types.md)
- [Reference Types](reference-types.md)
- [Nullable types](../../programming-guide/nullable-types/index.md)