分类
标签
.NET 9008 adb android apt asp.net ASP.NET Core audio bug C c++ C++ chrome cmd csharp CSharp css css3 debian debug dotnet dotnet Framework dpkg GDI&GDI+ gdi&gdi+ golang graphics html html5 http java javascript json kali linux linux mci microsoft minimap MSBuild mysql OpenCV PInvoke powershell python QQ rust shell speex sql tutorial ubuntu ui unity vb.net visual studio Visual Studio web Web win32 winapi windows winform WinForm wpf WPF xaml xfce 列表 刷机 前端 加密 反射 反编译 可视化 图像处理 多线程 字符串 安卓 实例 局域网 幻影坦克 库 开发语言 异步 微信 手册 手机 接口 摘要 救砖 数字签名 数字证书 数字音频 数据库 桌面程序 游戏 游戏引擎 源码 爬虫 玩游戏 电脑硬件 笔记 算法 类库 线性代数 编程语言 网络 脚本语言 计算机图形学 计算机基础 设计模式 语音编解码 运维 进制 面向对象编程 音频 音频编码解码
866 字
4 分钟
[C#] Range函数, 好用就完事儿了!
这么多重载, 完全够用了~, 返回值是 IEnumerable<T> 其中, Range函数是简单的循环, RangeEx加入了检测, 不会造成死循环, 且精度非常准确
using System;
using System.Collections.Generic;
namespace NullLib.Range
{
public class NRange
{
public static IEnumerable<int> Range(int stop)
{
for (int i = 0; i < stop; i++)
yield return i;
}
public static IEnumerable<int> Range(int start, int stop)
{
for (int i = start; i < stop; i++)
yield return i;
}
public static IEnumerable<int> Range(int start, int stop, int step)
{
for (int i = start; i < stop; i += step)
yield return i;
}
public static IEnumerable<float> Range(float stop)
{
for (int i = 0; i < stop; i++)
yield return i;
}
public static IEnumerable<float> Range(float start, float stop)
{
for (float i = start; i < stop; i++)
yield return i;
}
public static IEnumerable<float> Range(float start, float stop, float step)
{
for (float i = start; i < stop; i += step)
yield return i;
}
public static IEnumerable<double> Range(double stop)
{
for (int i = 0; i < stop; i++)
yield return i;
}
public static IEnumerable<double> Range(double start, double stop)
{
for (double i = start; i < stop; i++)
yield return i;
}
public static IEnumerable<double> Range(double start, double stop, double step)
{
for (double i = start; i < stop; i += step)
yield return i;
}
public static IEnumerable<int> RangeEx(int stop)
{
int start = 0, step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<int> RangeEx(int start, int stop)
{
int step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<int> RangeEx(int start, int stop, int step)
{
int current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<float> RangeEx(float stop)
{
float start = 0, step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<float> RangeEx(float start, float stop)
{
float step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<float> RangeEx(float start, float stop, float step)
{
float current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<double> RangeEx(double stop)
{
double start = 0, step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<double> RangeEx(double start, double stop)
{
double step = 1, current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
public static IEnumerable<double> RangeEx(double start, double stop, double step)
{
double current = start;
step = step < 0 ? -step : step;
if (start < stop)
for (int i = 0; current < stop; i++, current = step * i + start)
yield return current;
else
for (int i = 0; current > stop; i--, current = step * i + start)
yield return current;
}
}
}
[C#] Range函数, 好用就完事儿了!
https://slimenull.com/posts/20210219070741/