-
-
Save CarterTsai/de72754a7901bc76bdb9e0d869507e6b to your computer and use it in GitHub Desktop.
testInherit.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public interface BaseClass | |
{ | |
string baseName {get; set;} | |
} | |
public class A : BaseClass | |
{ | |
public string baseName {get; set;} | |
public string aName {get; set;} | |
} | |
public class B : BaseClass | |
{ | |
public string baseName {get; set;} | |
public string bName {get; set;} | |
} | |
public static string Hello<T>(T data)where T: BaseClass | |
{ | |
if(data.baseName == "A"){ | |
var d = (A)(object) data; | |
return "Hello " + d.aName; | |
} | |
if(data.baseName == "B"){ | |
var d = (B)(object) data; | |
return "Hello " + d.bName; | |
} | |
return ""; | |
} | |
public static void Main() | |
{ | |
var x = new A(); | |
x.baseName = "A"; | |
x.aName = "A Name"; | |
var h = Program.Hello<A>(x); | |
Console.WriteLine(h); | |
var y = new B(); | |
y.baseName = "B"; | |
y.bName = "B Name"; | |
var J = Program.Hello<B>(y); | |
Console.WriteLine(J); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment