1

Can a class have 2 objects with the same name? If yes then while displaying, which particular object's roll, name and course details get printed?

class Program
{
    static void Main(string[] args)
    {
        Student st=null;

        Console.WriteLine("Enter Records");
        for (int i = 0; i < 3; i++)
        {
            st = new Student();
            Console.WriteLine("Enter roll");
            st.roll = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter name");
            st.name = Console.ReadLine();
            Console.WriteLine("Enter course");
            st.course = Console.ReadLine();
        }

        Console.WriteLine("Show Records");
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("Roll "+st.roll.ToString());
            Console.WriteLine("Name "+st.name);
            Console.WriteLine("Course "+st.course);
        }

        Console.ReadKey();
    }
}

class Student
{
    public int roll;
    public string name;
    public string course;
}
6
  • So, each Student needs multiple courses, or something different? Commented Mar 31, 2014 at 2:58
  • 1
    You only have one Student object in st, that keeps getting overwritten. You need to use a collection or array of Student. Commented Mar 31, 2014 at 2:59
  • @Grant Winney, it is working just fine, I m getting the right input and output but i m not confident about my understanding of the concept of objects for I have asked the query above. Commented Mar 31, 2014 at 3:08
  • @Grant Winney, try it for yourself. Commented Mar 31, 2014 at 3:18
  • @OldProgrammer How is it that I am still getting the right input and output when the object should practically be overwritten?? Commented Mar 31, 2014 at 3:28

1 Answer 1

2

No, but you can create a List<Students> which is basically a list of Students

using System.Collections.Generic;
class Program
{
    static void Main(string[] args)
    {
        List<Student> sts = new List<Student>();
        Student st = null;
        Console.WriteLine("Enter Records");
        for (int i = 0; i < 3; i++)
        {
            st = new Student();
            Console.WriteLine("Enter roll");
            st.roll = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter name");
            st.name = Console.ReadLine();
            Console.WriteLine("Enter course");
            st.course = Console.ReadLine();
            sts.Add(st);  // this is the line to be added to populate the list
        }

        Console.WriteLine("Show Records");
        for (int i = 0; i < sts.Count; i++)
        {
            st = sts[i];  // this needs to be added to read from list
            Console.WriteLine("Roll "+st.roll.ToString());
            Console.WriteLine("Name "+st.name);
            Console.WriteLine("Course "+st.course);
        }

        Console.ReadKey();
    }
}

class Student 
{
   public int roll;
   public string name;
   public string course;
}
Sign up to request clarification or add additional context in comments.

13 Comments

My question is, how can i make multiple objects of class student with the same name, st=new student();
If you do that, you are overriding the object
Which is what I am asking, you are making an object in a loop construct, therefore, each time the loop goes to work an object with the same name gets made. st=new student();
@androidrill: no, a variable gets assigned a new value. That's not the same thing as an object with the same name.
@jmoreno How is it possible? "new" keyword means a new object is being formed within the loop construct, everytime loop goes to work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.