Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make answer class a struct?

Tags:

c#

class

struct

Usually play around with making games but I'm taking a detour into making a little question and answer app, for educational purposes.

Anyway I have a question class which holds numerous members: the question itself, the answer to the question, an array of possible answers etc. No doubts this should be a class.

My answer class however only holds a string, an Enum and an int id number as shown below:

public class Answer
{
    public string Answer { get { return answer;} private set { answer = value; } } 
    public Answer_Category = Some_Value; // The enum.
    public int ID { get { return id; } private set { return id; } }

    private string answer;
    private int id;
}

Ok so it holds two strings, also the ctor has been out.

So should I be making this a struct? I ask as it seems comparable to making a Vector a struct, being such a small data structure 'n all.

Naturally being a question and answer application the answer class/struct is going to be the subject of a lot of search calls.

IMO this should be a struct - solely because of the size of the structure, haven't played around with C# for some time though so just looking for some clarification.

like image 342
Lee Brindley Avatar asked Jan 19 '14 08:01

Lee Brindley


People also ask

Can you make a struct in a class?

Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one. It is called inner class.

Can you use struct in class C++?

The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.

Is struct and class same?

The only difference between a struct and class in C++ is the default accessibility of member variables and methods. In a struct they are public; in a class they are private.

Why do we use struct in C++?

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, string, bool, etc.).


1 Answers

The decision boils down to whether you want a value type or a reference type. In other words what do you want the assignment operator to mean? If you want assignment to copy the value, use a struct. If you want assignment to take another reference e, use a class.

like image 69
David Heffernan Avatar answered Sep 20 '22 18:09

David Heffernan