0

I have a function that collects stats in a loop and stores them in 7 different variables. I want to group these variables into some kind of data structure, I don't want to create any class or a struct with global visibility for it, because I only need it in one function. I can use tuples, like List< (int, int, string, ... > ) for grouping, but what if I want to use an anonymous type, how can I create an array of it or put it inside a list? Like:

List< myAnonymousType > list = new();

Are there any other options/data types that I can create inside a function to group my variables, without creating any global data types?

Yekoor
  • 79
  • 3
  • 1
    Check [this](https://stackoverflow.com/q/612689/9363973) Q&A, otherwise you could always create a `private` or `internal` class or struct or record if you want to have instance methds – MindSwipe May 23 '22 at 08:54
  • An anonymous class is converted by the compiler into a normal internal class under the hood. I don't see a reason for not doing the same as well. You can even make the class a private nested if you want. – freakish May 23 '22 at 08:55
  • @MindSwipe it looks a bit uglier than I hoped, but yes, it answers my question, thanks. Private class isn't really an option, I need local visibility. – Yekoor May 23 '22 at 09:13
  • @freakish that's a very strange POV. You don't write code "under the hood", but in an IDE. – Yekoor May 23 '22 at 09:13
  • Define "local visibility". Inside the same namespace? Use `internal`. Inside the same class? Use `private`. – MindSwipe May 23 '22 at 09:22
  • @Yekoor this is not what I'm saying. I'm saying you should explicitly create a class with either `internal` or `private` visibility, because that's exactly the same thing what the compiler does for you with the "anonymous classes" feature. – freakish May 23 '22 at 09:23
  • @MindSwipe it should only be visible inside a function, like a local variable, because I have many different functions like that one with different variables that partly intersect and I don't want to create a public or even private data structure for all of them. – Yekoor May 23 '22 at 09:25
  • @Yekoor types are not variables and their visibility cannot be restricted to a function only. Anonymous classes are visible in your assembly as well. Their name is compiler generated, but they can be retrieved with reflection. And so IMO you are overthinking things. Just create an internal/private class and be done with it. – freakish May 23 '22 at 09:30
  • @freakish it's just a temporary object that I need only inside a function and I have many functions like that with different set of variables, it's not a good idea to create a dozen of private data structures if I never re-use them and don't need them outside a single function. – Yekoor May 23 '22 at 09:32
  • @freakish I'm not overthinking anything, it's more of a theoretical question, I can just use tuples, but I wonder if there are other elegant ways without code clutter and unneeded global types that intelliSense will pop up in a huge list. – Yekoor May 23 '22 at 09:35

0 Answers0