Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:46:43+00:00 2026-06-10T02:46:43+00:00

I have written a small code, but found something amazing. I have a class

  • 0

I have written a small code, but found something amazing. I have a class name Students and inside that calss declared a List<> like this with variable name Students (same as the Class name)

  Class Students
{
    private String Name;
    private int Age;   
    public Students(){}

    List<Students> Students = new List<Students>();

...
}

Here compile time error is

‘Students’: member names can not be the same as their enclosing type

But if I declare the same List in other class … like

        Class Students
        {
           private String Name;
           private int Age;   
           public Students(){}    
           ...
        }


        Class Program
        {
         ....
            List<Students> Students = new List<Students>();
            Students.Add(new Students("Deb","B++"));
            Students.Add(new Students("DDD", "A++"));
          ............
        }

This Works fine.
My Question is, why? How we can create custom variable as the class name in other calsses but not in the same class?
Any elaborate answer would be good, as I want to gain knowledge in this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:46:44+00:00Added an answer on June 10, 2026 at 2:46 am

    The answer is mentioned in the error message:

    ‘Students’: member names can not be the same as their enclosing type

    It’s only an error if you are inside the class with the same name as the member.

    Anyway, it’s a bad idea in any case to name variables like this. They should be not be capitalized.

    Edit: Here’s an example that might cause ambiguity:

    class A
    {
        public void Foo() { }
    }
    
    class B
    {
        private A B = new A();
    
        static void Foo() { }
    
        void Bar()
        {
            B.Foo(); // what am I calling here?
        }
    }
    

    Edit 2: Searching around a bit I’ve found a very similar question: Why C# member names can't be the same as the enclosing type name? and the consensus was just that it’s a limitation of C# (no clear explanation why). VB.NET seems to allow that this situation to occur.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just written a small piece of code and it struck me that
I have written a small piece of code to calculate quadratic equations, but if
I have written a small piece of code that would perform Run length encoding
I have written a small code for usb detection for android device but its
I have written a small plugin that displays tweets. following is the code that
What I have written a small piece of code to find and remove if
I have written a small chat app that uses mysql + php to facilitate
I have written a small app that puts my bluetooth in discoverable mode for
I have written a small plugin to change the text size but it doesn't
I have written a small makefile for a few simple C programs that compiles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.