Bird
Raised Fist0

What is wrong with this class declaration?

medium📝 Debug Q7 of Q15
C Sharp (C#) - Classes and Objects
What is wrong with this class declaration?
class Employee {
  public string Name;
  public void Employee() {
    Name = "John";
  }
}
AConstructor has a return type 'void'
BClass name is invalid
CField 'Name' must be private
DMethod 'Employee' should be static
Step-by-Step Solution
Solution:
  1. Step 1: Identify constructor syntax

    Constructors should not have a return type, not even void.
  2. Step 2: Check method declaration

    The method public void Employee() looks like a constructor but has return type void, so it's a method, not a constructor.
  3. Final Answer:

    Constructor has a return type 'void' -> Option A
  4. Quick Check:

    Constructors have no return type [OK]
Quick Trick: Constructors never specify a return type [OK]
Common Mistakes:
MISTAKES
  • Adding void to constructor
  • Confusing method with constructor
  • Thinking fields must be private

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes