C# 在不使用for循环的情况下为数组/列表设置相同的值

C# 在不使用for循环的情况下为数组/列表设置相同的值,c#,arrays,C#,Arrays,我想在不使用循环的情况下为数组或列表的特定范围设置相同的值。情况如下: int[] myArray = new int[100]; int lower = 20; int upper = 80; int value = 5; myArray.Method(lower,upper,value); 我已经尝试了myArray.SetValue()和myList.InsertRange(),但它只允许设置一个值,不允许设置范围 有没有C方法可以完成这个任务? 如果没有循环,这可以做到吗?那么

我想在不使用循环的情况下为数组或列表的特定范围设置相同的值。情况如下:

int[] myArray = new int[100]; 
int lower = 20; 
int upper = 80; 
int value = 5;

myArray.Method(lower,upper,value);
我已经尝试了
myArray.SetValue()
myList.InsertRange()
,但它只允许设置一个值,不允许设置范围

有没有C方法可以完成这个任务?
如果没有循环,这可以做到吗?

那么,您可以创建一个大小合适的临时数组,并将其复制到源数组中

Array.Copy(Enumerable.Repeat(value, upper-lower+1).ToArray(), 0,
           myArray, lower, upper-lower+1);
但这是非常低效的(而且,在内部,
可枚举。Repeat
也使用循环)

我看不出简单的
for
循环有什么问题:

for(int i = lower; i <= upper; i++)
    myArray[i] = value;

