From 015f3b29f57a1f78d23c219f274043b8326bf47b Mon Sep 17 00:00:00 2001 From: Maira Wenzel Date: Sun, 16 Dec 2018 17:12:58 -0800 Subject: [PATCH] remove inner snippets + formatting --- .../keywords/codesnippet/CSharp/value_1.cs | 42 ------- .../keywords/codesnippet/CSharp/var_1.cs | 26 ---- .../keywords/codesnippet/CSharp/virtual_1.cs | 42 ------- .../keywords/codesnippet/CSharp/virtual_2.cs | 75 ----------- .../keywords/value-types-table.md | 40 +++--- .../keywords/value-types.md | 118 +++++++++--------- .../language-reference/keywords/value.md | 31 ++--- .../csharp/language-reference/keywords/var.md | 40 +++--- .../language-reference/keywords/virtual.md | 109 ++++++++-------- .../language-reference/keywords/void.md | 22 ++-- .../language-reference/keywords/volatile.md | 34 ++--- 11 files changed, 201 insertions(+), 378 deletions(-) delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/value_1.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/var_1.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_1.cs delete mode 100644 docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_2.cs diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/value_1.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/value_1.cs deleted file mode 100644 index ffcb680b43ccf..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/value_1.cs +++ /dev/null @@ -1,42 +0,0 @@ - class MyBaseClass - { - // virtual auto-implemented property. Overrides can only - // provide specialized behavior if they implement get and set accessors. - public virtual string Name { get; set; } - - // ordinary virtual property with backing field - private int num; - public virtual int Number - { - get { return num; } - set { num = value; } - } - } - - - class MyDerivedClass : MyBaseClass - { - private string name; - - // Override auto-implemented property with ordinary property - // to provide specialized accessor behavior. - public override string Name - { - get - { - return name; - } - set - { - if (value != String.Empty) - { - name = value; - } - else - { - name = "Unknown"; - } - } - } - - } \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/var_1.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/var_1.cs deleted file mode 100644 index 3b055be00d900..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/var_1.cs +++ /dev/null @@ -1,26 +0,0 @@ - // Example #1: var is optional because - // the select clause specifies a string - string[] words = { "apple", "strawberry", "grape", "peach", "banana" }; - var wordQuery = from word in words - where word[0] == 'g' - select word; - - // Because each element in the sequence is a string, - // not an anonymous type, var is optional here also. - foreach (string s in wordQuery) - { - Console.WriteLine(s); - } - - // Example #2: var is required when - // the select clause specifies an anonymous type - var custQuery = from cust in customers - where cust.City == "Phoenix" - select new { cust.Name, cust.Phone }; - - // var must be used because each item - // in the sequence is an anonymous type - foreach (var item in custQuery) - { - Console.WriteLine("Name={0}, Phone={1}", item.Name, item.Phone); - } diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_1.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_1.cs deleted file mode 100644 index ffcb680b43ccf..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_1.cs +++ /dev/null @@ -1,42 +0,0 @@ - class MyBaseClass - { - // virtual auto-implemented property. Overrides can only - // provide specialized behavior if they implement get and set accessors. - public virtual string Name { get; set; } - - // ordinary virtual property with backing field - private int num; - public virtual int Number - { - get { return num; } - set { num = value; } - } - } - - - class MyDerivedClass : MyBaseClass - { - private string name; - - // Override auto-implemented property with ordinary property - // to provide specialized accessor behavior. - public override string Name - { - get - { - return name; - } - set - { - if (value != String.Empty) - { - name = value; - } - else - { - name = "Unknown"; - } - } - } - - } \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_2.cs b/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_2.cs deleted file mode 100644 index 2d50e54f02078..0000000000000 --- a/docs/csharp/language-reference/keywords/codesnippet/CSharp/virtual_2.cs +++ /dev/null @@ -1,75 +0,0 @@ - class TestClass - { - public class Shape - { - public const double PI = Math.PI; - protected double x, y; - public Shape() - { - } - public Shape(double x, double y) - { - this.x = x; - this.y = y; - } - - public virtual double Area() - { - return x * y; - } - } - - public class Circle : Shape - { - public Circle(double r) : base(r, 0) - { - } - - public override double Area() - { - return PI * x * x; - } - } - - class Sphere : Shape - { - public Sphere(double r) : base(r, 0) - { - } - - public override double Area() - { - return 4 * PI * x * x; - } - } - - class Cylinder : Shape - { - public Cylinder(double r, double h) : base(r, h) - { - } - - public override double Area() - { - return 2 * PI * x * x + 2 * PI * x * y; - } - } - - static void Main() - { - double r = 3.0, h = 5.0; - Shape c = new Circle(r); - Shape s = new Sphere(r); - Shape l = new Cylinder(r, h); - // Display results: - Console.WriteLine("Area of Circle = {0:F2}", c.Area()); - Console.WriteLine("Area of Sphere = {0:F2}", s.Area()); - Console.WriteLine("Area of Cylinder = {0:F2}", l.Area()); - } - } - /* - Output: - Area of Circle = 28.27 - Area of Sphere = 113.10 - Area of Cylinder = 150.80 - */ \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/value-types-table.md b/docs/csharp/language-reference/keywords/value-types-table.md index 8d295b05f42f6..73e79c12b2df3 100644 --- a/docs/csharp/language-reference/keywords/value-types-table.md +++ b/docs/csharp/language-reference/keywords/value-types-table.md @@ -11,25 +11,25 @@ ms.assetid: 67d8f631-b6e3-4d83-9910-5ec497f8c5f3 --- # Value types table (C# Reference) -The following table shows the C# value types. - -|Value type|Category|Type suffix| -|----------------|--------------|-----------------| -|[bool](bool.md)|Boolean|| -|[byte](byte.md)|Unsigned, numeric, [integral](integral-types-table.md)|| -|[char](char.md)|Unsigned, numeric, [integral](integral-types-table.md)|| -|[decimal](decimal.md)|Numeric, [floating-point](floating-point-types-table.md)|M or m| -|[double](double.md)|Numeric, [floating-point](floating-point-types-table.md)|D or d| -|[enum](enum.md)|Enumeration|| -|[float](float.md)|Numeric, [floating-point](floating-point-types-table.md)|F or f| -|[int](int.md)|Signed, numeric, [integral](integral-types-table.md)|| -|[long](long.md)|Signed, numeric, [integral](integral-types-table.md)|L or l| -|[sbyte](sbyte.md)|Signed, numeric, [integral](integral-types-table.md)|| -|[short](short.md)|Signed, numeric, [integral](integral-types-table.md)|| -|[struct](struct.md)|User-defined structure|| -|[uint](uint.md)|Unsigned, numeric, [integral](integral-types-table.md)|U or u| -|[ulong](ulong.md)|Unsigned, numeric, [integral](integral-types-table.md)|UL, Ul, uL, ul, LU, Lu, lU, or lu| -|[ushort](ushort.md)|Unsigned, numeric, [integral](integral-types-table.md)|| +The following table shows the C# value types: + +|Value type|Category|Type suffix| +|----------------|--------------|-----------------| +|[bool](bool.md)|Boolean|| +|[byte](byte.md)|Unsigned, numeric, [integral](integral-types-table.md)|| +|[char](char.md)|Unsigned, numeric, [integral](integral-types-table.md)|| +|[decimal](decimal.md)|Numeric, [floating-point](floating-point-types-table.md)|M or m| +|[double](double.md)|Numeric, [floating-point](floating-point-types-table.md)|D or d| +|[enum](enum.md)|Enumeration|| +|[float](float.md)|Numeric, [floating-point](floating-point-types-table.md)|F or f| +|[int](int.md)|Signed, numeric, [integral](integral-types-table.md)|| +|[long](long.md)|Signed, numeric, [integral](integral-types-table.md)|L or l| +|[sbyte](sbyte.md)|Signed, numeric, [integral](integral-types-table.md)|| +|[short](short.md)|Signed, numeric, [integral](integral-types-table.md)|| +|[struct](struct.md)|User-defined structure|| +|[uint](uint.md)|Unsigned, numeric, [integral](integral-types-table.md)|U or u| +|[ulong](ulong.md)|Unsigned, numeric, [integral](integral-types-table.md)|UL, Ul, uL, ul, LU, Lu, lU, or lu| +|[ushort](ushort.md)|Unsigned, numeric, [integral](integral-types-table.md)|| ## Remarks @@ -50,4 +50,4 @@ If a [real numerical literal](~/_csharplang/spec/lexical-structure.md#real-liter - [Reference tables for types](reference-tables-for-types.md) - [Default values table](default-values-table.md) - [Value types](value-types.md) -- [Formatting numeric results table](formatting-numeric-results-table.md) +- [Formatting numeric results table](formatting-numeric-results-table.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/value-types.md b/docs/csharp/language-reference/keywords/value-types.md index 80f6fe2957021..6ce62ac048bae 100644 --- a/docs/csharp/language-reference/keywords/value-types.md +++ b/docs/csharp/language-reference/keywords/value-types.md @@ -23,14 +23,14 @@ There are two kinds 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. -All value types are derived implicitly from the . - -Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces. - +All value types are derived implicitly from the . + +Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces. + 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`. - -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). - + +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 The *simple types* are a set of predefined struct types provided by C# and comprise the following types: @@ -50,59 +50,59 @@ The simple types differ from other struct types in that they permit certain addi - 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 - 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 -int myInt; -``` - - You cannot use it before you initialize it. You can initialize it using the following statement: - -```csharp -myInt = new int(); // Invoke default constructor for int type. -``` - - This statement is equivalent to the following statement: - -```csharp -myInt = 0; // Assign an initial value, 0 in this example. -``` - - You can, of course, have the declaration and the initialization in the same statement as in the following examples: - -```csharp -int myInt = new int(); -``` - - –or– - -```csharp -int myInt = 0; -``` - - 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](new.md) to invoke the default constructor. For example, the following statement invokes the default constructor of the `Point` struct: - -```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](new.md). - - For information about formatting the output of numeric types, see [Formatting numeric results table](formatting-numeric-results-table.md). - +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 +int myInt; +``` + +You cannot use it before you initialize it. You can initialize it using the following statement: + +```csharp +myInt = new int(); // Invoke default constructor for int type. +``` + +This statement is equivalent to the following statement: + +```csharp +myInt = 0; // Assign an initial value, 0 in this example. +``` + +You can, of course, have the declaration and the initialization in the same statement as in the following examples: + +```csharp +int myInt = new int(); +``` + +–or– + +```csharp +int myInt = 0; +``` + +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](new.md) to invoke the default constructor. For example, the following statement invokes the default constructor of the `Point` struct: + +```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](new.md). + +For information about formatting the output of numeric types, see [Formatting numeric results table](formatting-numeric-results-table.md). + ## See also -- [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) +- [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) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/value.md b/docs/csharp/language-reference/keywords/value.md index 2308290c4a802..5290518769073 100644 --- a/docs/csharp/language-reference/keywords/value.md +++ b/docs/csharp/language-reference/keywords/value.md @@ -1,7 +1,6 @@ --- -title: "value - C# Reference" +title: "value contextual keyword - C# Reference" ms.custom: seodec18 - ms.date: 07/20/2015 f1_keywords: - "value_CSharpKeyword" @@ -10,17 +9,19 @@ helpviewer_keywords: ms.assetid: c99d6468-687f-4a46-89b4-a95e1b00bf6d --- # value (C# Reference) -The contextual keyword `value` is used in the set accessor in ordinary property declarations. It is similar to an input parameter on a method. The word `value` references the value that client code is attempting to assign to the property. In the following example, `MyDerivedClass` has a property called `Name` that uses the `value` parameter to assign a new string to the backing field `name`. From the point of view of client code, the operation is written as a simple assignment. - - [!code-csharp[csrefKeywordsModifiers#26](../../../csharp/language-reference/keywords/codesnippet/CSharp/value_1.cs)] - - For more information about the use of `value`, see [Properties](../../../csharp/programming-guide/classes-and-structs/properties.md). - -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## 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) +The contextual keyword `value` is used in the set accessor in ordinary property declarations. It is similar to an input parameter on a method. The word `value` references the value that client code is attempting to assign to the property. In the following example, `MyDerivedClass` has a property called `Name` that uses the `value` parameter to assign a new string to the backing field `name`. From the point of view of client code, the operation is written as a simple assignment. + +[!code-csharp[csrefKeywordsModifiers#26](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#26)] + +For more information about the use of `value`, see [Properties](../../programming-guide/classes-and-structs/properties.md). + +## C# language specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] + +## See also + +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Keywords](index.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/var.md b/docs/csharp/language-reference/keywords/var.md index 831ed5f249d1e..3f76bc27ad0aa 100644 --- a/docs/csharp/language-reference/keywords/var.md +++ b/docs/csharp/language-reference/keywords/var.md @@ -11,22 +11,24 @@ helpviewer_keywords: ms.assetid: 0777850a-2691-4e3e-927f-0c850f5efe15 --- # var (C# Reference) -Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit "type" `var`. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of `i` are functionally equivalent: - -```csharp -var i = 10; // Implicitly typed. -int i = 10; // Explicitly typed. -``` - - For more information, see [Implicitly Typed Local Variables](../../../csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md) and [Type Relationships in LINQ Query Operations](../../../csharp/programming-guide/concepts/linq/type-relationships-in-linq-query-operations.md). - -## Example - The following example shows two query expressions. In the first expression, the use of `var` is permitted but is not required, because the type of the query result can be stated explicitly as an `IEnumerable`. However, in the second expression, `var` allows the result to be a collection of anonymous types, and the name of that type is not accessible except to the compiler itself. Use of `var` eliminates the requirement to create a new class for the result. Note that in Example #2, the `foreach` iteration variable `item` must also be implicitly typed. - - [!code-csharp[csrefKeywordsTypes#18](../../../csharp/language-reference/keywords/codesnippet/CSharp/var_1.cs)] - -## See Also - -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [Implicitly Typed Local Variables](../../../csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables.md) + +Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit "type" `var`. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of `i` are functionally equivalent: + +```csharp +var i = 10; // Implicitly typed. +int i = 10; // Explicitly typed. +``` + +For more information, see [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md) and [Type Relationships in LINQ Query Operations](../../programming-guide/concepts/linq/type-relationships-in-linq-query-operations.md). + +## Example + +The following example shows two query expressions. In the first expression, the use of `var` is permitted but is not required, because the type of the query result can be stated explicitly as an `IEnumerable`. However, in the second expression, `var` allows the result to be a collection of anonymous types, and the name of that type is not accessible except to the compiler itself. Use of `var` eliminates the requirement to create a new class for the result. Note that in Example #2, the `foreach` iteration variable `item` must also be implicitly typed. + +[!code-csharp[csrefKeywordsTypes#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsTypes/CS/keywordsTypes.cs#18)] + +## See also + +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [Implicitly Typed Local Variables](../../programming-guide/classes-and-structs/implicitly-typed-local-variables.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/virtual.md b/docs/csharp/language-reference/keywords/virtual.md index 609dccd16f4f8..c9bb73d6d0b22 100644 --- a/docs/csharp/language-reference/keywords/virtual.md +++ b/docs/csharp/language-reference/keywords/virtual.md @@ -1,7 +1,6 @@ --- title: "virtual - C# Reference" ms.custom: seodec18 - ms.date: 07/20/2015 f1_keywords: - "virtual_CSharpKeyword" @@ -11,55 +10,59 @@ helpviewer_keywords: ms.assetid: 5da9abae-bc1e-434f-8bea-3601b8dcb3b2 --- # virtual (C# Reference) -The `virtual` keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: - -```csharp -public virtual double Area() -{ - return x * y; -} -``` - - The implementation of a virtual member can be changed by an [overriding member](../../../csharp/language-reference/keywords/override.md) in a derived class. For more information about how to use the `virtual` keyword, see [Versioning with the Override and New Keywords](../../../csharp/programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing When to Use Override and New Keywords](../../../csharp/programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). - -## Remarks - When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. - - By default, methods are non-virtual. You cannot override a non-virtual method. - - You cannot use the `virtual` modifier with the `static`, `abstract`, `private`, or `override` modifiers. The following example shows a virtual property: - - [!code-csharp[csrefKeywordsModifiers#26](../../../csharp/language-reference/keywords/codesnippet/CSharp/virtual_1.cs)] - - Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax. - -- It is an error to use the `virtual` modifier on a static property. - -- A virtual inherited property can be overridden in a derived class by including a property declaration that uses the `override` modifier. - -## Example - In this example, the `Shape` class contains the two coordinates `x`, `y`, and the `Area()` virtual method. Different shape classes such as `Circle`, `Cylinder`, and `Sphere` inherit the `Shape` class, and the surface area is calculated for each figure. Each derived class has its own override implementation of `Area()`. - - Notice that the inherited classes `Circle`, `Sphere`, and `Cylinder` all use constructors that initialize the base class, as shown in the following declaration. - -```csharp -public Cylinder(double r, double h): base(r, h) {} -``` - - The following program calculates and displays the appropriate area for each figure by invoking the appropriate implementation of the `Area()` method, according to the object that is associated with the method. - - [!code-csharp[csrefKeywordsModifiers#23](../../../csharp/language-reference/keywords/codesnippet/CSharp/virtual_2.cs)] - -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See Also - -- [C# Reference](../../../csharp/language-reference/index.md) -- [C# Programming Guide](../../../csharp/programming-guide/index.md) -- [Modifiers](../../../csharp/language-reference/keywords/modifiers.md) -- [C# Keywords](../../../csharp/language-reference/keywords/index.md) -- [Polymorphism](../../../csharp/programming-guide/classes-and-structs/polymorphism.md) -- [abstract](../../../csharp/language-reference/keywords/abstract.md) -- [override](../../../csharp/language-reference/keywords/override.md) -- [new](../../../csharp/language-reference/keywords/new.md) + +The `virtual` keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it: + +```csharp +public virtual double Area() +{ + return x * y; +} +``` + +The implementation of a virtual member can be changed by an [overriding member](override.md) in a derived class. For more information about how to use the `virtual` keyword, see [Versioning with the Override and New Keywords](../../programming-guide/classes-and-structs/versioning-with-the-override-and-new-keywords.md) and [Knowing When to Use Override and New Keywords](../../programming-guide/classes-and-structs/knowing-when-to-use-override-and-new-keywords.md). + +## Remarks + +When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. + +By default, methods are non-virtual. You cannot override a non-virtual method. + +You cannot use the `virtual` modifier with the `static`, `abstract`, `private`, or `override` modifiers. The following example shows a virtual property: + +[!code-csharp[csrefKeywordsModifiers#26](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#26)] + +Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax. + +- It is an error to use the `virtual` modifier on a static property. + +- A virtual inherited property can be overridden in a derived class by including a property declaration that uses the `override` modifier. + +## Example + +In this example, the `Shape` class contains the two coordinates `x`, `y`, and the `Area()` virtual method. Different shape classes such as `Circle`, `Cylinder`, and `Sphere` inherit the `Shape` class, and the surface area is calculated for each figure. Each derived class has its own override implementation of `Area()`. + +Notice that the inherited classes `Circle`, `Sphere`, and `Cylinder` all use constructors that initialize the base class, as shown in the following declaration. + +```csharp +public Cylinder(double r, double h): base(r, h) {} +``` + +The following program calculates and displays the appropriate area for each figure by invoking the appropriate implementation of the `Area()` method, according to the object that is associated with the method. + +[!code-csharp[csrefKeywordsModifiers#23](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#23)] + +## C# language specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] + +## See also + +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [Modifiers](modifiers.md) +- [C# Keywords](index.md) +- [Polymorphism](../../programming-guide/classes-and-structs/polymorphism.md) +- [abstract](abstract.md) +- [override](override.md) +- [new](new.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/void.md b/docs/csharp/language-reference/keywords/void.md index c8c6c386f1acd..8ccf9c7a7e9dc 100644 --- a/docs/csharp/language-reference/keywords/void.md +++ b/docs/csharp/language-reference/keywords/void.md @@ -11,6 +11,7 @@ helpviewer_keywords: ms.assetid: 0d2d8a95-fe20-4fbd-bf5d-c1e54bce71d4 --- # void (C# Reference) + When used as the return type for a method, `void` specifies that the method doesn't return a value. `void` isn't allowed in the parameter list of a method. A method that takes no parameters and returns no value is declared as follows: @@ -22,19 +23,20 @@ public void SampleMethod() } ``` -`void` is also used in an unsafe context to declare a pointer to an unknown type. For more information, see [Pointer types](../../../csharp/programming-guide/unsafe-code-pointers/pointer-types.md). +`void` is also used in an unsafe context to declare a pointer to an unknown type. For more information, see [Pointer types](../../programming-guide/unsafe-code-pointers/pointer-types.md). `void` is an alias for the .NET Framework type. -## C# Language Specification - [!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] +## C# language specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] ## 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) -- [Reference Types](../../../csharp/language-reference/keywords/reference-types.md) -- [Value Types](../../../csharp/language-reference/keywords/value-types.md) -- [Methods](../../../csharp/programming-guide/classes-and-structs/methods.md) -- [Unsafe Code and Pointers](../../../csharp/programming-guide/unsafe-code-pointers/index.md) +- [C# Reference](../index.md) +- [C# Programming Guide](../../programming-guide/index.md) +- [C# Keywords](index.md) +- [Reference Types](reference-types.md) +- [Value Types](value-types.md) +- [Methods](../../programming-guide/classes-and-structs/methods.md) +- [Unsafe Code and Pointers](../../programming-guide/unsafe-code-pointers/index.md) \ No newline at end of file diff --git a/docs/csharp/language-reference/keywords/volatile.md b/docs/csharp/language-reference/keywords/volatile.md index 8e46b314211e0..dac48fbf35971 100644 --- a/docs/csharp/language-reference/keywords/volatile.md +++ b/docs/csharp/language-reference/keywords/volatile.md @@ -13,37 +13,37 @@ ms.assetid: 78089bc7-7b38-4cfd-9e49-87ac036af009 # volatile (C# Reference) The `volatile` keyword indicates that a field might be modified by multiple threads that are executing at the same time. The compiler, the runtime system, and even hardware may rearrange reads and writes to memory locations for performance reasons. Fields that are declared `volatile` are not subject to these optimizations. Adding the `volatile` modifier ensures that all threads will observe volatile writes performed by any other thread in the order in which they were performed. There is no guarantee of a single total ordering of volatile writes as seen from all threads of execution. - -The `volatile` keyword can be applied to fields of these types: - -- Reference types. -- Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile." -- Simple types such as `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `char`, `float`, and `bool`. -- An `enum` type with one of the following base types: `byte`, `sbyte`, `short`, `ushort`, `int`, or `uint`. + +The `volatile` keyword can be applied to fields of these types: + +- Reference types. +- Pointer types (in an unsafe context). Note that although the pointer itself can be volatile, the object that it points to cannot. In other words, you cannot declare a "pointer to volatile." +- Simple types such as `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `char`, `float`, and `bool`. +- An `enum` type with one of the following base types: `byte`, `sbyte`, `short`, `ushort`, `int`, or `uint`. - Generic type parameters known to be reference types. -- and . +- and . Other types, including `double` and `long`, cannot be marked `volatile` because reads and writes to fields of those types cannot be guaranteed to be atomic. To protect multi-threaded access to those types of fields, use the class members or protect access using the [`lock`](lock-statement.md) statement. The `volatile` keyword can only be applied to fields of a `class` or `struct`. Local variables cannot be declared `volatile`. - + ## Example -The following example shows how to declare a public field variable as `volatile`. - +The following example shows how to declare a public field variable as `volatile`. + [!code-csharp[declareVolatile](~/samples/snippets/csharp/language-reference/keywords/volatile/Program.cs#Declaration)] The following example demonstrates how an auxiliary or worker thread can be created and used to perform processing in parallel with that of the primary thread. For more information about multithreading, see [Managed Threading](../../../standard/threading/index.md). - + [!code-csharp[declareVolatile](~/samples/snippets/csharp/language-reference/keywords/volatile/Program.cs#Volatile)] With the `volatile` modifier added to the declaration of `_shouldStop` in place, you'll always get the same results (similar to the excerpt shown in the preceding code). However, without that modifier on the `_shouldStop` member, the behavior is unpredictable. The `DoWork` method may optimize the member access, resulting in reading stale data. Because of the nature of multi-threaded programming, the number of stale reads is unpredictable. Different runs of the program will produce somewhat different results. -## C# Language Specification +## C# language specification + +[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] -[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)] - -## See Also +## See also - [C# language specification: volatile keyword](../../../../_csharplang/spec/classes.md#volatile-fields) - [C# Reference](../index.md) @@ -51,4 +51,4 @@ With the `volatile` modifier added to the declaration of `_shouldStop` in place, - [C# Keywords](index.md) - [Modifiers](modifiers.md) - [lock statement](lock-statement.md) -- class +- class \ No newline at end of file