Accessing a property of derived class from the base class in C#
Certainly you can downcast, like so:
for (int i = 0; i < MyList.Count; i++)
{
if (MyList[i] is ClassA)
{
var a = ((ClassA)MyList[i]).PropertyA;
// do stuff with a
}
if (MyList[i] is ClassB)
{
var b = ((ClassB)MyList[i]).PropertyB;
// do stuff with b
}
}
... However, you should take another look at what you're trying to accomplish. If you have common code that needs to get to properties of ClassA and ClassB, then you may be better off wrapping access to those properties up into a shared, virtual property or method in the ancestor class.
Something like:
public class BaseClass
{
public virtual void DoStuff() { }
}
public class ClassA : BaseClass
{
public object PropertyA { get; set; }
public override void DoStuff()
{
// do stuff with PropertyA
}
}
public class ClassB : BaseClass
{
public object PropertyB { get; set; }
public override void DoStuff()
{
// do stuff with PropertyB
}
}
how to access properties in derived class stored in a list of the base class
You will have to use explicit casting, the code would look like this,
// This is the Child Class which derives from class Base.
Child c = new Child();
//UpCast can be done implicitly without any issues
Base b = c;
//Explicit conversion is required to cast the object back to derived.
Child c1 = (Child) b;
Now , since this is runtime operation, you will not get an error in case of incorrect casting, hence always make a check before casting. you can use "is" or "as" keyword, Below are the links that will give you more details.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is
Hope this helps
Better way of passing properties of base class to derived class?
This is the proper implementation:
public class TheBase
{
public int One { get; set; }
public int Two { get; set; }
public int Three { get; set; }
public TheBase(int one, int two, int three)
{
One = one;
Two = two;
Three = three;
}
public TheBase(TheBase theBase)
{
One = theBase.One;
Two = theBase.Two;
Three = theBase.Three;
}
}
public class Derived : TheBase
{
public int Four { get; set; }
public int Five { get; set; }
public Derived(TheBase theBase, int four, int five) : base(theBase)
{
Four = four;
Five = five;
}
public Derived(int one, int two, int three, int four, int five) : base(one, two, three)
{
Four = four;
Five = five;
}
}
How to use a derived property on a base class?
Hiding a member with the new
keyword should generally be avoided. Instead make the base class' property virtual
and override it in the descending class. The MyBaseMethod
will automatically use this overridden property in inheriting classes.
public class MyBase
{
public virtual string MyProperty { get { return "Base"; } }
public string MyBaseMethod()
{
return MyProperty;
}
}
public class MyInherited : MyBase
{
public override string MyProperty { get { return "Inherited"; } }
}
var inherited = new MyInherited();
Console.WriteLine(inherited.MyBaseMethod()); // ==> "Inherited"
See this interesting post related to the new
keyword: Why do we need the new keyword and why is the default behavior to hide and not override?
Get properties from derived class in base class
If both classes are in the same assembly, you can try this:
Assembly
.GetAssembly(typeof(BaseClass))
.GetTypes()
.Where(t => t.IsSubclassOf(typeof(BaseClass))
.SelectMany(t => t.GetProperties());
This will give you all the properties of all the subclasses of BaseClass
.
C# Use base class property value in a derived class property override
Use base.AnimalId
public class Animal
{
private int _animalId;
public virtual int AnimalId
{
get { return _animalId; }
}
}
public class Dog : Animal
{
public override int AnimalId
{
get
{
if (Request.Params["New_Animal"] == "true")
return -1;
else
return base.AnimalId;
}
}
}
Related Topics
Access Visual Studio 2017's Private Registry Hive
How to Have an Optional Parameter for an ASP.NET Soap Web Service
Convert String Value to Operator in C#
Using Sse in C# Is It Possible
How to Use Extension Methods and Linq in .Net 2.0 or 3.0
How to Convert Datetime in Specific Timezone
C# Parameterized Queries for Oracle - Serious & Dangerous Bug!
Entity Framework Creates a Plural Table Name, But the View Expects a Singular Table Name
How to Sort a List<T> by Multiple T.Attributes
Nhibernate How to Query Against an Ilist<String> Property
Text Property in a Usercontrol in C#
How to Change Pixel Color of an Image in C#.Net
Applying % Number Format to a Cell Value Using Openxml
Set Item Focus in Listview Wpf
Efficient Way to Round Double Precision Numbers to a Lower Precision Given in Number of Bits
Why Would Try/Finally Rather Than a "Using" Statement Help Avoid a Race Condition