for(inti=lower;i那么,您可以创建一个大小合适的临时数组,并将其复制到源数组中

Array.Copy(Enumerable.Repeat(value, upper-lower+1).ToArray(), 0,
           myArray, lower, upper-lower+1);
但这是非常低效的(而且,在内部,
可枚举。Repeat
也使用循环)

我看不出简单的
for
循环有什么问题:

for(int i = lower; i <= upper; i++)
    myArray[i] = value;

for(inti=lower;i那么,您可以创建一个大小合适的临时数组,并将其复制到源数组中

Array.Copy(Enumerable.Repeat(value, upper-lower+1).ToArray(), 0,
           myArray, lower, upper-lower+1);
但这是非常低效的(而且,在内部,
可枚举。Repeat
也使用循环)

我看不出简单的
for
循环有什么问题:

for(int i = lower; i <= upper; i++)
    myArray[i] = value;

for(inti=lower;i那么,您可以创建一个大小合适的临时数组,并将其复制到源数组中

Array.Copy(Enumerable.Repeat(value, upper-lower+1).ToArray(), 0,
           myArray, lower, upper-lower+1);
但这是非常低效的(而且,在内部,
可枚举。Repeat
也使用循环)

我看不出简单的
for
循环有什么问题:

for(int i = lower; i <= upper; i++)
    myArray[i] = value;

for(int i=lower;i您需要在某个地方进行循环,因为您使用的硬件很可能不支持这种类型的操作

下面是一个通用扩展方法,它将根据您的规范在所有数组类型上工作:

class Program
{
    static void Main(string[] args)
    {
        int[] ints = new int[5];
        ints.UpdateRange(2, 4, 5);
        //ints has value [0,0,5,5,0]
    }
}

public static class ArrayExtensions
{
    public static void UpdateRange<T>(this T[] array, int lowerBound, int exclusiveUpperBound, T value)
    {
        Contract.Requires(lowerBound >= 0, "lowerBound must be a positive number");
        Contract.Requires(exclusiveUpperBound > lowerBound, "exclusiveUpperBound must be greater than lower bound");
        Contract.Requires(exclusiveUpperBound <= array.Length, "exclusiveUpperBound must be less than or equal to the size of the array");

        for (int i = lowerBound; i < exclusiveUpperBound; i++) array[i] = value;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[]ints=新的int[5];
内部更新(2,4,5);
//ints的值为[0,0,5,5,0]
}
}
公共静态类ArrayExtensions
{
公共静态void UpdateRange(此T[]数组,int lowerBound,int exclusiveUpperBound,T值)
{
Contract.Requires(lowerBound>=0,“lowerBound必须是正数”);
合同要求(exclusiveUpperBound>lowerBound,“exclusiveUpperBound必须大于下限”);

Requires(exclusiveUpperBound您需要在某个地方进行循环,因为您使用的硬件很可能不支持这种类型的操作

下面是一个通用扩展方法,它将根据您的规范在所有数组类型上工作:

class Program
{
    static void Main(string[] args)
    {
        int[] ints = new int[5];
        ints.UpdateRange(2, 4, 5);
        //ints has value [0,0,5,5,0]
    }
}

public static class ArrayExtensions
{
    public static void UpdateRange<T>(this T[] array, int lowerBound, int exclusiveUpperBound, T value)
    {
        Contract.Requires(lowerBound >= 0, "lowerBound must be a positive number");
        Contract.Requires(exclusiveUpperBound > lowerBound, "exclusiveUpperBound must be greater than lower bound");
        Contract.Requires(exclusiveUpperBound <= array.Length, "exclusiveUpperBound must be less than or equal to the size of the array");

        for (int i = lowerBound; i < exclusiveUpperBound; i++) array[i] = value;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[]ints=新的int[5];
内部更新(2,4,5);
//ints的值为[0,0,5,5,0]
}
}
公共静态类ArrayExtensions
{
公共静态void UpdateRange(此T[]数组,int lowerBound,int exclusiveUpperBound,T值)
{
Contract.Requires(lowerBound>=0,“lowerBound必须是正数”);
合同要求(exclusiveUpperBound>lowerBound,“exclusiveUpperBound必须大于下限”);

Requires(exclusiveUpperBound您需要在某个地方进行循环,因为您使用的硬件很可能不支持这种类型的操作

下面是一个通用扩展方法,它将根据您的规范在所有数组类型上工作:

class Program
{
    static void Main(string[] args)
    {
        int[] ints = new int[5];
        ints.UpdateRange(2, 4, 5);
        //ints has value [0,0,5,5,0]
    }
}

public static class ArrayExtensions
{
    public static void UpdateRange<T>(this T[] array, int lowerBound, int exclusiveUpperBound, T value)
    {
        Contract.Requires(lowerBound >= 0, "lowerBound must be a positive number");
        Contract.Requires(exclusiveUpperBound > lowerBound, "exclusiveUpperBound must be greater than lower bound");
        Contract.Requires(exclusiveUpperBound <= array.Length, "exclusiveUpperBound must be less than or equal to the size of the array");

        for (int i = lowerBound; i < exclusiveUpperBound; i++) array[i] = value;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[]ints=新的int[5];
内部更新(2,4,5);
//ints的值为[0,0,5,5,0]
}
}
公共静态类ArrayExtensions
{
公共静态void UpdateRange(此T[]数组,int lowerBound,int exclusiveUpperBound,T值)
{
Contract.Requires(lowerBound>=0,“lowerBound必须是正数”);
合同要求(exclusiveUpperBound>lowerBound,“exclusiveUpperBound必须大于下限”);

Requires(exclusiveUpperBound您需要在某个地方进行循环,因为您使用的硬件很可能不支持这种类型的操作

下面是一个通用扩展方法,它将根据您的规范在所有数组类型上工作:

class Program
{
    static void Main(string[] args)
    {
        int[] ints = new int[5];
        ints.UpdateRange(2, 4, 5);
        //ints has value [0,0,5,5,0]
    }
}

public static class ArrayExtensions
{
    public static void UpdateRange<T>(this T[] array, int lowerBound, int exclusiveUpperBound, T value)
    {
        Contract.Requires(lowerBound >= 0, "lowerBound must be a positive number");
        Contract.Requires(exclusiveUpperBound > lowerBound, "exclusiveUpperBound must be greater than lower bound");
        Contract.Requires(exclusiveUpperBound <= array.Length, "exclusiveUpperBound must be less than or equal to the size of the array");

        for (int i = lowerBound; i < exclusiveUpperBound; i++) array[i] = value;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int[]ints=新的int[5];
内部更新(2,4,5);
//ints的值为[0,0,5,5,0]
}
}
公共静态类ArrayExtensions
{
公共静态void UpdateRange(此T[]数组,int lowerBound,int exclusiveUpperBound,T值)
{
Contract.Requires(lowerBound>=0,“lowerBound必须是正数”);
合同要求(exclusiveUpperBound>lowerBound,“exclusiveUpperBound必须大于下限”);

合同。需要(exclusiveUpperBound A
for
循环似乎是一个明显的选择。这有什么问题吗?你是在寻找一个现有的框架解决方案,还是可以创建自己的扩展方法?你会以某种方式使用一个循环,这只是你把它藏在哪里的问题。@sloth我只是好奇。我经常用我的fo替换它对于我以前不知道的方法,r循环…对于
循环,A
似乎是一个明显的选择。这有什么问题吗?你是在寻找一个现有的框架解决方案,还是可以创建自己的扩展方法?你会以某种方式使用一个循环,这只是你把它藏在哪里的问题。@sloth我只是好奇。这种情况经常发生我正在用我的for循环替换我以前不知道的方法…一个
for
循环似乎是一个明显的选择。这有什么错?你是在寻找一个现有的框架解决方案,还是可以创建自己的扩展方法?你会以某种方式使用一个循环,这只是你把它藏在哪里的问题。@sloth我只是c这种情况经常发生,我用我的for循环来代替我以前不知道的方法…A
for
循环似乎是显而易见的选择。怎么了