Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got a book about C# programming then copied and pasted some code as an example.
How ever the error "Inconsistent accessibility: base class is less accessible than child class" pop out.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2_52
{
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rectangle = new Square(12);
            rectangle.Width = 10;
            rectangle.Height = 5;
            Console.WriteLine(rectangle.Area);
        }
    }
    class Rectangle
    {
        public Rectangle(int width,int height)
        {
            Width = width;
            Height = height;
        }
        public virtual int Height { get; set; }
        public virtual int Width { get; set; }
        public int Area
        {
            get { return Height * Width; }
        }
    }
    private class Square : Rectangle
    {
        public Square(int size):base(size,size)
        {

        }
        public override int Width
        {
            get
            {
                    return base.Width;
            }
            set
            {
                base.Width = value; base.Width = value;
            }
        }
    }
}

Thanks for correcting it.
Posted
Comments
BillWoodruff 8-Jan-15 19:44pm    
Note: In Visual Studio 2013 the compile error you will get is "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal"

1 solution

C#
private class Square : Rectangle
 
Share this answer
 
Comments
[no name] 8-Jan-15 15:59pm    
It works out. Is it default "private" or "public" if without the access modifier?
Richard Deeming 8-Jan-15 16:08pm    
You can't have a private class unless it's nested inside another class.

For a top-level class (directly inside a namespace), if you don't specify an access modifier, the default will be internal.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900