Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C# verify the C# Private Definition?

I use private and public methods all the time. However, I do not understand why they work. Creating a small Hello World Program:

public class CallPublicHelloWorld
{
    public void CallHelloWorld()
    {
        publicHelloWorld();
        privateHelloWorld();
    }

    private void privateHelloWorld()
    {
        Console.WriteLine("Hello World");
    }

    public void publicHelloWorld()
    {
        Console.WriteLine("Hello World");
    }
}

The IL created for the public method:

IL_0000:  nop
IL_0001:  ldstr      "Hello World"
IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
IL_000b:  nop
IL_000c:  ret

The IL created for the private method:

IL_0000:  nop
IL_0001:  ldstr      "Hello World"
IL_0006:  call       void [mscorlib]System.Console::WriteLine(string)
IL_000b:  nop
IL_000c:  ret

It's the exact same.

How does the JIT differentiate and verify that the private/public rules were followed?

like image 830
Dudemanword Avatar asked Feb 24 '14 16:02

Dudemanword


People also ask

How does C language work?

C is what's referred to as a compiled language, meaning you have to use a compiler to turn the code into an executable file before you can run it. The code is written into one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEdit on a Mac, and gedit in Linux.

What does %d do in C?

%d is a format specifier, used in C Language. Now a format specifier is indicated by a % (percentage symbol) before the letter describing it. In simple words, a format specifier tells us the type of data to store and print. Now, %d represents the signed decimal integer.

What is && operator in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int . Both operands must have an arithmetic or pointer type.

What does %c mean 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.


1 Answers

You omitted the relevant lines from the generated IL:

.method private hidebysig instance void privateHelloWorld () cil managed 

.method public hidebysig instance void publicHelloWorld () cil managed 

And that's all there is to it. See the accessibility section in this Common Type System MSDN page.

When mangling the IL to call the private method and compiling it with ilasm, at runtime you'll get:

Unhandled Exception: System.MethodAccessException: Attempt by method 'Program.Main(System.String[])' to access method 'CallPublicHelloWorld.privateHelloWorld()' failed.

at Program.Main(String[] args)

So there is an accessibility check performed by the runtime.

like image 196
CodeCaster Avatar answered Sep 28 '22 09:09

CodeCaster