0

How can I create correct struct array without using jagged array? I have tried -

student[] newarray = new student[]{student_info,student2_info};
Console.WriteLine(newarray[0]);

But i get "project name.student" in console

public struct student
{
    public string Name { get; set; }
    public string Last_Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}
class H
{
    static void Main(string[] args)
    {
        student student_info = new student();
        student_info.Name = "Mike";
        student_info.Last_Name = "Johnson";
        student_info.Address = "Baker str. 84/4a";
        student_info.City = "New LM";
        student_info.Country = "Paris";

        student student2_info = new student();
        student student3_info = new student();
        student student4_info = new student();
        student student5_info = new student();

        string[] my_array1 = { student_info.Name,  student_info.Last_Name,   student_info.Address,  student_info.City,   student_info.Country };
        string[] my_array2 = { student2_info.Name, student2_info.Last_Name,  student2_info.Address, student2_info.City,  student2_info.Country };
        string[] my_array3 = { student3_info.Name, student3_info.Last_Name,  student3_info.Address, student3_info.City,  student3_info.Country };
        string[] my_array4 = { student4_info.Name, student4_info.Last_Name,  student4_info.Address, student4_info.City,  student4_info.Country };
        string[] my_array5 = { student5_info.Name, student5_info.Last_Name,  student5_info.Address, student5_info.City,  student5_info.Country };

        string[][] list = new string[][] { my_array1, my_array2, my_array3, my_array4, my_array5 };
        for (int x = 0; x < 5; x++) 
        {
            for (int y = 0; y <= 4; y++) 
            { 
                //  Console.WriteLine(list[x][y]);
            }

            student[] newarray = new student[]{student_info,student2_info};
            Console.WriteLine(newarray[0]);
        }
    }
}

3 Answers 3

3

The reason you get 'project name.student' is that this is the default output of the ToString() method of your struct. You need to add an override of ToString() to your Student struct that will return whatever you want to be written to the console.

Another option is to send to the Console.WriteLine a property of your struct, such as Console.WriteLine(newarray[0].Name); (as suggested by fix_likes_coding and Hellfire in the other answers to this question )

You can use any of these options, to my personal taste the override of ToString seems more elegant.

Sign up to request clarification or add additional context in comments.

2 Comments

Elegant depending on what the intent is, if you just want to output the name then the first is more elegant, if you wanted to output "Name LastName (Address)" overridding ToString is so much better than concatenation or string.format ;)
@Hellfire: Yes and no. It's generally more elegant because you don't have to specify the property in the Console.WriteLine. It's encapsulated in the ToString(). However, if the common use is to show full name, and in another case you wish to show only the first name, of course the elegant way would be to return the full name using ToString() and write Console.WriteLine(newarray[0].Name);
2

What are you hoping to output to the console?

What you are seeing is the full type name of the object due to the default object.ToString implementation. You need to pick the property to output to the console, not the object if you want to see something.

Change this:

 Console.WriteLine(newarray[0]);

To this:

 Console.WriteLine(newarray[0].Name);

and your output will be the students name.

Comments

2

ProjectName.student is the type of the struct. The default ToString() of an object or struct will print its type. Now depending on what properties of the student you want to write to output you can do something like:

Console.WriteLine(String.Format("Students name: {0}", newarray[0].Name));

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.