Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Practice: Objects | Introduction to Object-Oriented Programming (OOP)
C# Beyond Basics

Practice: ObjectsPractice: Objects

The following program contains two classes, namely Person and Address.

Read the code and fill in the blanks appropriately. In this task you will have to figure out how to access the Country field of the address object in the address field of the p1 object.

cs

index.cs

1. If a field of an object stores another object, we can further use the dot (.) notation for accessing deeper into that object. For-example objectName.someField.anotherField.

using System;

class Person {
    public string name;
    public int age;
    
    // We can use other classes as datatypes for a field in a class
    public Address address;
    
    // We can use the class itself as a datatype for a field as well
    public Person father;
    public Person mother;
}

class Address {
    public string Country;
    public string City;
}

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        Person p1 = new Person();
        p1.name = "Mihaly";
        p1.age = 21;
        
        Person p2 = new Person();
        p2.name = "Ann";
        p2.age = 52;
        
        Person p3 = new Person();
        p3.name = "Nagy";
        p3.age = 51;
        
        p1.mother = p2;
        p1.father = p3;
        
        Address address = new Address();
        address.Country = "Hungary";
        address.City = "Budapest";
        
        p1.address = address;
        
        // Note the expression 'p1.address.Country'
        // p1.address accesses the stored Address object
        // p1.address.Country accesses the Address object's Country field.
        Console.WriteLine($"{p1.name} is the son of {p2.name} and {p3.name}. He is from {p1.address.Country}.");
    }
}      
      

Все було зрозуміло?

Секція 3. Розділ 6
course content

Зміст курсу

C# Beyond Basics

Practice: ObjectsPractice: Objects

The following program contains two classes, namely Person and Address.

Read the code and fill in the blanks appropriately. In this task you will have to figure out how to access the Country field of the address object in the address field of the p1 object.

cs

index.cs

1. If a field of an object stores another object, we can further use the dot (.) notation for accessing deeper into that object. For-example objectName.someField.anotherField.

using System;

class Person {
    public string name;
    public int age;
    
    // We can use other classes as datatypes for a field in a class
    public Address address;
    
    // We can use the class itself as a datatype for a field as well
    public Person father;
    public Person mother;
}

class Address {
    public string Country;
    public string City;
}

public class ConsoleApp
{
    public static void Main(string[] args)
    {
        Person p1 = new Person();
        p1.name = "Mihaly";
        p1.age = 21;
        
        Person p2 = new Person();
        p2.name = "Ann";
        p2.age = 52;
        
        Person p3 = new Person();
        p3.name = "Nagy";
        p3.age = 51;
        
        p1.mother = p2;
        p1.father = p3;
        
        Address address = new Address();
        address.Country = "Hungary";
        address.City = "Budapest";
        
        p1.address = address;
        
        // Note the expression 'p1.address.Country'
        // p1.address accesses the stored Address object
        // p1.address.Country accesses the Address object's Country field.
        Console.WriteLine($"{p1.name} is the son of {p2.name} and {p3.name}. He is from {p1.address.Country}.");
    }
}      
      

Все було зрозуміло?

Секція 3. Розділ 6
some-alt