Replies: 5 comments
-
Disclaimer: I am not sure I understand what you are saying. Are you asking "why, on line 14 can I not simply use the syntax
This question doesn't really belong here, perhaps community members at Stack Overflow, Reddit, MSDN Forums or other resources may be more helpful. This particular repository is for describing the current and future versions of the C# language. "This is where new C# language features are developed, adopted and specified." If instead what you are trying to do is propose that the syntax |
Beta Was this translation helpful? Give feedback.
-
Ideally, I think this should yield an error: "could not assign/implicitly convert My opinion is that the behavior of implicit conversion through assignment is not optimal in this case. A pretty big gotcha for me. I was expecting an error like "you can't assign yada yada", if array operations are not possible on the query result, no matter how legal the conversion. I think that, on line 14, |
Beta Was this translation helpful? Give feedback.
-
Every class/struct is implicitly convertible to the interfaces that they are declared to implement, and single dimensional arrays are implicitly convertible to
A proposal that makes breaking changes to those is dead in the water due to how much code depends on them. |
Beta Was this translation helpful? Give feedback.
-
I understood this assignment:
as an expression leading to a explicit conversion (cast), a user-defined conversion or a conversion with helper classes, not an expression prepared for a implicit conversion to another type. I would like this mechanism to not happen: |
Beta Was this translation helpful? Give feedback.
-
In fact you could write it yourself (you would need public static class Enumerable
{
public static IEnumerable<T> Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
foreach(var item in source)
{
if (predicate(item)) yield return item;
}
}
public static T[] ToArray<T>(this IEnumerable<T> source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
return new List<T>(source).ToArray();
}
} (the actual implementation has a bunch more code for a few tricks to be faster; also for reference, Aside from the fact that these methods are named what they are, they are not in any way special. You could name them public class Program
{
static void Main(string[] args)
{
int[] fib = new[] {0, 1, 1, 2, 3, 5};
IEnumerable<int> fib2 = fib.If(x => x > 2).Evaluate();
Console.WriteLine($"{ fib2.Evaluate()[0] }");
int[] fib3 = fib.If(x => x > 2).Evaluate();
Console.WriteLine($"{ fib3[0] }");
}
} |
Beta Was this translation helpful? Give feedback.
-
Should (?) catch type changing from collection to array here:
IEnumerable<int> fib2 = fib.Where(x => x > 2).ToArray();
.Catches type change here:
Console.WriteLine($"{ fib2[0] }");
.Fixable by
Console.WriteLine($"{ fib2.ToArray()[0] }");
.Why does it take two
ToArray()
or why is the firstToArray()
nullified byIEnumerable<int>
type annotation?Beta Was this translation helpful? Give feedback.
All reactions