menu

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

by
1 vote
5f9b7e18c75e0216343379.png


5f9b803f91889162514861.png

How to create a property in the SecondClass class that should be a collection of firstClass created in the Program class where we added its objects - coll1. The first two classes are in the picture. The third one, where the property should be here below in the text. There should be just one line.

namespace NewProject
{
public class SecondClass
{

}
}

5 Comments

Vasily Bannikov , Can you write how the code will look like. I don't understand it yet, there should be one line of code. more specifically the task sounds like this - create a property of class 2, which will be a collection of the first class where we loaded its objects - coll1.
IvanS1989 , it's written in the rules that you have to use the tag <code lang="cs"></code>
IvanS1989 , use the tag code
Developer , I added pictures. The text is not indented for some reason.
Format the code. it's impossible to read

1 Answer

by
0 votes
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>();
}
}