public class clsb
{
public string subjectName = "";
public string mainsubject()
{
return subjectName;
}
}
public class clsA : clsb
{
public string subjectName = "";
public string Allsubject()
{
return subjectName;
}
}
private void getdata()
{
clsA obj = new clsA();
//base and derived class
obj.subjectName = "English";
string A1 = obj.mainsubject();
string A2 = obj.Allsubject();
}Loading
Prasad RaveendranPosted May 31, 2023, 2:52 PM
In C#, when you declare a new variable with the same name in a derived class, it effectively hides the base class variable. Therefore, you cannot directly access the base class variable using an instance of the derived class.
If you need to access the base class variable, you can create a property or a method in the base class to provide access to that variable.
sample project attached for your perusal. I created a console application in .NET 7.0. Please do let me know if there are any issues
Rajkiran SwainPosted May 31, 2023, 11:31 AM