Bird
0
0

What is the best way to achieve this?

hard🚀 Application Q15 of 15
C Sharp (C#) - Classes and Objects

You have a class library project with a class Helper that has a method Calculate() marked as internal. You want to allow another project in the same solution to use Calculate() without making it public. What is the best way to achieve this?

AUse the <code>InternalsVisibleTo</code> attribute to expose internal members to the other project
BChange the method to public
CChange the method to private and create a public wrapper
DMove the method to the other project
Step-by-Step Solution
Solution:
  1. Step 1: Understand internal and project boundaries

    internal restricts access to the same assembly, so other projects cannot access it by default.
  2. Step 2: Use InternalsVisibleTo attribute

    This attribute allows you to specify friend assemblies that can access internal members without making them public.
  3. Step 3: Evaluate other options

    Changing to public exposes to all, private hides too much, moving method is impractical.
  4. Final Answer:

    Use the InternalsVisibleTo attribute to expose internal members to the other project -> Option A
  5. Quick Check:

    InternalsVisibleTo grants internal access to specific projects [OK]
Quick Trick: Use InternalsVisibleTo to share internal members across projects [OK]
Common Mistakes:
MISTAKES
  • Making method public unnecessarily
  • Thinking private allows cross-project access
  • Moving code instead of using attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

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