Frage

I'm trying to store information in a block of anonymous values

//holds all info
var jobs = new { newJob, numBytes, requiredTime };

then take that information and place it into a list as a single element

//puts above info into a list
joblist.Add(Convert.ToString(jobs));
Console.WriteLine(joblist[0]);   //testing purposes

now what I would like to do is be able to call joblist and take the value of example numBytes at position 4.

Is this possible? Or could someone help with an alternate way of doing this? Much thanks!

War es hilfreich?

Lösung 2

The "normal" thing to do in C# would be to create a class to hold the information that you want to store. For example:

public class Job
{
    public string Name { get; set; }
    public int NumBytes { get; set; }
    public DateTime RequiredTime { get; set; }
}

Then you can add these to a list:

var jobs = new List<Job>();

var aJob = new Job();
aJob.Name = "Job 1";
aJob.NumBytes = 123;

jobs.add(aJob);

Then you can access jobs by its index in the list:

var jobNumBytes = jobs[3].NumBytes;

One thing to note about C#, when you do:

new { newJob, numBytes, requiredTime };

The compiler, at build time, just creates you a strongly typed class (just like the Job class I created above) and generates a random name for it. It infers the property names and types from the variables that you are assigning to it. The created .exe or .dll actually does contain a class definition for this type, you just can't easily get to it from other places in your code. It isn't truly "dynamic". So using that syntax is usually just a lazy way of declaring a class that you just need for a moment. Usually just inside 1 method, then you don't need it any more. Creating a named class is usually what you want to do.

Andere Tipps

Create a named class. Then you can have a list of objects of that type and manipulate that list in any way you want.

Using classes is best-practice for what you are trying to do. By default you should consider storing structured data in an object model consisting of custom classes. There is another answer here which is proposing to use dynamic - this is valid and has its place, but it is more of a last resort solution. What you want is to play to the strength of C# which are rich classes and static typing. Anonymous types are also statically typed, but as you cannot name the type you cannot declare a statically typed list to hold them. You also can't use them as return types of methods.

Actually I don't know exactly what you mean with "now what I would like to do is be able to call joblist and remove for example numBytes at position 4."

But I guess you just want to put the objects in a list and query for numBytes and maybe remove some elements.

With dynamics you can handle dynamic objects...

var jobs = new List<dynamic>();
for (int i = 0; i < 100; i++)
{
    string newJob = "Job" + i;
    int numBytes = i;
    TimeSpan requiredTime = new TimeSpan(0,0,i);
    //holds all info
    var job = new { newJob, numBytes, requiredTime };
    jobs.Add(job);
}

jobs.RemoveAll(p => p.numBytes > 50);

Instead of this, I agree with the comments below your question and would create a normal class which holds the properties you need and simply put instances of that into a list. Dynamics should be used only in very rare situations, and yours doesn't sound like it is extremely special.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top