Skip to content

Commit 760a590

Browse files
Youssef1313Ron Petrusha
authored andcommitted
Update cs0266.md (#14653)
* Update cs0266.md * Update cs0266.md
1 parent ca96dca commit 760a590

File tree

1 file changed

+41
-40
lines changed
  • docs/csharp/language-reference/compiler-messages

1 file changed

+41
-40
lines changed

docs/csharp/language-reference/compiler-messages/cs0266.md

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
---
22
title: "Compiler Error CS0266"
33
ms.date: 07/20/2015
4-
f1_keywords:
4+
f1_keywords:
55
- "CS0266"
6-
helpviewer_keywords:
6+
helpviewer_keywords:
77
- "CS0266"
88
ms.assetid: 6cca5d6a-f3e0-482a-af25-af73bfe3e303
99
---
1010
# 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.
3031
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;
5051
}
5152
}
5253

0 commit comments

Comments
 (0)