User Tools

Site Tools


notes:csharp:linq

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:linq [2018/12/13]
leszek [LINQ]
notes:csharp:linq [2019/01/18] (current)
leszek [Operators]
Line 629: Line 629:
  
   * //Cast// casts each element to the target type and fails if any element is not of the right type.   * //Cast// casts each element to the target type and fails if any element is not of the right type.
 +  * //Cast// uses an old style cast rather than `as` operator. It means that `Cast<​T>​` does not need to have a class constraint.
   * //OfType// skips any elements of the wrong type.   * //OfType// skips any elements of the wrong type.
  
Line 654: Line 655:
 Example: Cast Example: Cast
 <code csharp> <code csharp>
-int[] arr1 = { 12}; +var list new ArrayList ​{"​C"​"​B"​"​A"​}; // ArrayList stores its elements as objects 
-List<intresult1 ​arr1.Cast<int>().ToList(); +IEnumerable<stringquery list.Cast<string>().OrderBy(a => a); 
-foreach (int i in result1) Console.WriteLine(i); // 1 2 3+foreach (var a in query) Console.WriteLine(a); // A B C
  
 object[] arr2 = { 1, "​A",​ 3 }; object[] arr2 = { 1, "​A",​ 3 };
notes/csharp/linq.txt · Last modified: 2019/01/18 by leszek