Answer the question
In order to leave comments, you need to log in
How to create a property in another C# class based on a class collection?
How to create a property in the SecondClass class, which should be a collection of firstClass created in the Program class where we added its objects - coll1. The first two classes in the figure. The third one is where the property should be here below here below in the text. There should be only one line.
namespace NewProject
{
public class SecondClass
{
}
}
Answer the question
In order to leave comments, you need to log in
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var firstObject = new FirstClass();
var secondObject = new SecondClass();
var thirdObject = new ThirdClass();
var list = new List<IParent>();
list.Add(firstObject);
list.Add(secondObject);
thirdObject.Collection = list;
}
}
/// <summary>
/// Родительский интерфейс
/// </summary>
public interface IParent
{
}
/// <summary>
/// Класс наследуемый от родительского интерфейса
/// </summary>
public class FirstClass : IParent
{
}
/// <summary>
/// Класс наследуемый от родительского интерфейса
/// </summary>
public class SecondClass : IParent
{
}
public class ThirdClass
{
public List<IParent> Collection { get; set; } = new List<IParent>();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question