Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can Main be a private method

How can Main be a private method

Scheduled Pinned Locked Moved C#
dotnet
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tony_Joh
    wrote on last edited by
    #1

    Hello! I just wonder how can this small code fragment work when I have Main method as private. How can the runtime part(CLR) access main method when it is private. public class myApp { private static void Main() { System.Console.WriteLine("Main is now private "); } } //Tony

    L R 2 Replies Last reply
    0
    • T Tony_Joh

      Hello! I just wonder how can this small code fragment work when I have Main method as private. How can the runtime part(CLR) access main method when it is private. public class myApp { private static void Main() { System.Console.WriteLine("Main is now private "); } } //Tony

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can also use Reflection to call private method in a class.

      1 Reply Last reply
      0
      • T Tony_Joh

        Hello! I just wonder how can this small code fragment work when I have Main method as private. How can the runtime part(CLR) access main method when it is private. public class myApp { private static void Main() { System.Console.WriteLine("Main is now private "); } } //Tony

        R Offline
        R Offline
        Robert Rohde
        wrote on last edited by
        #3

        stancrm is totally right. With Reflection you can call private members and that is probably what the .NEt runtime does. Visibility modifiers are mostly relevant for the compiler. With Reflection you can access everything no matter what visibility it has. This not only includes calling methods, but also accessing and changing fields which are hidden privately within external classes.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups