C#.NET — Default accessibility of class members and intra-class access rules. Which of the following statements are correct? Data members of a class are by default public. Data members of a class are by default private. Member functions (methods) of a class are by default public. A private function of a class can access a public function within the same class. Member functions of a class are by default private.

Difficulty: Easy

Correct Answer: 2, 4, 5

Explanation:


Introduction / Context:
This question checks your understanding of C#.NET's default accessibility for class members (fields and methods) and how access modifiers behave within the same class. Many beginners assume methods default to public, but in C# the default for class members is private unless specified.



Given Data / Assumptions:

  • We are considering class members declared without any explicit access modifier.
  • We are dealing with top-level classes in C# (not nested types with different rules).


Concept / Approach:
In C#, members of a class (fields, methods, properties) default to private. Access modifiers govern visibility outside the class, but inside the same class, all members can call each other regardless of each one’s modifier. Thus a private method can invoke a public method of the same class (and vice versa).



Step-by-Step Solution:

Evaluate (1): False. Class fields are not public by default; they are private.Evaluate (2): True. Data members default to private.Evaluate (3): False. Methods are also private by default.Evaluate (4): True. Within a class, any member can access any other member because access checks are about outside visibility.Evaluate (5): True. Member functions default to private if no modifier is given.


Verification / Alternative check:
Create a class with a field int x; and method void M() { } without modifiers and inspect with IntelliSense or compile-time visibility—both are private.



Why Other Options Are Wrong:
Any option including (1) or (3) is incorrect because they claim a default of public, which is not how C# works.



Common Pitfalls:
Transferring assumptions from languages where defaults differ; forgetting that properties and methods also default to private in classes.



Final Answer:
2, 4, 5

More Questions from Classes and Objects

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion