7

I am new to C#, I am learning it and it is just a dummy test program. I am getting the error that is mentioned in the title of this post. Below is the C# code.

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

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

        class Program
        {
            public int propertyInt
            {
                get { return 1; }
                set { Console.WriteLine(value); }
            }
        }
    }
}
Abbas
  • 14,186
  • 6
  • 41
  • 72
Muhammad Sohail
  • 191
  • 2
  • 2
  • 6

2 Answers2

7

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!

Dr Rob Lang
  • 6,659
  • 5
  • 40
  • 60
  • 2
    The compiler could have been made to be able to understand that since the classes are actually `Program` and `Program.Program` The fundamental problem is that C# spec defines that you can't do that. In VB you could create a nested class with the same name like this. I imagine the design choice was made to avoid ambiguity and confusion with constructor syntax – Chris Feb 07 '14 at 14:48
  • 1
    @RobLang i thicked it as correct. Thanks for informing me about this feature of SOF. I wasn't aware of this, I am new here as well. :-) – Muhammad Sohail Feb 07 '14 at 14:51
2

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); }
            }
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Mehdi Elhaij
  • 46
  • 2
  • 7