Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
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
Please
log in
to post a comment.
Lat/Lon with buffer
DB Search
test
bla bla
pattern2
Random number generator with IF
1
Fórum Parallel Threads ( Without Fun )
Nested Namespace
Contravariance
stackse - search stackoverflow differently
Please log in to post a comment.