BasicKnowledgeCSharp/LessonsAndTasks/Lesson 42_2 - Example 1 - Оператор условного NULL/Program.cs

36 lines
698 B
C#
Raw Permalink Normal View History

2024-10-05 09:59:53 +03:00
using System;
/*
* Оператор условного null
*/
class Program
{
private static Person GetPerson()
{
Person person = null;
return person;
}
static void Main()
{
Person person = null;
Console.WriteLine(person?.Contacts?.PhoneNumber ?? "no data");
person = GetPerson();
// person, Contacts и PhoneNumber - ссылочные типы - так как они class
Console.WriteLine(person?.Contacts?.PhoneNumber ?? "no data");
Console.ReadKey();
}
}
public class Person
{
public Contacts Contacts { get; set; }
}
public class Contacts
{
public string PhoneNumber { get; set; }
}