#708 – A Name Must Be Unique Within A Declaration Space

When you declare an element in C#, your declaration exists within a particular declaration space.  The declaration space is the context within which you can’t have two elements declared with the same name.

One example of a declaration space is a class–within a class, you cannot have more than one member with the same name, but you can have a member with the same name in a different class.  In the example below, we define a Name property in one class and a Name method in another class.

    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        // NO: We already have a "Name" member in this declaration space
        //public void Name()
        //{
        //}
    }

    public class Baby
    {
        public string BabyName { get; set; }

        public void Name(string newName)
        {
            BabyName = string.Format("Cute little {0}", newName);
        }
    }
Advertisement