Skip to content

Commit 405525b

Browse files
authored
Basic types, classes and structs (#1134)
* Basic types, classes and structs Split Types topic and added C# 7 literala features * Fixed snippet links * Fixed two more links * Addressed all review comments * Removed wrenches again after esolving conflict
1 parent d4b07e9 commit 405525b

19 files changed

+326
-46
lines changed

docs/csharp/basic-types.md

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,84 @@
22
title: Basic Types | C# Guide
33
description: Learn about the core types (numerics, strings, and object) in all C# programs
44
keywords: .NET, .NET Core, C#
5-
author: dotnet-bot
5+
author: stevehoag
66
manager: wpickett
7-
ms.date: 06/25/2016
7+
ms.date: 10/10/2016
88
ms.topic: article
99
ms.prod: .net-core
1010
ms.technology: .net-core-technologies
1111
ms.devlang: csharp
1212
ms.assetid: 95c686ba-ae4f-440e-8e94-0dbd6e04d11f
1313
---
1414

15-
# 🔧 Basic Types
15+
# Types, variables, and values
16+
C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method signature specifies a type for each input parameter and for the return value. The .NET Framework class library defines a set of built-in numeric types as well as more complex types that represent a wide variety of logical constructs, such as the file system, network connections, collections and arrays of objects, and dates. A typical C# program uses types from the class library as well as user-defined types that model the concepts that are specific to the program's problem domain.
17+
18+
The information stored in a type can include the following:
19+
20+
- The storage space that a variable of the type requires.
21+
22+
- The maximum and minimum values that it can represent.
23+
24+
- The members (methods, fields, events, and so on) that it contains.
25+
26+
- The base type it inherits from.
27+
28+
- The location where the memory for variables will be allocated at run time.
29+
30+
- The kinds of operations that are permitted.
31+
32+
The compiler uses type information to make sure that all operations that are performed in your code are *type safe*. For example, if you declare a variable of type [int](https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx), the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type [bool](https://msdn.microsoft.com/en-us/library/c8f5xwh7.aspx), the compiler generates an error, as shown in the following example:
33+
34+
[!code-csharp[Type Safety](../../samples/snippets/csharp/concepts/basic-types/type-safety.cs)]
35+
36+
> [!NOTE]
37+
> C and C++ developers, notice that in C#, [bool](https://msdn.microsoft.com/en-us/library/c8f5xwh7.aspx) is not convertible to [int](https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx).
38+
39+
The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory.
1640

17-
> **Note**
18-
>
19-
> This topic hasn’t been written yet!
20-
>
21-
> We welcome your input to help shape the scope and approach. You can track the status and provide input on this
22-
> [issue](https://github.com/dotnet/core-docs/issues/963) at GitHub.
23-
>
24-
> If you would like to review early drafts and outlines of this topic, please leave a note with your contact information in the issue.
25-
>
26-
> Learn more about how you can contribute on [GitHub](https://github.com/dotnet/core-docs/blob/master/CONTRIBUTING.md).
27-
>
41+
## Specifying types in variable declarations
42+
When you declare a variable or constant in a program, you must either specify its type or use the [var](https://msdn.microsoft.com/en-us/library/bb383973.aspx) keyword to let the compiler infer the type. The following example shows some variable declarations that use both built-in numeric types and complex user-defined types:
43+
44+
[!code-csharp[Variable Declaration](../../samples/snippets/csharp/concepts/basic-types/variable-declaration.cs)]
45+
46+
The types of method parameters and return values are specified in the method signature. The following signature shows a method that requires an [int](https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx) as an input argument and returns a string:
47+
48+
[!code-csharp[Method Signature](../../samples/snippets/csharp/concepts/basic-types/method-signature.cs)]
49+
50+
After a variable is declared, it cannot be re-declared with a new type, and it cannot be assigned a value that is not compatible with its declared type. For example, you cannot declare an [int](https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx) and then assign it a Boolean value of [true](https://msdn.microsoft.com/en-us/library/06d3w013.aspx). However, values can be converted to other types, for example when they are assigned to new variables or passed as method arguments. A *type conversion* that does not cause data loss is performed automatically by the compiler. A conversion that might cause data loss requires a *cast* in the source code.
51+
52+
For more information, see [Casting and type conversions](https://msdn.microsoft.com/en-us/library/ms173105.aspx).
53+
54+
## Built-in types
55+
C# provides a standard set of built-in numeric types to represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in **string** and **object** types. These are available for you to use in any C# program. For a more information about the built-in types, see [Types reference tables](https://msdn.microsoft.com/en-us/library/1dhd7f2x.aspx).
56+
57+
## Custom types
58+
You use the [struct](https://msdn.microsoft.com/en-us/library/ah19swz4.aspx), [class](https://msdn.microsoft.com/en-us/library/0b0thckt.aspx), [interface](https://msdn.microsoft.com/en-us/library/87d83y5b.aspx), and [enum](https://msdn.microsoft.com/en-us/library/sbbt4032.aspx) constructs to create your own custom types. The .NET Framework class library itself is a collection of custom types provided by Microsoft that you can use in your own applications. By default, the most frequently used types in the class library are available in any C# program. Others become available only when you explicitly add a project reference to the assembly in which they are defined. After the compiler has a reference to the assembly, you can declare variables (and constants) of the types declared in that assembly in source code. For more information, see [.NET Framework class library](https://msdn.microsoft.com/en-us/library/gg145045(v=vs.110).aspx).
59+
60+
## Generic types
61+
A type can be declared with one or more *type parameters* that serve as a placeholder for the actual type (the *concrete type*) that client code will provide when it creates an instance of the type. Such types are called *generic types*. For example, the .NET Framework type @System.Collections.Generic.List%601 has one type parameter that by convention is given the name *T*. When you create an instance of the type, you specify the type of the objects that the list will contain, for example, string:
62+
63+
[!code-csharp[Generic types](../../samples/snippets/csharp/concepts/basic-types/generic-type.cs)]
64+
65+
The use of the type parameter makes it possible to reuse the same class to hold any type of element, without having to convert each element to [object](https://msdn.microsoft.com/en-us/library/9kkx3h3c.aspx). Generic collection classes are called *strongly-typed collections* because the compiler knows the specific type of the collection's elements and can raise an error at compile-time if, for example, you try to add an integer to the `strings` object in the previous example. For more information, see [Generics](generics.md).
66+
67+
## Implicit types, anonymous types, and tuple types
68+
As stated previously, you can implicitly type a local variable (but not class members) by using the [var](https://msdn.microsoft.com/en-us/library/bb383973.aspx) keyword. The variable still receives a type at compile time, but the type is provided by the compiler. For more information, see [Implicitly typed local variables](https://msdn.microsoft.com/en-us/library/bb384061.aspx).
69+
70+
In some cases, it is inconvenient to create a named type for simple sets of related values that you do not intend to store or pass outside method boundaries. You can create *anonymous types* for this purpose. For more information, see [Anonymous types](https://msdn.microsoft.com/en-us/library/bb397696.aspx).
71+
72+
It's common to want to return more than one value from a method. You can create *tuple types* that return multiple values in a single method call. For more information, see [Tuples](tuples.md)
73+
74+
## The Common type system
75+
It is important to understand two fundamental points about the type system in the .NET Framework:
76+
77+
- It supports the principle of inheritance. Types can derive from other types, called *base types*. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as @System.Int32 (C# keyword: `int`), derive ultimately from a single base type, which is @System.Object (C# keyword: `object`). This unified type hierarchy is called the [Common type system](../standard/common-type-system.md) (CTS). For more information about inheritance in C#, see [Inheritance](https://msdn.microsoft.com/en-us/library/ms173149.aspx).
78+
79+
- Each type in the CTS is defined as either a *value type* or a *reference type*. This includes all custom types in the .NET Framework class library and also your own user-defined types. Types that you define by using the [struct](https://msdn.microsoft.com/en-us/library/ah19swz4.aspx) keyword are value types; all the built-in numeric types are **structs**. For more information about value types, see [Structs](structs.md). Types that you define by using the [class](https://msdn.microsoft.com/en-us/library/0b0thckt.aspx) keyword are reference types. For more information about reference types, see [Classes](classes.md). Reference types and value types have different compile-time rules, and different run-time behavior.
80+
81+
82+
## See also
83+
[Structs](structs.md)
84+
85+
[Classes](classes.md)

docs/csharp/classes.md

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,80 @@
22
title: Classes | C# Guide
33
description: Learn about the class types and how you create them
44
keywords: .NET, .NET Core, C#
5-
author: dotnet-bot
5+
author: stevehoag
66
manager: wpickett
7-
ms.date: 06/25/2016
7+
ms.date: 10/10/2016
88
ms.topic: article
99
ms.prod: .net-core
1010
ms.technology: .net-core-technologies
1111
ms.devlang: csharp
1212
ms.assetid: 95c686ba-ae4f-440e-8e94-0dbd6e04d11f
1313
---
1414

15-
# 🔧 Classes
15+
# Classes
16+
A *class* is a construct that enables you to create your own custom types by grouping together variables of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type. If the class is not declared as static, client code can use it by creating *objects* or *instances* which are assigned to a variable. The variable remains in memory until all references to it go out of scope. At that time, the CLR marks it as eligible for garbage collection. If the class is declared as [static](https://msdn.microsoft.com/en-us/library/98f28cdx.aspx), then only one copy exists in memory and client code can only access it through the class itself, not an *instance variable*. For more information, see [Static classes and static class members](https://msdn.microsoft.com/en-us/library/79b3xss3.aspx).
17+
18+
## Reference types
19+
A type that is defined as a [class](https://msdn.microsoft.com/en-us/library/0b0thckt.aspx) is a *reference type*. At run time, when you declare a variable of a reference type, the variable contains the value [null](https://msdn.microsoft.com/en-us/library/edakx9da.aspx) until you explicitly create an instance of the object by using the [new](https://msdn.microsoft.com/en-us/library/51y09td4.aspx) operator, or assign it an object that has been created elsewhere by using [new](https://msdn.microsoft.com/en-us/library/51y09td4.aspx), as shown in the following example:
20+
21+
[!code-csharp[Reference Types](../../samples/snippets/csharp/concepts/classes/reference-type.cs)]
22+
23+
When the object is created, the memory is allocated on the managed heap, and the variable holds only a reference to the location of the object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as *garbage collection*. However, garbage collection is also highly optimized, and in most scenarios, it does not create a performance issue. For more information about garbage collection, see [Automatic memory management and garbage collection](../standard/garbagecollection/gc.md).
24+
25+
Reference types fully support *inheritance*, a fundamental characteristic of object-oriented programming. When you create a class, you can inherit from any other interface or class that is not defined as [sealed](https://msdn.microsoft.com/en-us/library/88c54tsw.aspx), and other classes can inherit from your class and override your virtual methods. For more information, see [Inheritance](https://msdn.microsoft.com/en-us/library/ms173149.aspx).
26+
27+
## Declaring classes
28+
Classes are declared by using the [class](https://msdn.microsoft.com/en-us/library/0b0thckt.aspx) keyword, as shown in the following example:
29+
30+
[!code-csharp[Declaring Classes](../../samples/snippets/csharp/concepts/classes/declaring-classes.cs)]
31+
32+
The **class** keyword is preceded by the access modifier. Because [public](https://msdn.microsoft.com/en-us/library/yzh058ae.aspx) is used in this case, anyone can create objects from this class. The name of the class follows the **class** keyword. The remainder of the definition is the class body, where the behavior and data are defined. Fields, properties, methods, and events on a class are collectively referred to as *class members*.
33+
34+
## Creating objects
35+
A class defines a type of object, but it is not an object itself. An object is a concrete entity based on a class, and is sometimes referred to as an instance of a class.
36+
37+
Objects can be created by using the [new](https://msdn.microsoft.com/en-us/library/51y09td4.aspx) keyword followed by the name of the class that the object will be based on, like this:
38+
39+
[!code-csharp[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects.cs)]
40+
41+
When an instance of a class is created, a reference to the object is passed back to the programmer. In the previous example, `object1` is a reference to an object that is based on `Customer`. This reference refers to the new object but does not contain the object data itself. In fact, you can create an object reference without creating an object at all:
42+
43+
[!code-csharp[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects2.cs)]
44+
45+
We do not recommend creating object references such as this one that does not refer to an object because trying to access an object through such a reference will fail at run time. However, such a reference can be made to refer to an object, either by creating a new object, or by assigning it to an existing object, such as this:
46+
47+
[!code-csharp[Creating Objects](../../samples/snippets/csharp/concepts/classes/creating-objects3.cs)]
48+
49+
This code creates two object references that both refer to the same object. Therefore, any changes to the object made through `object3` will be reflected in subsequent uses of `object4`. Because objects that are based on classes are referred to by reference, classes are known as reference types.
50+
51+
## Class inheritance
52+
Inheritance is accomplished by using a *derivation*, which means a class is declared by using a *base class* from which it inherits data and behavior. A base class is specified by appending a colon and the name of the base class following the derived class name, like this:
53+
54+
[!code-csharp[Inheritance](../../samples/snippets/csharp/concepts/classes/inheritance.cs)]
55+
56+
When a class declares a base class, it inherits all the members of the base class except the constructors.
57+
58+
Unlike C++, a class in C# can only directly inherit from one base class. However, because a base class may itself inherit from another class, a class may indirectly inherit multiple base classes. Furthermore, a class can directly implement more than one interface. For more information, see [Interfaces](interfaces.md).
59+
60+
A class can be declared [abstract](https://msdn.microsoft.com/en-us/library/sf985hc5.aspx). An abstract class contains abstract methods that have a signature definition but no implementation. Abstract classes cannot be instantiated. They can only be used through derived classes that implement the abstract methods. By contrast, a [sealed](https://msdn.microsoft.com/en-us/library/88c54tsw.aspx) class does not allow other classes to derive from it. For more information, see [Abstract and sealed classes and class members](https://msdn.microsoft.com/en-us/library/ms173150.aspx).
61+
62+
Class definitions can be split between different source files. For more information, see [Partial class definitions](https://msdn.microsoft.com/en-us/library/wa80x488.aspx).
63+
64+
65+
## Example
66+
In the following example, a public class that contains a single field, a method, and a special method called a constructor is defined. For more information, see [Constructors](https://msdn.microsoft.com/en-us/library/ace5hbzh.aspx). The class is then instantiated with the **new** keyword.
67+
68+
[!code-csharp[Class Example](../../samples/snippets/csharp/concepts/classes/class-example.cs)]
69+
70+
## C# language specification
71+
For more information, see the [C# language specification](https://msdn.microsoft.com/en-us/library/ms228593.aspx). The language specification is the definitive source for C# syntax and usage.
72+
73+
## See also
74+
[C# programming guide](https://msdn.microsoft.com/en-us/library/67ef8sbd.aspx)
75+
[Polymorphism](https://msdn.microsoft.com/en-us/library/ms173152.aspx)
76+
[Class and struct members](https://msdn.microsoft.com/en-us/library/ms173113.aspx)
77+
[Class and struct methods](https://msdn.microsoft.com/en-us/library/ms173114.aspx)
78+
[Constructors](https://msdn.microsoft.com/en-us/library/ace5hbzh.aspx)
79+
[Destructors](https://msdn.microsoft.com/en-us/library/66x5fx1b.aspx)
80+
[Objects](https://msdn.microsoft.com/en-us/library/ms173110.aspx)
1681

17-
> **Note**
18-
>
19-
> This topic hasn’t been written yet!
20-
>
21-
> We welcome your input to help shape the scope and approach. You can track the status and provide input on this
22-
> [issue](https://github.com/dotnet/core-docs/issues/964) at GitHub.
23-
>
24-
> If you would like to review early drafts and outlines of this topic, please leave a note with your contact information in the issue.
25-
>
26-
> Learn more about how you can contribute on [GitHub](https://github.com/dotnet/core-docs/blob/master/CONTRIBUTING.md).
27-
>

0 commit comments

Comments
 (0)