-2

Hello I am new to programming and I came across this problem. I created some class and want it to have these data types.

public struct Test 
{
    public int  ID;
    public string x ;
    public string y;
    public string z ;
}

My problem is because I need to enter a lot of data in that struct and I don't know is there any faster and better method of doing this besides this one. Can someone tell me better solution for this with using struct or without it.

Test example; 
example.ID = 1;
example.x = "Something ";
example.y = "Something  ";
example.z = "Something "; 

I thought about creating list with that structure like this but I don't know how to work with this type of data and how to manipulate it.

List<Test> test1 = new List<Test>(); 
7
  • 2
    is there any faster and better method of doing this - doing what? Which "method" you've use now and why you think it's slower/worse? Can someone tell me better solution - I suppose not, because you actually didn't ask what you want/trying to do. E.g. you said "I have a bag, i put there a book, but is there faster/better way to manipulate over the book". How you imagine your manipulation? Commented Nov 9, 2023 at 10:50
  • "My problem is because I need to enter a lot of data in that struct" -- One integer value and three strings doesn't look like a lot to me. Maybe you mean something else? Commented Nov 9, 2023 at 10:53
  • 1
    Btw is the whitespace at the end of the "Something " strings important? If not, I would suggest to edit the question and remove the whitespace, because it might distract people from your main problem (which is still unclear to me). Commented Nov 9, 2023 at 10:56
  • Pay somebody to enter. Commented Nov 9, 2023 at 10:59
  • 1
    Anyway: you should usually first just do the most naive and easy appraoch, which in your case seems like just typing all these properties. When you got that, you should add abstractions to reduce duplication and things. But you shouldn't start off by any kind of optimzation, unless you really know what you actually need to achieve. In other words: first build something that works, afterwards optimize. Commented Nov 9, 2023 at 11:10

1 Answer 1

1

You could continue doing it like that, initialize each field in separate line. This is fine, and readable. It takes space in code, but space is not bad. Whatever you want to replace it with will also take space, anyway.

Problem with structs with fields that can change after construction (also known as "mutable structs") is when they become properties of another class. Ask yourself do you really need a struct, or is class just as good here. There is a fundamental difference between a struct and a class. Follow the link above to find out about it. This is the main reason why all the solutions below exist:

You could create a constructor that takes all the parameters.

If there is more than one way to create the object, and the parameters are very different then you could have several static methods that have descriptive names and appropriate parameters, and return the struct.

If parameters are truly numerous (you decide what that means for you), and the lots of them could be omitted, you could have a Dictionary<string, object> as just one parameter, with key being the parameter name. This solution defeats the type safety that C# provides, but in some cases code readability is more important than type safety. Instead of object you could have string if all your parameters are in fact strings. I wouldn't use List here, unless the parameters are ordered by their nature.

Or you could have a combination of the above.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.