You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-options/unsafe-compiler-option.md
+11-3Lines changed: 11 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "-unsafe (C# Compiler Options)"
3
-
ms.date: 07/20/2015
3
+
ms.date: 04/25/2018
4
4
ms.prod: .net
5
5
ms.technology:
6
6
- "devlang-csharp"
@@ -11,8 +11,6 @@ helpviewer_keywords:
11
11
- "-unsafe compiler option [C#]"
12
12
- "unsafe compiler option [C#]"
13
13
- "/unsafe compiler option [C#]"
14
-
ms.assetid: fdb77ed9-da03-45bd-bb7f-250704da1bcc
15
-
caps.latest.revision: 19
16
14
author: "BillWagner"
17
15
ms.author: "wiwagn"
18
16
---
@@ -36,6 +34,16 @@ The **-unsafe** compiler option allows code that uses the [unsafe](../../../csha
36
34
37
35
3. Select the **Allow Unsafe Code** check box.
38
36
37
+
### To add this option in a csproj file
38
+
39
+
Open the .csproj file for a project, and add the following elements:
40
+
41
+
```xml
42
+
<PropertyGroup>
43
+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
44
+
</PropertyGroup>
45
+
```
46
+
39
47
For information about how to set this compiler option programmatically, see <xref:VSLangProj80.CSharpProjectConfigurationProperties3.AllowUnsafeBlocks%2A>.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/default-values-table.md
+22-23Lines changed: 22 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,12 +15,11 @@ helpviewer_keywords:
15
15
- "variables [C#], value types"
16
16
- "constructors [C#], default constructor"
17
17
- "types [C#], default constructor return values"
18
-
ms.assetid: 4af2c1df-9e3a-48c1-83ac-b192986fc5bc
19
-
caps.latest.revision: 12
20
18
author: "BillWagner"
21
19
ms.author: "wiwagn"
22
20
---
23
21
# Default values table (C# Reference)
22
+
24
23
The following table shows the default values of value types returned by the default constructors. Default constructors are invoked by using the `new` operator, as follows:
25
24
26
25
```csharp
@@ -37,26 +36,26 @@ Remember that using uninitialized variables in C# is not allowed.
|[struct](../../../csharp/language-reference/keywords/struct.md)|The value produced by setting all value-type fields to their default values and all reference-type fields to `null`.|
The `fixed` statement prevents the garbage collector from relocating a movable variable. The `fixed` statement is only permitted in an [unsafe](../../../csharp/language-reference/keywords/unsafe.md) context. `Fixed` can also be used to create [fixed size buffers](../../../csharp/programming-guide/unsafe-code-pointers/fixed-size-buffers.md).
20
-
21
-
The `fixed` statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement. Without `fixed`, pointers to movable managed variables would be of little use since garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a `fixed` statement.
You can initialize a pointer by using an array, a string, a fixed-size buffer, or the address of a variable. The following example illustrates the use of variable addresses, arrays, and strings. For more information about fixed-size buffers, see [Fixed Size Buffers](../../../csharp/programming-guide/unsafe-code-pointers/fixed-size-buffers.md).
You can initialize multiple pointers, as long as they are all of the same type.
30
-
17
+
18
+
The `fixed` statement prevents the garbage collector from relocating a movable variable. The `fixed` statement is only permitted in an [unsafe](unsafe.md) context. `Fixed` can also be used to create [fixed size buffers](../../programming-guide/unsafe-code-pointers/fixed-size-buffers.md).
19
+
20
+
The `fixed` statement sets a pointer to a managed variable and "pins" that variable during the execution of the statement. Pointers to movable managed variables are useful only in a `fixed` context. Without a `fixed` context, garbage collection could relocate the variables unpredictably. The C# compiler only lets you assign a pointer to a managed variable in a `fixed` statement.
You can initialize a pointer by using an array, a string, a fixed-size buffer, or the address of a variable. The following example illustrates the use of variable addresses, arrays, and strings. For more information about fixed-size buffers, see [Fixed Size Buffers](../../programming-guide/unsafe-code-pointers/fixed-size-buffers.md).
After the code in the statement is executed, any pinned variables are unpinned and subject to garbage collection. Therefore, do not point to those variables outside the `fixed` statement.
40
-
41
-
> [!NOTE]
42
-
> Pointers initialized in fixed statements cannot be modified.
43
-
44
-
In unsafe mode, you can allocate memory on the stack, where it is not subject to garbage collection and therefore does not need to be pinned. For more information, see [stackalloc](../../../csharp/language-reference/keywords/stackalloc.md).
After the code in the statement is executed, any pinned variables are unpinned and subject to garbage collection. Therefore, do not point to those variables outside the `fixed` statement. The variables declared in the `fixed` statement are scoped to that statement, making this easier:
39
+
40
+
```csharp
41
+
fixed (byte*ps=srcarray, pd=dstarray)
42
+
{
43
+
...
44
+
}
45
+
// ps and pd are no longer in scope here.
46
+
```
47
+
48
+
Pointers initialized in `fixed` statements are readonly variables. If you want to modify the pointer value, you must declare a second pointer variable, and modify that. The variable declared in the `fixed` statement cannot be modified:
49
+
50
+
```csharp
51
+
fixed (byte*ps=srcarray, pd=dstarray)
52
+
{
53
+
byte*pSourceCopy=ps;
54
+
pSourceCopy++; // point to the next element.
55
+
ps++; // invalid: cannot modify ps, as it is declared in the fixed statement.
56
+
}
57
+
```
58
+
59
+
60
+
In unsafe mode, you can allocate memory on the stack, where it is not subject to garbage collection and therefore does not need to be pinned. For more information, see [stackalloc](stackalloc.md).
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/concepts/linq/linq-to-adonet-portal-page.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ ms.author: "wiwagn"
19
19
> [!NOTE]
20
20
> The [!INCLUDE[linq_adonet](~/includes/linq-adonet-md.md)] documentation is located in the ADO.NET section of the .NET Framework SDK: [LINQ and ADO.NET](http://msdn.microsoft.com/library/bf0c8f93-3ff7-49f3-8aed-f2b7ac938dec).
21
21
22
-
There are three separate ADO.NET [!INCLUDE[vbteclinqext](~/includes/vbteclinqext-md.md)] technologies: [!INCLUDE[linq_dataset](~/includes/linq-dataset-md.md)], [!INCLUDE[vbtecdlinq](~/includes/vbtecdlinq-md.md)], and [!INCLUDE[linq_entities](~/includes/linq-entities-md.md)]. [!INCLUDE[linq_dataset](~/includes/linq-dataset-md.md)] provides richer, optimized querying over the <xref:System.Data.DataSet>, [!INCLUDE[vbtecdlinq](~/includes/vbtecdlinq-md.md)] enables you to directly query [!INCLUDE[ssNoVersion](~/includes/ssnoversion-md.md)] database schemas, and [!INCLUDE[linq_entities](~/includes/linq-entities-md.md)] allows you to query an [!INCLUDE[adonet_edm](~/includes/adonet-edm-md.md)].
22
+
There are three separate ADO.NET [!INCLUDE[vbteclinqext](~/includes/vbteclinqext-md.md)] technologies: [!INCLUDE[linq_dataset](~/includes/linq-dataset-md.md)], [!INCLUDE[vbtecdlinq](~/includes/vbtecdlinq-md.md)], and [!INCLUDE[linq_entities](~/includes/linq-entities-md.md)]. [!INCLUDE[linq_dataset](~/includes/linq-dataset-md.md)] provides richer, optimized querying over the <xref:System.Data.DataSet>, [!INCLUDE[vbtecdlinq](~/includes/vbtecdlinq-md.md)] enables you to directly query SQL Server database schemas, and [!INCLUDE[linq_entities](~/includes/linq-entities-md.md)] allows you to query an [!INCLUDE[adonet_edm](~/includes/adonet-edm-md.md)].
23
23
24
24
## LINQ to DataSet
25
25
The <xref:System.Data.DataSet> is one of the most widely used components in [!INCLUDE[vstecado](~/includes/vstecado-md.md)], and is a key element of the disconnected programming model that [!INCLUDE[vstecado](~/includes/vstecado-md.md)] is built on. Despite this prominence, however, the <xref:System.Data.DataSet> has limited query capabilities.
Copy file name to clipboardExpand all lines: docs/csharp/programming-guide/statements-expressions-operators/default-value-expressions.md
+25-10Lines changed: 25 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,47 @@
1
1
---
2
2
title: "Default value expressions (C# Programming Guide)"
3
3
description: "Default value expressions produce the default value for any reference type or value type"
4
-
ms.date: 08/23/2017
4
+
ms.date: 04/25/2018
5
5
ms.prod: .net
6
6
ms.technology:
7
7
- "devlang-csharp"
8
8
ms.topic: "article"
9
9
helpviewer_keywords:
10
10
- "generics [C#], default keyword"
11
11
- "default keyword [C#], generic programming"
12
-
ms.assetid: b9daf449-4e64-496e-8592-6ed2c8875a98
13
-
caps.latest.revision: 22
14
12
author: "BillWagner"
15
13
ms.author: "wiwagn"
16
14
---
17
15
# default value expressions (C# programming guide)
18
16
19
-
A default value expression produces the default value for a type. Default value expressions are particularly useful in generic classes and methods. One issue that arises using generics is how to assign a default value to a parameterized type `T` when you do not know the following in advance:
17
+
A default value expression `default(T)` produces the default value of a type `T`. The following table shows which values are produced for various types:
|[enum](../../language-reference/keywords/enum.md)|The value produced by the expression `(E)0`, where `E` is the enum identifier.|
26
+
|[struct](../../language-reference/keywords/struct.md)|The value produced by setting all value type fields to their default value and all reference type fields to `null`.|
27
+
|Nullable type|An instance for which the <xref:System.Nullable%601.HasValue%2A> property is `false` and the <xref:System.Nullable%601.Value%2A> property is undefined.|
28
+
29
+
Default value expressions are particularly useful in generic classes and methods. One issue that arises using generics is how to assign a default value of a parameterized type `T` when you don't know the following in advance:
20
30
21
31
- Whether `T` is a reference type or a value type.
22
-
- If `T` is a value type, whether is a numeric value or a user-defined struct.
32
+
- If `T` is a value type, whether it's a numeric value or a struct.
33
+
34
+
Given a variable `t` of a parameterized type `T`, the statement `t = null` is only valid if `T` is a reference type. The assignment `t = 0` only works for numeric value types but not for structs. To solve that, use a default value expression:
23
35
24
-
Given a variable `t` of a parameterized type `T`, the statement `t = null` is only valid if `T` is a reference type. The assignment `t = 0` only works for numeric value types but not for structs. The solution is to use a default value expression, which returns `null` for reference types (class types and interface types) and zero for numeric value types. For user-defined structs, it returns the struct initialized to the zero bit pattern, which produces 0 or `null` for each member depending on whether that member is a value or reference type. For nullable value types, `default` returns a <xref:System.Nullable%601?displayProperty=nameWithType>, which is initialized like any struct.
36
+
```csharp
37
+
Tt=default(T);
38
+
```
25
39
26
40
The `default(T)` expression is not limited to generic classes and methods. Default value expressions can be used with any managed type. Any of these expressions are valid:
The following example from the `GenericList<T>` class shows how to use the `default(T)` operator in a generic class. For more information, see [Generics Overview](../generics/introduction-to-generics.md).
44
+
The following example from the `GenericList<T>` class shows how to use the `default(T)` operator in a generic class. For more information, see [Introduction to Generics](../generics/introduction-to-generics.md).
0 commit comments