Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# allows methods/members to be public when the class is internal

Tags:

c#

oop

I have a class which I marked as internal and I marked fields and methods as public. It compiled without errors or warnings. Is there any specific need to have methods as public and class as internal (except when they are being implemented from interfaces or classes)?

like image 711
Ravisha Avatar asked Feb 15 '10 08:02

Ravisha


People also ask

Why is C used?

The C programming language is the recommended language for creating embedded system drivers and applications. The availability of machine-level hardware APIs, as well as the presence of C compilers, dynamic memory allocation, and deterministic resource consumption, make this language the most popular.

Why is C used in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Why should you learn C?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why C is still used today?

C exists everywhere in the modern world. A lot of applications, including Microsoft Windows, run on C. Even Python, one of the most popular languages, was built on C. Modern applications add new features implemented using high-level languages, but a lot of their existing functionalities use C.


1 Answers

It has no adverse effects, and also means that if you ever do decide to make the type public, you won't need to change the accessibility of your members.

Basically for a member:

  • public means the member is visible to anyone who can see the Type.

  • internal means the member is only visible in the current assembly, even if the Type is publicly visible.

So your choice would be based on which of these is most appropriate. In general it's most appropriate to make the members public (i.e. visible to anyone who can see the Type, i.e. part of the Type's public API). You would make members internal for the same reason you make members internal in a public class - typically helper members that should only be visible to "friend" classes in the same assembly, and don't form part of the public API.

In addition, an internal Type can derive from a public Type, so can inherit and override public members. Would it make sense to allow overridden public members, but not new public members?

like image 66
Joe Avatar answered Nov 16 '22 02:11

Joe