|
2 | 2 | title: Basic Types | C# Guide |
3 | 3 | description: Learn about the core types (numerics, strings, and object) in all C# programs |
4 | 4 | keywords: .NET, .NET Core, C# |
5 | | -author: dotnet-bot |
| 5 | +author: stevehoag |
6 | 6 | manager: wpickett |
7 | | -ms.date: 06/25/2016 |
| 7 | +ms.date: 10/10/2016 |
8 | 8 | ms.topic: article |
9 | 9 | ms.prod: .net-core |
10 | 10 | ms.technology: .net-core-technologies |
11 | 11 | ms.devlang: csharp |
12 | 12 | ms.assetid: 95c686ba-ae4f-440e-8e94-0dbd6e04d11f |
13 | 13 | --- |
14 | 14 |
|
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. |
16 | 40 |
|
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) |
0 commit comments