Help understand C # code

Can someone explain the following method to me? I don’t quite understand what he is doing and how he is doing it.

private List<Label> CreateLabels(params string[] names) { return new List<Label>(names.Select(x => new Label { ID = 0, Name = x })); } 
+7
c #
source share
6 answers

Divide it into different bits:

 private List<Label> CreateLabels(params string[] names) 

This means that we are declaring a method that returns a list of Label references. We take an array of string references as parameters and declare it as an array of parameters, which means that callers can simply specify the following arguments:

 List<Label> labels = CreateLabels("first", "second", "third"); 

(Or they can explicitly pass in an array of strings as usual.)

Now, to understand the body, we will separate it as follows:

 IEnumerable<Labael> labels = names.Select(x => new Label { ID = 0, Name = x }); List<Label> list = new List<Label>(labels); return list; 

The second and third lines should be fairly simple - they simply construct a List<Label> from a sequence of labels, and then return it. The first line is likely to cause problems.

Select is an extension method for the generic type IEnumerable<T> (a sequence of elements of type T ), which lazily returns a new sequence by performing a projection as a delegate.

In this case, the delegate is set using the lambda expression as follows:

 x => new Label { ID = 0, Name = x } 

This says: "Given x , create a Label and set its ID property to 0, and its Name property to x ." Here, the type x defined as string , because we call Select in an array of strings. This is not just using a lambda expression, but also an object initializer expression. The new Label { ID = 0, Name = x } equivalent:

 Label tmp = new Label(); tmp.ID = 0; tmp.Name = x; Label result = tmp; 

We could write a separate method for this:

 private static Label CreateLabel(string x) { Label tmp = new Label(); tmp.ID = 0; tmp.Name = x; return tmp; } 

and then called:

 IEnumerable<Label> labels = names.Select(CreateLabel); 

What the compiler does for you behind the scenes is effective.

Thus, the projection creates a sequence of labels with the names specified by the parameter. The code then creates a list of tags from this sequence and returns it.

Note that it (IMO) will be more idiomatic LINQ to write:

 return names.Select(x => new Label { ID = 0, Name = x }).ToList(); 

instead of explicitly creating a List<Label> .

+18
source share

In general, the meaning behind well-written code is often inferred by the code itself. This is called a "self-documenting code." A code that does not follow this should hopefully be accompanied by comments.

In this case, here is how you can understand what the code does:

The method gives you a few key bits of information:

  • The name of the method. This allows you to find out what this method does. In this example, the method is called CreateLabels

  • Method signature This indicates what the method requires. In this example, there is an array of type string with the params . The params indicates that this method will accept one or more names as inputs to the method and will collect them into a single array.

  • Return method This will say that you want the method to give you when it is done. In this example, it returns a list of shortcuts.

So, since the method is called CreateLabels , it takes a series of rows with names and returns a list of tables, we can say that "Given several names, this method will return a list of tables",

Now, if you need to know how the method does this, look at the body.

return new List<Label>(names.Select(x => new Label { ID = 0, Name = x }));

For those who do not understand the linq or lambda expression, this is a little difficult to understand.

Allows us to work from the outside in:

return new List<Label>() builds a new list and returns a new list and calls the method exit.

names.Select() Is a method that will apply a set of operations to all objects in the array and returns a new collection that will contain the result of applying these operations.

x=>new Label {ID = 0, Name = x} is a lambda expression that states: x goes to a new Label object for which the ID field is 0 and Name is the value x .

Putting it all together, the operator return new List<Label>(names.Select(x => new Label { ID = 0, Name = x })); creates a new label object for each name in names and stores it in a new list, which is returned at the end of the method.

This is the recording method:

 List<Labels> toReturn = new List<Labels>(); foreach(string name in names) { Label l = new Label(); l.ID = 0; l.Name = name; toReturn.Add(l); } return toReturn; 
+9
source share

It just takes a set of variable-sized names and turns them into a list of labels with an identifier set to 0.

 List<Label> labels = CreateLabels("one", "two", "three"); foreach(Label label in labels) { Console.WriteLine(label.Name); Console.WriteLine(label.ID); } 

This will print:

one
0
two
0
three
0

+6
source share

This code returns a collection of ( List ) Label classes. The Foreach string variable in the names input parameter creates a new instance of the Label class with an identifier equal to 0, and the Name property equal to the current names element

You can change this code as follows:

 private List<Label> CreateLabels(params string[] names) { List<Label> result = new List<Label>(); foreach(string name in names) { Label label = new Label() label.ID = 0; label.Name = name; result.Add(label); } result; } 
+3
source share

you need to study this linq 101 example , then you yourself know the meaning of this code.

+1
source share

Ok, here goes:

  new Label { ID = 0, Name = x } 

This creates a new Label instance with ID set to 0 and Name set to x .

  x => new Label { ID = 0, Name = x } 

This is called lambda expression - this is the definition of an anonymous function that takes a single input ( x ) and returns a new instance of the label. You may find it easier to read x => ... because " x goes to ...".

  names.Select(x => new Label { ID = 0, Name = x }) 

This calls the Select method of the names object, passing the above lambda expression as a parameter. We see that the names type is an array of string . In fact, this method - string[] does not define the Select method, this Select method ( here on MSDN) is defined elsewhere (in the System.Core assembly)

The select method is simply "Projects each element of the sequence into a new form." - essentially it converts each element into an array into another object (in our case, a Label instance)

To clarify: the above statement returns an IEnumerable (collection view) of Label objects, one for each row in names .

  return new List<Label>(...); 

Returns a new list of Label objects - in our case we pass our IEnumable returned by calling Select .

In short:

This method lists the Label objects, one for each line in names . Each Label object has its own ID set to 0 , and its Name set to a string value from the names array.

Does this help explain what is happening here?

+1
source share

All Articles