1

Possible Duplicate:
Struct v/s Class in C# - Please explain the behavior.

I am confused that have seen often that we create custom object by following

public class Student
{
  public string Name{get;set;}
  public string Class{get;set;}
  public string Section{get;set;}
}

and create instant and use its attributes as property for example

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

Student studenttoadd = new Student();//Object
studenttoadd.Name = "You";
studenttoadd.Class = "PlayGroup";

absentUsers.Add(studenttoadd);

and so on

but isn't it true that we can do same with Struct?

What is mentioned approach called(example i have given)?

When to Use this approach and when to use struct?

is there any performance /memory allocation difference?

Community
  • 1
  • 1
Afnan Bashir
  • 7,319
  • 20
  • 76
  • 138
  • 2
    See the following answers: http://stackoverflow.com/questions/5620843/does-it-make-sense-to-define-a-struct-with-a-reference-type-member/5621610#5621610, http://stackoverflow.com/questions/5456463/protection-level-of-a-struct-field-within-a-class/5456563#5456563 – Cody Gray - on strike Jun 07 '11 at 15:50
  • @Cody: I started to write an answer and realized that 'Struct vs. Class' has been repeated many times. – IAbstract Jun 07 '11 at 15:55
  • It would be a huge mistake to use `struct` here. A common mistake for people from C++ etc, but a mistake. – Marc Gravell Jun 07 '11 at 15:57
  • so basically when would you prefer struct over this approach – Afnan Bashir Jun 07 '11 at 16:17

0 Answers0