C # define a dynamic array (generic ),
C # when defining dynamic arrays in code, it is very exquisite. We usually don't know how much space we need, and the conversion between data is also a problem.
Question 1:
The GUID type cannot be converted to GUID []
Answer:
We usually use arrays when trying:
<span style="font-size:18px;"> Guid[] staff = new Guid[] { settingEvaluationBLL.LoadEnities(s => s.YzStaffEntityEvaluaterID.ID == EvaluationID).ToList().Select(u=>u.CriticsID).ToList().FirstOrDefault() };</span>
We all know that. FirstOrDefault () returns the first element in the sequence. If the sequence does not contain any element, the default value is returned. Therefore, it is not a returned array at all.
In fact, the new Guid {string type} can be converted to the GUID type.
Therefore, we use. ToArray <Guid> ();, which is a set and the size of the set can be changed.
The correct statement should be:
<span style="font-size:18px;"> List<Model.YzSettingEvaluationEntity> resultlist = settingEvaluationBLL.LoadEnities(s=>s.YzStaffEntityEvaluaterID.ID ==EvaluationID).ToList(); Guid[] str = resultlist.Select(u => u.CriticsID).ToArray<Guid>();</span>
Question 2: How to obtain any type of set?
<span style="font-size:18px;"> String[] score;Decimal[] scorevalue;score = scoreslist.Select(u => u.ScoreLevel).ToArray();scorevalue = scoreslist.Select(u => u.Scores).ToArray();</span>
As we can see, the initial capacity of Arraylist is 0 by default. As the element is added to ArrayList, the capacity will automatically increase through reallocation as needed.
Of course, the benefits of Arraylist will certainly be flawed. Its disadvantage is that the type is insecure. We use it to store data of the type, such as int and string. Using ArrayList means that all values must be packed into Object objects, when using a set element, you also need to perform the unpacking operation, which brings a lot of performance loss.
Question 3: how to define various types of generics?
Generic:
<span style="font-size:18px;"> IList<String>SeriesNameSave = new List<String>();</span>
How to pack a wildcard:
Here is an example of the GUID type:
<span style="font-size:18px;">IList<GUID> SeriesIDSave = new List<GUID>();SeriesIDSave.Add(seriesBLL.LoadEnities(u => u.SeriesName == SeriesValue[i]).Select(u => u.ID).FirstOrDefault()); </span>
When resizing a generic model, Add is used.
If we use a generic set, it will be more convenient to use. We can see that the GUID type is also obtained:
<span style="font-size:18px;"> SeriesValue = resultlist.Select(u => u.YzStaffEntityCriticsID.YzSeriesEntityID).ToArray<Guid>();</span>
In this way, we can solve the problem in one sentence.
Summary:
Let's look at the differences between arrays, sets, and generics:
Any type of elements can be placed in the Set, which will automatically increase. type conversion is required when the collection is taken out.
A generic set can only contain elements of the defined type, and will automatically increase. type conversion is not required during extraction.
The array can only contain elements of the defined type, but does not automatically increase. type conversion is not required when the array is retrieved.