Complete the code to make the field accessible from anywhere.
class MyClass { [1] int number; }
The public modifier makes the member accessible from any other code.
Complete the code to restrict access to the method only within the same assembly.
class Calculator { [1] int Add(int a, int b) { return a + b; } }
The internal modifier restricts access to the same assembly.
Complete the code to make the field accessible only inside the class.
class Person { [1] string name; public Person(string name) { this.name = name; } }
The private modifier restricts access to inside the class only, which is correct for encapsulation.
Fill in the blank to declare a method accessible only within the class and its derived classes, but not outside.
using System; class Animal { [1] void Speak() { Console.WriteLine("Animal sound"); } } class Dog : Animal { public void Bark() { Speak(); } }
The protected modifier allows access inside the class and its subclasses, but not outside.
Fill all three blanks to create a class with a private field, an internal method, and a public method.
class BankAccount { [1] decimal balance; [2] void UpdateBalance(decimal amount) { balance += amount; } [3] decimal GetBalance() { return balance; } }
The field is private to hide it, the update method is internal to allow access within the assembly, and the getter is public to allow access from anywhere.