Error: member names cannot be the same as their enclosing type

When you do this:

Program prog = new Program();

The C# compiler cannot tell if you want to use the Program here:

namespace DriveInfos
{
    class Program  // This one?
    {
        static void Main(string[] args)
        {

Or if you mean to use the other definition of Program:

    class Program
    {
        public int propertyInt
        {
            get { return 1; }
            set { Console.WriteLine(value); }
        }
    }

The best thing to do here is to change the name of the internal class, which will give you:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DriveInfos
{
    class Program
    {
        static void Main(string[] args)
        {
            MyProgramContext prog = new MyProgramContext();
            prog.propertyInt = 5;
            Console.WriteLine(prog.propertyInt);
            Console.Read();
        }

        class MyProgramContext
        {
            public int propertyInt
            {
                get { return 1; }
                set { Console.WriteLine(value); }
            }
        }
    }
}

So now there is no confusion - not for the compiler, nor for you when you come back in 6 months and try to work out what it is doing!


You have two classes with the same name Program. Rename one of them.

namespace DriveInfos
{
    class Program
    {
        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.propertyInt = 5;
            Console.WriteLine(prog.propertyInt);
            Console.Read();
        }

        class Program1
        {
            public int propertyInt
            {
                get { return 1; }
                set { Console.WriteLine(value); }
            }
        }
    }
}