Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
4.2 Casting
//4.2 Casting using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //Casting is converting one type to another, speaking simply. //Only some types can be converted to any given type int a = 5; bool b = true; //a = (int)b; //nope long l = 15; l = (long)a; double d = 5.5; l = (long)d; Console.WriteLine(l); Apple apple = new Apple(); Fruit fruit = new Fruit(); //apple = (Apple)fruit; //nope, Fruit is not an Apple fruit = (Fruit)apple; Console.WriteLine(fruit.GetMyName()); //checking whether can convert var fr = new Fruit(); var ap = new Apple(); Console.WriteLine(fr as Apple == null); //can't Console.WriteLine(ap as Fruit == null); //can } } public class Fruit { public void EatMe() { Console.WriteLine("Eating..."); } public string GetMyName() { return "Some fruit"; } } public class Apple : Fruit { public string GetMyName() { return "Apple"; } } }
run
|
edit
|
history
|
help
0
C# Basic learning
cff
4x4 ver 3
Overriding
25
sintildes
cs002
Doubt-" 1 parameter Gen _Derive_Class inherit the 2 parameter Gen_Base_Class"
Testing 008
sdfghyjujyhtgrfedcf