How to initialize a derived class with an instance of the base class in c#?
Hello to all,
I am new to this forum. I am last year Computer Science student. In our last lecture we learn about how to initialize a derived class. But there is one question in mind. Can we initialize a derived class with an instance of the base class in c#? If yes, can anyone tell me how to initialize a derived class with an instance of the base class in c#? Please help me.
Thank you.
Re: How to initialize a derived class with an instance of the base class in c#?
I have written following code for your understanding. Just try to understand it. In the following code I have use MyListEg to implement all methods. In the following code I have use 3list to achieve result. I also have use one variable known as "lists" to store variable.
Code:
public class MyListEg<T> : List<T> {
public MyListEg(List<T> list) {
this = lists;
base = lists;
}
}
Re: How to initialize a derived class with an instance of the base class in c#?
From the three object oriented principle Inheritance is one of the most useful feature. This feature allow you to reuse existing code. Through this you can save your lot of time by re-using code. I have written following code for your understanding. Just try to understand it.
Code:
using System;
public class ParentClassEg
{
public ParentClassEg()
{
Console.WriteLine("you are in Parent Constructor.");
}
public void print()
{
Console.WriteLine("you are in Parent Class.");
}
}
public class ChildClassEg : ParentClassEg
{
public ChildClassEg()
{
Console.WriteLine("you are inChild Constructor.");
}
public static void Main()
{
ChildClassEg child = new ChildClassEg();
childs.print();
}
}
Re: How to initialize a derived class with an instance of the base class in c#?
You have to use following code in your program to initialize a derived class with an instance of the base class in c#. In this code I have use extension method on the List object.
public MyListEg(List<T> lists) : base(lists)
If you know how to use extension methods then use it on another class. just use this code in first line.
Re: How to initialize a derived class with an instance of the base class in c#?
If you use .Net framework 3.5 then it would be difficult for you to define an extension method on a list. In this case you have to use following code in your program to fix this problem. In the following code I have use MyListExtensionClassEg class to define all methods.
Code:
public static class MyListExtensionClassEg
{
public static void MyListEg<T>(this Lists<Ts> lists)
{
// Write your code
}
}
Re: How to initialize a derived class with an instance of the base class in c#?
You have to write two code in your program to initialize a derived class with an instance of the base class in c#. I have written following two code for you. Just try to understand it.
Code:
public MyListEg(List<Ts> lists):base(lists)
The above code will call the following constructor of the base class.
Code:
public List(IEnumerables<Ts> collections);
after writing above code, write following code in your program.
Code:
public class MyListEg<Ts> : List<Ts>
{
public MyLists(List<Ts> lists)
{
AddRanges(lists);
}
}