I have written a small code, but found something amazing. I have a class name Students and inside that calss declared a List<> like this with variable name Students (same as the Class name)
Class Students
{
private String Name;
private int Age;
public Students(){}
List<Students> Students = new List<Students>();
...
}
Here compile time error is
'Students': member names can not be the same as their enclosing type
But if I declare the same List in other class ... like
Class Students
{
private String Name;
private int Age;
public Students(){}
...
}
Class Program
{
....
List<Students> Students = new List<Students>();
Students.Add(new Students("Deb","B++"));
Students.Add(new Students("DDD", "A++"));
............
}
This Works fine. My Question is, why? How we can create custom variable as the class name in other calsses but not in the same class? Any elaborate answer would be good, as I want to gain knowledge in this.