I know the Title might sound confusing to some of you. But I currently have a weird issue with my C# code on my MacBook. Let me explain: I created a class with the Name "Stadt" and I want to create a list with the Type Stadt and all its attributes. Somehow I can only call the created List called "staedte" in other methods within the class. But I am not able to call it in the class itself. I don't really sure I understand. I am still new to c# I would appreciate any help.This is the image of my code
I tried the same with net 6.0 but that didn't change anything.
CodePudding user response:
You have declared:
List<Stadt> staedte;
in your code and omitted the accessibility (private/protected/internal/public) which implicitly makes the field private (only accessible from within the own class).
Try:
public List<Stadt> staedte;
Be aware that exposing fields outside your class is not best practice. Use a property instead.
Additionally you cannot write code inside the class directly. you have to write your code in methods or properties.
CodePudding user response:
Try:
public static List<Stadt> staedte = new List<Stadt>()
{
new Stadt(){prop1 = value1, prop2 = value2, ...},
... ,
...
};
i think it can work for you.