I
I
IvanS19892020-10-30 04:36:52
OOP
IvanS1989, 2020-10-30 04:36:52

How to create a property in another C# class based on a class collection?

5f9b7e18c75e0216343379.png

5f9b803f91889162514861.png

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

1 answer(s)
V
Vladislav, 2020-10-30
@Jewish_Cat

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 question

Ask a Question

731 491 924 answers to any question