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. default accessibility

default accessibility

Scheduled Pinned Locked Moved C#
questioncsharpcsshelptutorial
8 Posts 5 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.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:

    Program.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestConsole
    {

    // public class MainConsole
    // program is built successfully when using this line instead of the bottom one

    class MainConsole // default scope for this class??
    {
        public const int myAge = 27;
    
        public int returnZero()
        {
            return 0;
            //return (number1 + number2);
        }
        static void Main(string\[\] args)
        {
            Console.WriteLine("Start program");
        }
    }
    

    myClass.cs

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace TestConsole
    {
    class myClass : TestConsole.MainConsole
    {
    private int Age;
    public myClass()
    {
    //initialize Age to zero (long-winded)
    Age = base.returnZero();
    }
    }
    }

    Jon

    A S A 3 Replies Last reply
    0
    • J jon 80

      With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:

      Program.cs

      using System;
      using System.Collections.Generic;
      using System.Text;

      namespace TestConsole
      {

      // public class MainConsole
      // program is built successfully when using this line instead of the bottom one

      class MainConsole // default scope for this class??
      {
          public const int myAge = 27;
      
          public int returnZero()
          {
              return 0;
              //return (number1 + number2);
          }
          static void Main(string\[\] args)
          {
              Console.WriteLine("Start program");
          }
      }
      

      myClass.cs

      using System;
      using System.Collections.Generic;
      using System.Text;

      namespace TestConsole
      {
      class myClass : TestConsole.MainConsole
      {
      private int Age;
      public myClass()
      {
      //initialize Age to zero (long-winded)
      Age = base.returnZero();
      }
      }
      }

      Jon

      A Offline
      A Offline
      Ahmad Adnan
      wrote on last edited by
      #2

      coz the Child Class is Default (Private) u change Change class myClass : TestConsole.MainConsole into Public class myClass : TestConsole.MainConsole

      1 Reply Last reply
      0
      • J jon 80

        With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:

        Program.cs

        using System;
        using System.Collections.Generic;
        using System.Text;

        namespace TestConsole
        {

        // public class MainConsole
        // program is built successfully when using this line instead of the bottom one

        class MainConsole // default scope for this class??
        {
            public const int myAge = 27;
        
            public int returnZero()
            {
                return 0;
                //return (number1 + number2);
            }
            static void Main(string\[\] args)
            {
                Console.WriteLine("Start program");
            }
        }
        

        myClass.cs

        using System;
        using System.Collections.Generic;
        using System.Text;

        namespace TestConsole
        {
        class myClass : TestConsole.MainConsole
        {
        private int Age;
        public myClass()
        {
        //initialize Age to zero (long-winded)
        Age = base.returnZero();
        }
        }
        }

        Jon

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining MainConsole with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. In MainConsole you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to derive MyClass from it, MyClass has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]

        Scott.


        —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

        J S 2 Replies Last reply
        0
        • J jon 80

          With .NET 2.0, it's noted that accessibility of classes need not be defined, since classes can be inherited within the same assembly. When the superclass is given accessibility as public, the program runs successfully. However setting the inherited class as public, error messages are displayed: public class myClass : TestConsole.MainConsole Error: Inconsistent accessibility: base class 'TestConsole.MainConsole' is less accessible than class 'TestConsole.myClass' What is the default accessibility level when it is not explicitly defined? For example the following code is successfully built:

          Program.cs

          using System;
          using System.Collections.Generic;
          using System.Text;

          namespace TestConsole
          {

          // public class MainConsole
          // program is built successfully when using this line instead of the bottom one

          class MainConsole // default scope for this class??
          {
              public const int myAge = 27;
          
              public int returnZero()
              {
                  return 0;
                  //return (number1 + number2);
              }
              static void Main(string\[\] args)
              {
                  Console.WriteLine("Start program");
              }
          }
          

          myClass.cs

          using System;
          using System.Collections.Generic;
          using System.Text;

          namespace TestConsole
          {
          class myClass : TestConsole.MainConsole
          {
          private int Age;
          public myClass()
          {
          //initialize Age to zero (long-winded)
          Age = base.returnZero();
          }
          }
          }

          Jon

          A Offline
          A Offline
          Arjan Einbu
          wrote on last edited by
          #4

          Default accessibility is private for everything but namespace elements. That means classes, structs, delegates or enums that are not defined within another class are by default internal. In all other cases accessibility defaults to private. In your case, the myClass and MainConsole classes are internal.

          J 1 Reply Last reply
          0
          • S Scott Dorman

            The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining MainConsole with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. In MainConsole you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to derive MyClass from it, MyClass has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]

            Scott.


            —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

            J Offline
            J Offline
            jon 80
            wrote on last edited by
            #5

            Thanks

            Jon

            1 Reply Last reply
            0
            • A Arjan Einbu

              Default accessibility is private for everything but namespace elements. That means classes, structs, delegates or enums that are not defined within another class are by default internal. In all other cases accessibility defaults to private. In your case, the myClass and MainConsole classes are internal.

              J Offline
              J Offline
              jon 80
              wrote on last edited by
              #6

              Thanks

              Jon

              1 Reply Last reply
              0
              • S Scott Dorman

                The default accessibility is always private. This applies to classes, methods, properties, and fields. In your code sample, defining MainConsole with out specifying the accessibility makes it private. However, the problem you are running into isn't the accessibility on the class since in this case, they are both private. In MainConsole you do not have a default constructor, so the compiler is generating one for you. The compiler generated constructor is always private. When you then try to derive MyClass from it, MyClass has a public constructor. What the compiler sees is that you have a class with a public constructor inheriting from one with a private constructor and says that there is no way for the derived class to create an instance of the base class. (If something is private, even the derived classes don't have visibility to it.) [modification 13:28 Tuesday 4th September, 2007] I was wrong on this response, not sure what I was thinking. See this response: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&select=2209501&mpp=50&fr=354&df=100#xx2209501xx[^] [/modification]

                Scott.


                —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                S Offline
                S Offline
                Steve Hansen
                wrote on last edited by
                #7

                Hmmm, default accessibility for classes and structs is internal, nested classes and structs are private. If you create no constructors for a class it will have a default public constructor. The error he gets is because MainConsole is internal and he creates a public class that inherits from it.

                S 1 Reply Last reply
                0
                • S Steve Hansen

                  Hmmm, default accessibility for classes and structs is internal, nested classes and structs are private. If you create no constructors for a class it will have a default public constructor. The error he gets is because MainConsole is internal and he creates a public class that inherits from it.

                  S Offline
                  S Offline
                  Scott Dorman
                  wrote on last edited by
                  #8

                  You're right...I'm not sure what I was thinking when I wrote that response.

                  Scott.


                  —In just two days, tomorrow will be yesterday. [Forum Guidelines] [Articles] [Blog]

                  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