<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FClass_Interface%2FObject_Cast</id>
		<title>Csharp/C Sharp/Class Interface/Object Cast - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://nfex.ru/index.php?action=history&amp;feed=atom&amp;title=Csharp%2FC_Sharp%2FClass_Interface%2FObject_Cast"/>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Object_Cast&amp;action=history"/>
		<updated>2026-04-29T16:35:18Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Object_Cast&amp;diff=566&amp;oldid=prev</id>
		<title> в 15:31, 26 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Object_Cast&amp;diff=566&amp;oldid=prev"/>
				<updated>2010-05-26T15:31:18Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 15:31, 26 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Object_Cast&amp;diff=567&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://nfex.ru/index.php?title=Csharp/C_Sharp/Class_Interface/Object_Cast&amp;diff=567&amp;oldid=prev"/>
				<updated>2010-05-26T11:38:55Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Casting Objects==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
public class MotorVehicle {&lt;br /&gt;
    public string model;&lt;br /&gt;
    public MotorVehicle(string model) {&lt;br /&gt;
        this.model = model;&lt;br /&gt;
    }&lt;br /&gt;
    public void Start() {&lt;br /&gt;
        Console.WriteLine(model + &amp;quot; started&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class Product : MotorVehicle {&lt;br /&gt;
    public bool convertible;&lt;br /&gt;
    public Product(string model, bool convertible) :&lt;br /&gt;
        base(model) {&lt;br /&gt;
        this.convertible = convertible;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Motorcycle : MotorVehicle {&lt;br /&gt;
    public bool sidecar;&lt;br /&gt;
    public Motorcycle(string model, bool sidecar) :&lt;br /&gt;
        base(model) {&lt;br /&gt;
        this.sidecar = sidecar;&lt;br /&gt;
    }&lt;br /&gt;
    public void PullWheelie() {&lt;br /&gt;
        Console.WriteLine(model + &amp;quot; pulling a wheelie!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
class MainClass {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        Product myProduct = new Product(&amp;quot;MR2&amp;quot;, true);&lt;br /&gt;
        MotorVehicle myMotorVehicle = (MotorVehicle)myProduct;&lt;br /&gt;
        Console.WriteLine(&amp;quot;myMotorVehicle.model = &amp;quot; + myMotorVehicle.model);&lt;br /&gt;
        myMotorVehicle.Start();&lt;br /&gt;
        Motorcycle myMotorcycle = new Motorcycle(&amp;quot;V-Rod&amp;quot;, true);&lt;br /&gt;
        MotorVehicle myMotorVehicle2 = (MotorVehicle)myMotorcycle;&lt;br /&gt;
        Console.WriteLine(&amp;quot;myMotorVehicle2.model =&amp;quot; + myMotorVehicle2.model);&lt;br /&gt;
        myMotorVehicle2.Start();&lt;br /&gt;
        Motorcycle myMotorcycle2 = (Motorcycle)myMotorVehicle2;&lt;br /&gt;
        Console.WriteLine(&amp;quot;myMotorcycle2.model = &amp;quot; + myMotorcycle2.model);&lt;br /&gt;
        Console.WriteLine(&amp;quot;myMotorcycle2.sidecar = &amp;quot; + myMotorcycle2.sidecar);&lt;br /&gt;
        myMotorcycle2.Start();&lt;br /&gt;
        myMotorcycle2.PullWheelie();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Casting objects: downcast==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
   &lt;br /&gt;
public class CPU {&lt;br /&gt;
  public string model;&lt;br /&gt;
   &lt;br /&gt;
  public CPU(string model) {&lt;br /&gt;
    this.model = model;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  public void Start() {&lt;br /&gt;
    Console.WriteLine(model + &amp;quot; started&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
public class Intel : CPU {&lt;br /&gt;
  public bool convertible;&lt;br /&gt;
   &lt;br /&gt;
  public Intel(string model, bool convertible) : base(model) {&lt;br /&gt;
    this.convertible = convertible;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
public class AMD : CPU {&lt;br /&gt;
  public bool sidecar;&lt;br /&gt;
   &lt;br /&gt;
  public AMD(string model, bool sidecar) : base(model) {&lt;br /&gt;
    this.sidecar = sidecar;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  public void PullWheelie() {&lt;br /&gt;
    Console.WriteLine(model + &amp;quot; pulling a wheelie!&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
class Test {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    Intel myIntel = new Intel(&amp;quot;MR2&amp;quot;, true);&lt;br /&gt;
   &lt;br /&gt;
    // create a AMD object&lt;br /&gt;
    AMD myAMD = new AMD(&amp;quot;V-Rod&amp;quot;, true);&lt;br /&gt;
   &lt;br /&gt;
    // cast myAMD to CPU (upcast)&lt;br /&gt;
    CPU myCPU2 = (CPU) myAMD;&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
    // cast myCPU2 to AMD (downcast)&lt;br /&gt;
    AMD myAMD2 = (AMD) myCPU2;&lt;br /&gt;
   &lt;br /&gt;
    // myMotorCycle2 has access to all members of the AMD class&lt;br /&gt;
    Console.WriteLine(&amp;quot;myAMD2.model = &amp;quot; + myAMD2.model);&lt;br /&gt;
    Console.WriteLine(&amp;quot;myAMD2.sidecar = &amp;quot; + myAMD2.sidecar);&lt;br /&gt;
    myAMD2.Start();&lt;br /&gt;
    myAMD2.PullWheelie();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Casting objects: upcast==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
   &lt;br /&gt;
public class CPU {&lt;br /&gt;
  public string model;&lt;br /&gt;
   &lt;br /&gt;
  public CPU(string model) {&lt;br /&gt;
    this.model = model;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  public void Start() {&lt;br /&gt;
    Console.WriteLine(model + &amp;quot; started&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
public class Intel : CPU {&lt;br /&gt;
  public bool convertible;&lt;br /&gt;
   &lt;br /&gt;
  public Intel(string model, bool convertible) : base(model) {&lt;br /&gt;
    this.convertible = convertible;&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
public class AMD : CPU {&lt;br /&gt;
  public bool sidecar;&lt;br /&gt;
   &lt;br /&gt;
  public AMD(string model, bool sidecar) : base(model) {&lt;br /&gt;
    this.sidecar = sidecar;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  public void PullWheelie() {&lt;br /&gt;
    Console.WriteLine(model + &amp;quot; pulling a wheelie!&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
   &lt;br /&gt;
class Test {&lt;br /&gt;
  public static void Main() {&lt;br /&gt;
    Intel myIntel = new Intel(&amp;quot;MR2&amp;quot;, true);&lt;br /&gt;
   &lt;br /&gt;
    // cast myIntel to CPU (upcast)&lt;br /&gt;
    CPU myCPU = (CPU) myIntel;&lt;br /&gt;
   &lt;br /&gt;
    Console.WriteLine(&amp;quot;myCPU.model = &amp;quot; + myCPU.model);&lt;br /&gt;
    myCPU.Start();&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
       &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Downcast will fail.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
class Employee { }&lt;br /&gt;
   &lt;br /&gt;
class ContractEmployee : Employee { }&lt;br /&gt;
   &lt;br /&gt;
class CastExample3&lt;br /&gt;
{&lt;br /&gt;
    public static void Main ()&lt;br /&gt;
    {&lt;br /&gt;
        ContractEmployee ce = (ContractEmployee)new Employee(); &lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==This code raises an exception at run time because of an invalid cast==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;csharp&amp;quot;&amp;gt; &lt;br /&gt;
using System;&lt;br /&gt;
&lt;br /&gt;
public class Starter {&lt;br /&gt;
    public static void Main() {&lt;br /&gt;
        MyClass obj = new MyClass();&lt;br /&gt;
        // Fails at compile time&lt;br /&gt;
        // YClass alias=obj;&lt;br /&gt;
        // Fails at run time&lt;br /&gt;
        YClass alias = (YClass)obj;&lt;br /&gt;
        obj.MethodA();&lt;br /&gt;
        obj.MethodB();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class MyClass {&lt;br /&gt;
    public virtual void MethodA() {&lt;br /&gt;
    }&lt;br /&gt;
    public virtual void MethodB() {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
public class YClass : MyClass {&lt;br /&gt;
    public override void MethodA() {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
 &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>