0

I am new to c# and just practicing with object arrays. tried to display the elements of the array using loop but this code didn't display anything. Please help

using System;
using System.Text;
   
namespace pra
{
    class program
    {
        public static void Main(string[] args)
        {
      
        person[] person1 = new person[2];

        person1[0] = new person("Mike");
        person1[1] = new person("John");

        for (int i =0; i < person1.Length; i++)
        {
            Console.WriteLine("{0}",person1[i].Name);
        }
    }


    
}
class person
{
    string name;
    public string Name { get; set; }

    public person(string name)
    {
        this.name = name;
    }
}

}

3
  • Your person class has a private member name which you set in the constructor and a public auto-implemented property Name which you don't set - and you try to print the property which is not set Commented Sep 28, 2022 at 21:07
  • Your class Person doesn't return anything when you call Name property. Commented Sep 28, 2022 at 21:08
  • 2
    Since you are learning, ... Consider learning the naming conventions. If you were clearer on the naming and capitalization, the error might have been more apparent. Also, did you put breakpoints in your code and step through it? The debugger is great at making errors visible Commented Sep 28, 2022 at 21:10

3 Answers 3

3

You're setting a value to the name field (which is never read), then reading from the Name property (which was never set).

You don't need (or want) both of them here. Remove the field and just use the property:

class person
{
    public string Name { get; set; }

    public person(string name)
    {
        this.Name = name;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The way to make it work is to change your Person class

class person
{
    string name;
    public string Name { get=>name; set=>name=value; }
    public person(string name)
    {
        this.name = name;
    }
}

Comments

0

You don't need the field name and an auto property Name at the same time. If you store the value in name it, won't be accessible by the property Name, unless you write a getter and a setter to do so. But with C# you can skip all that and just use an auto property with a hidden backing field.

class Person
{
    public string Name { get; set; }

    public Person(string name)
    {
        this.Name = name;
    }
    public override string ToString()
    {
        return Name;
    }
}

You can also simplify the conversion between Person and string by overriding the ToString() method. See example usage below:

class Program
{
    public static void Main(string[] args)
    {
      
        Person[] array= new Person[2];

        array[0] = new Person("Mike");
        array[1] = new Person("John");

        for (int i =0; i < array.Length; i++)
        {
            Console.WriteLine(array[i]);
        }
    }
}

You will notice that the WriteLine() statement will display each person's Nameproperty because that is what theToString()` method instructs it to do. Control of what and how it is displayed is in the class and not with the program.

PS. Also, note that class names and types should be capitalized in order to distinguish them from fields and variables. Properties also should be capitalized.

Comments

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.