How to loop through the properties of a Class?
Something like:
object obj = new object();
PropertyInfo[] properties = obj.GetType().GetProperties();
foreach (var p in properties)
{
var myVal = p.GetValue(obj);
}
Note you need to allocate the object and pass it into the PropertyInfo.
Iterate through several field strings
I don't exactly know what the term for this kind of storage is
Those are ValueTuple
s.
I want to use the
Rank
property when using other commands
You can create arrays of ValueTuple
s as well:
var developers = new []
{
(Name: "Sophie", Rank: "DEVELOPER"),
(Name: "Aldrige", Rank: "DEVELOPER"),
};
Which can then be iterated:
foreach (var developer in developers)
{
Console.WriteLine($"{developer.Name} has rank: {developer.Rank}");
}
If you need the names of all developers and members, you could use LINQ:
var members = new []
{
(Name: "John", Rank: "MEMBER"),
(Name: "Hammond", Rank: "MEMBER"),
(Name: "Paul", Rank: "MEMBER"),
};
var names = developers.Concat(members).Select(x => x.Name);
Related Topics
How to Store List of Data from Database to Arraylist or List in C#
C# How to Check If a Url Exists/Is Valid
The Ssl Connection Could Not Be Established
What Could Cause an Assignment to Not Work
C# File.Delete, File Being Used by Another Process
Using Recursion to Add Odd Numbers
Calling a Function from Another Class in C#/.Net MVC App
Asp.Net Core Identity Successful Login Redirecting Back to Login Page
String to Decimal With 2 Decimal Places Always
How to Call a Method Daily, At Specific Time, in C#
How to Write Data to a Text File Without Overwriting the Current Data
Sorting String Values Without Using Any Method/Function
Extract First Element from Json
Sending Array of Bytes from Client to Server