|
1 | 1 | ---
|
2 | 2 | title: "Compiler Error CS0266"
|
3 | 3 | ms.date: 07/20/2015
|
4 |
| -f1_keywords: |
| 4 | +f1_keywords: |
5 | 5 | - "CS0266"
|
6 |
| -helpviewer_keywords: |
| 6 | +helpviewer_keywords: |
7 | 7 | - "CS0266"
|
8 | 8 | ms.assetid: 6cca5d6a-f3e0-482a-af25-af73bfe3e303
|
9 | 9 | ---
|
10 | 10 | # Compiler Error CS0266
|
11 |
| -Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you missing a cast?) |
12 |
| - |
13 |
| - This error occurs when your code tries to convert between two types that cannot be implicitly converted, but where an explicit conversion is available. For more information, see [Casting and Type Conversions](../../programming-guide/types/casting-and-type-conversions.md). |
14 |
| - |
15 |
| - The following code shows examples that generate CS0266. |
16 |
| - |
17 |
| -```csharp |
18 |
| -// CS0266.cs |
19 |
| -class MyClass |
20 |
| -{ |
21 |
| - public static void Main() |
22 |
| - { |
23 |
| - // You cannot implicitly convert a double to an integer. |
24 |
| - double d = 3.2; |
25 |
| - |
26 |
| - // The following line causes compiler error CS0266. |
27 |
| - int i1 = d; |
28 |
| - |
29 |
| - // However, you can resolve the error by using an explicit conversion. |
| 11 | + |
| 12 | +Cannot implicitly convert type 'type1' to 'type2'. An explicit conversion exists (are you missing a cast?) |
| 13 | + |
| 14 | + This error occurs when your code tries to convert between two types that cannot be implicitly converted, but where an explicit conversion is available. For more information, see [Casting and Type Conversions](../../programming-guide/types/casting-and-type-conversions.md). |
| 15 | + |
| 16 | + The following code shows examples that generate CS0266: |
| 17 | + |
| 18 | +```csharp |
| 19 | +// CS0266.cs |
| 20 | +class MyClass |
| 21 | +{ |
| 22 | + public static void Main() |
| 23 | + { |
| 24 | + // You cannot implicitly convert a double to an integer. |
| 25 | + double d = 3.2; |
| 26 | + |
| 27 | + // The following line causes compiler error CS0266. |
| 28 | + int i1 = d; |
| 29 | + |
| 30 | + // However, you can resolve the error by using an explicit conversion. |
30 | 31 | int i2 = (int)d;
|
31 |
| - |
32 |
| - // You cannot implicitly convert an object to a class type. |
33 |
| - object obj = "MyString"; |
34 |
| - |
35 |
| - // The following assignment statement causes error CS0266. |
36 |
| - MyClass myClass = obj; |
37 |
| - |
38 |
| - // You can resolve the error by using an explicit conversion. |
39 |
| - MyClass c = ( MyClass )obj; |
40 |
| - |
41 |
| - // You cannot implicitly convert a base class object to a derived class type. |
42 |
| - MyClass mc = new MyClass(); |
43 |
| - DerivedClass dc = new DerivedClass(); |
44 |
| - |
45 |
| - // The following line causes compiler error CS0266. |
46 |
| - dc = mc; |
47 |
| - |
48 |
| - // You can resolve the error by using an explicit conversion. |
49 |
| - dc = (DerivedClass)mc; |
| 32 | + |
| 33 | + // You cannot implicitly convert an object to a class type. |
| 34 | + object obj = new MyClass(); |
| 35 | + |
| 36 | + // The following assignment statement causes error CS0266. |
| 37 | + MyClass myClass = obj; |
| 38 | + |
| 39 | + // You can resolve the error by using an explicit conversion. |
| 40 | + MyClass c = (MyClass)obj; |
| 41 | + |
| 42 | + // You cannot implicitly convert a base class object to a derived class type. |
| 43 | + MyClass mc = new MyClass(); |
| 44 | + DerivedClass dc = new DerivedClass(); |
| 45 | + |
| 46 | + // The following line causes compiler error CS0266. |
| 47 | + dc = mc; |
| 48 | + |
| 49 | + // You can resolve the error by using an explicit conversion. |
| 50 | + dc = (DerivedClass)mc; |
50 | 51 | }
|
51 | 52 | }
|
52 | 53 |
|
|
0 commit comments