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

This keyword behavior 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 print the current object's name using this keyword.

C Sharp (C#)
public class Person {
    public string Name;
    public void PrintName() {
        Console.WriteLine(this.[1]);
    }
}
Drag options to blanks, or click blank then click option'
Athis
BName
Cname
DPrintName
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'name' instead of 'Name' which is case-sensitive.
Trying to use 'this' alone without a member name.
2fill in blank
medium

Complete the constructor to assign the parameter to the instance variable using this keyword.

C Sharp (C#)
public class Car {
    public string Model;
    public Car(string model) {
        this.[1] = model;
    }
}
Drag options to blanks, or click blank then click option'
Amodel
BCar
Cthis
DModel
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the parameter instead of the instance variable.
Using lowercase 'model' on the left side which refers to the parameter.
3fill in blank
hard

Fix the error by completing the method to return the current object's description using this keyword.

C Sharp (C#)
public class Book {
    public string Title;
    public string GetDescription() {
        return this.[1];
    }
}
Drag options to blanks, or click blank then click option'
ATitle
Btitle
CGetDescription
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'title' which does not match the field name.
Returning this alone which is not a string.
4fill in blank
hard

Fill both blanks to create a method that compares the current object's age with another object's age using this keyword.

C Sharp (C#)
public class Animal {
    public int Age;
    public bool IsOlderThan(Animal other) {
        return this.[1] [2] other.Age;
    }
}
Drag options to blanks, or click blank then click option'
AAge
B>
C<
Dother
Attempts:
3 left
💡 Hint
Common Mistakes
Using other on the left side instead of this.
Using the wrong comparison operator.
5fill in blank
hard

Fill all three blanks to create a method that updates the current object's score only if the new score is higher, using this keyword.

C Sharp (C#)
public class Player {
    public int Score;
    public void UpdateScore(int newScore) {
        if (newScore [1] this.[2]) {
            this.[3] = newScore;
        }
    }
}
Drag options to blanks, or click blank then click option'
A<
BScore
C>
DnewScore
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator like '<' instead of '>'.
Assigning to newScore instead of this.Score.