New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: A simple for loop #1356
Comments
How often do you need a loop that will run n times with no way to determine the current iteration? |
I agree that current C-inspired for-loop is not the best. It's clumsy to write and unnecessarily verbose. The reason it looks like it does is for flexibility. But in C#, we can not take advantage of all the flexibility that C offers. When you use for-loops, it is common to increase by 1. So in most cases, it is unnecessary to write i++. I agree that there should be a shorter notation for counted loops, but the index variable must be included. We maybe can get inspection from other languages. |
I already said, this does not solve any specific problem. May be new comers will be more comfortable to start with this simple for loop. That's all. |
@Unknown6656 ah, I had forgotten about that. Thanks for the reminder about slices/ranges :-) |
@Unknown6656 Honestly though that isn't strictly better than foreach (0..10)
{
// ...
} It might not hit the +100 point mark but I think it's worth considering. |
That makes me sad. |
@bondsbw I know ....... me too ...... But it is a brutal world out there .... If you see signs of Javascript, Python or PHP - it is not yet too late. |
I've personally always been a fan of this looping construct:: int count = 10;
while(count --> 0){
// ...
} The |
Why not scope the iteration variable? for (var count = 10; count --> 0;)
{
// ...
} |
for (var i = 1 to 10 step 2) { ... } just kidding... 🤣 |
This is my preferred one. for (object boxedCounter = Activator.CreateInstance(Type.GetType("System.Decimal"), Activator.CreateInstance(Type.GetType("System.Int32")));
(Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Equals")).ToArray()[0].Invoke(Type.GetType("System.Decimal").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("CompareTo")).ToArray()[0].Invoke((decimal)boxedCounter,
new object[] { Activator.CreateInstance(Type.GetType("System.Decimal"), Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "10" })) }), new object[] {
Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "-1" })})).ToString().Cast<object>().SequenceEqual(new object[] { 'T', 'r', 'u', 'e'});
boxedCounter = Type.GetType("System.Decimal").GetMethod("op_Addition").Invoke(null, new object[] { (decimal)boxedCounter, Activator.CreateInstance(Type.GetType("System.Decimal"), Type.GetType("System.Int32").GetRuntimeMethods().Where(m => m.Name.SequenceEqual("Parse")).ToArray()[0].Invoke(null, new object[] { "1" })) }))
{
var i = (int)(decimal)boxedCounter;
} |
What about this? #pragma language using VB
For i = 0 To 10
#pragma language restore
Console.WriteLine(i);
} |
namespace MyNamespace
{
public static void For<T>(int length, Func<T> body)
{
for (int i = 0; i < length; i++)
body();
}
}
...
using static MyNamespace.For;
public static Main() => For(10, () =>
{
Console.WriteLine();
}); |
This is a tiny feature request and does not actually solve any specific problem. When encountered
for
loops in C, I did not get that much resemblance between the namefor
and the looping mechanism it provides. In my mind, to bear the resemblance, for would look something simpler, like this-So, hereby, I am requesting this feature in C#.
The text was updated successfully, but these errors were encountered: