0
0
C Sharp (C#)programming~10 mins

Passing reference types to methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pass the object to the method.

C Sharp (C#)
class Program {
    static void Main() {
        Person p = new Person();
        UpdateName([1]);
    }

    static void UpdateName(Person person) {
        person.Name = "Alice";
    }
}

class Person {
    public string Name { get; set; }
}
Drag options to blanks, or click blank then click option'
Anew Person()
BPerson
Cp
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of the object variable.
Passing null instead of the object.
2fill in blank
medium

Complete the method call to change the age of the person.

C Sharp (C#)
class Program {
    static void Main() {
        Person p = new Person();
        SetAge([1], 30);
    }

    static void SetAge(Person person, int age) {
        person.Age = age;
    }
}

class Person {
    public int Age { get; set; }
}
Drag options to blanks, or click blank then click option'
Anew Person()
Bp
CPerson
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a new object instead of the existing one.
Passing the class name instead of an object.
3fill in blank
hard

Fix the error in the method to correctly update the person's city.

C Sharp (C#)
class Program {
    static void Main() {
        Person p = new Person();
        ChangeCity(p);
        System.Console.WriteLine(p.City);
    }

    static void ChangeCity(Person person) {
        person.City = [1];
    }
}

class Person {
    public string City { get; set; }
}
Drag options to blanks, or click blank then click option'
Anull
BNew York
Cperson.City
D"New York"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the string.
Using an undefined variable.
4fill in blank
hard

Fill both blanks to create a method that updates the person's name and age.

C Sharp (C#)
class Program {
    static void Main() {
        Person p = new Person();
        UpdatePerson(p, [1]);
        System.Console.WriteLine($"Name: {p.Name}, Age: {p.Age}");
    }

    static void UpdatePerson(Person person, int newAge) {
        person.Name = [2];
        person.Age = newAge;
    }
}

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}
Drag options to blanks, or click blank then click option'
A"Bob"
B25
C"Alice"
D30
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the wrong type for age.
Not using quotes for the name string.
5fill in blank
hard

Fill all three blanks to create a method that updates name, age, and city of a person.

C Sharp (C#)
class Program {
    static void Main() {
        Person p = new Person();
        UpdateDetails(p, [1], [2], [3]);
        System.Console.WriteLine($"Name: {p.Name}, Age: {p.Age}, City: {p.City}");
    }

    static void UpdateDetails(Person person, string newName, int newAge, string newCity) {
        person.Name = newName;
        person.Age = newAge;
        person.City = newCity;
    }
}

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public string City { get; set; }
}
Drag options to blanks, or click blank then click option'
A"Charlie"
B40
C"Seattle"
D"Denver"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of arguments.
Forgetting quotes around string arguments.