Skip to content

Commit a4c76ed

Browse files
author
Ron Petrusha
authored
Merge pull request dotnet#1417 from rpetrusha/fixcode
Replaced incorrect code example.
2 parents 98d67c4 + 63957e5 commit a4c76ed

File tree

2 files changed

+52
-6
lines changed
  • docs/csharp/programming-guide/classes-and-structs
  • samples/snippets/csharp/programming-guide/classes-and-structs

2 files changed

+52
-6
lines changed

docs/csharp/programming-guide/classes-and-structs/index.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
---
22
title: "Classes and Structs (C# Programming Guide) | Microsoft Docs"
3-
4-
ms.date: "2015-07-20"
3+
description: Describes the use of classes and structures (structs) in C#.
4+
keywords: classes (C#), structs (C#), structures (structs) (C#), reference types (C#), value types (C#)
5+
ms.date: "2016-01-17"
56
ms.prod: .net
6-
7-
87
ms.technology:
98
- "devlang-csharp"
109

@@ -52,9 +51,9 @@ Classes and structs are two of the basic constructs of the common type system in
5251
For more information, see [Classes](../../../csharp/programming-guide/classes-and-structs/classes.md), [Objects](../../../csharp/programming-guide/classes-and-structs/objects.md), and [Structs](../../../csharp/programming-guide/classes-and-structs/structs.md).
5352

5453
## Example
55-
In the following example, `MyCustomClass` is defined with three members at the top level of the `ProgrammingGuide` namespace. An instance (object) of `MyCustomClass` is created in the `Main` method in the `Program` class, and the object’s methods and properties are accessed by using dot notation.
54+
In the following example, `CustomClass` in the `ProgrammingGuide` namespace has three members: an instance constructor, a property named `Number`, and a method named `Multiply`. The `Main` method in the `Program` class creates an instance (object) of `CustomClass`, and the object’s method and property are accessed by using dot notation.
5655

57-
[!code-cs[csProgGuideObjects#88](../../../csharp/programming-guide/classes-and-structs/codesnippet/CSharp/index_1.cs)]
56+
[!code-cs[csProgGuideObjects#1](../../../../samples/snippets/csharp/programming-guide/classes-and-structs/class1.cs#1)]
5857

5958
## Encapsulation
6059
*Encapsulation* is sometimes referred to as the first pillar or principle of object-oriented programming. According to the principle of encapsulation, a class or struct can specify how accessible each of its members is to code outside of the class or struct. Methods and variables that are not intended to be used from outside of the class or assembly can be hidden to limit the potential for coding errors or malicious exploits.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// <Snippet1>
2+
using System;
3+
4+
namespace ProgrammingGuide
5+
{
6+
// Class definition.
7+
public class CustomClass
8+
{
9+
// Class members.
10+
//
11+
// Property.
12+
public int Number { get; set; }
13+
14+
// Method.
15+
public int Multiply(int num)
16+
{
17+
return num * Number;
18+
}
19+
20+
// Instance Constructor.
21+
public CustomClass()
22+
{
23+
Number = 0;
24+
}
25+
}
26+
27+
// Another class definition that contains Main, the program entry point.
28+
class Program
29+
{
30+
static void Main(string[] args)
31+
{
32+
// Create an object of type CustomClass.
33+
CustomClass custClass = new CustomClass();
34+
35+
// Set the value of the public property.
36+
custClass.Number = 27;
37+
38+
// Call the public method.
39+
int result = custClass.Multiply(4);
40+
Console.WriteLine($"The result is {result}.");
41+
}
42+
}
43+
}
44+
// The example displays the following output:
45+
// The result is 108.
46+
// </Snippet1>
47+

0 commit comments

Comments
 (0)