Why doesn't this code (my altered example below) work? I just tried it in Linqpad (
LINQPad - The .NET Programmer's Playground[
^]);
void Main()
{
main m = new main();
main.f1 = "super";
fun1<main>();
Derive1 d = new Derive1();
fun1<Derive1>();
}
public class main{ static public string f1; }
public class Derive1: main{ }
public class Derive2 : main{ }
void fun1<T>() where T: main
{
string f = main.f1;
Console.WriteLine(f);
}
The output will be :
super
super
which is the value of the static variable. You cannot access the static the way you were doing it. It is a CLASS variable not an instance variable. Your way was accessing the instance var.
Edit
Altered code and added Derive1 instance and ran method with that class and it works too.
Now it outputs the value (super) twice.