0

Как правильно присвоить значение свойству класса, а затем вывести его?

namespace ConsoleApp1
{
public class Model1
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }
    public string Property4 { get; set; }
    public Model2 Model2 { get; set; }
}

public class Model2 { public string Property5 { get; set; } public string Property6 { get; set; } public string Property7 { get; set; } public string Property8 { get; set; } } class Program { static void Main(string[] args) { var Model1 = new Model1(); Model1.Model2.Property5 = "123"; //Model1.Model2.Property5 = "123"; Console.WriteLine(Model1.Model2.Property5); Console.ReadKey(); } }

dx9
  • 123
  • 9
  • 1
    Связанный вопрос: https://ru.stackoverflow.com/q/413041/213987 – A K Apr 22 '21 at 04:58

1 Answers1

3
    var Model1 = new Model1();
    Model1.Model2 = new Model2();
    Model1.Model2.Property5 = "123";

    var Model1 = new Model1() {
      Model2 = new Model2() {
        Property5 = "123"
      }
    };