C# 静态/通用/非通用定义错误

C# 静态/通用/非通用定义错误,c#,C#,技术人员-- 我认为我为Split正确地定义了这个静态扩展,显然不是因为message:extension方法必须在非泛型静态类中定义 这是一个简单的c#控制台程序,用于测试某些东西。以下是我所拥有的: class Program { static int Main(string[] args) { int[] numbers = new int[10000]; for (int i = 0; i < numbers.Length; ++i)

技术人员-- 我认为我为Split正确地定义了这个静态扩展,显然不是因为message:extension方法必须在非泛型静态类中定义

这是一个简单的c#控制台程序,用于测试某些东西。以下是我所拥有的:

class Program
{ 
  static int Main(string[] args)
  {
    int[] numbers = new int[10000]; 
           for (int i = 0; i < numbers.Length; ++i) 
               numbers[i] = i; 

  int[][] sectionedNumbers = numbers.Split(1000); 
   .
   . //blah blah blah .. rest of code

 return 0;
 }

 public static T[][] Split<T>(this T[] arrayIn, int length)
 {
  bool even = arrayIn.Length % length == 0;
    .
    .
    . // blah blah .. more code

   return newArray;
   }
类程序
{ 
静态int Main(字符串[]args)
{
整数[]个数=新整数[10000];
对于(int i=0;i

我做错了什么?

您好,您的容器类必须是静态的

在静态类中设置方法

public static class Extension
{
 public static T[][] Split<T>(this T[] arrayIn, int length)
 {
  bool even = arrayIn.Length % length == 0;
    .
    .
    . // blah blah .. more code

   return newArray;
   }

}
公共静态类扩展
{
公共静态T[][]拆分(此T[]数组长度,整数长度)
{
布尔偶数=arrayIn.Length%Length==0;
.
.
.//诸如此类..更多代码
返回新数组;
}
}

您好,您的容器类必须是静态的

在静态类中设置方法

public static class Extension
{
 public static T[][] Split<T>(this T[] arrayIn, int length)
 {
  bool even = arrayIn.Length % length == 0;
    .
    .
    . // blah blah .. more code

   return newArray;
   }

}
公共静态类扩展
{
公共静态T[][]拆分(此T[]数组长度,整数长度)
{
布尔偶数=arrayIn.Length%Length==0;
.
.
.//诸如此类..更多代码
返回新数组;
}
}

您的类
程序
不是错误消息所要求的静态程序

  • static
    指令添加到类声明中:

     static class Program
     {
         // ...
    
  • 或者将
    Split
    移动到另一个静态类中


然后您的代码应该再次编译。

您的类
程序
不是错误消息所要求的静态的

  • static
    指令添加到类声明中:

     static class Program
     {
         // ...
    
  • 或者将
    Split
    移动到另一个静态类中


然后您的代码应该再次编译。

您需要在类中定义扩展方法,如下所示:

public static class ArrayExtensions
{
 public static T[][] Split<T>(this T[] arrayIn, int length) 
 { 
  bool even = arrayIn.Length % length == 0; 
    . 
    . 
    . // blah blah .. more code 

   return newArray; 
   } 
}
公共静态类ArrayExtensions
{
公共静态T[][]拆分(此T[]数组长度,整数长度)
{ 
布尔偶数=arrayIn.Length%Length==0;
. 
. 
.//诸如此类..更多代码
返回新数组;
} 
}

您需要在类中定义扩展方法,如下所示:

public static class ArrayExtensions
{
 public static T[][] Split<T>(this T[] arrayIn, int length) 
 { 
  bool even = arrayIn.Length % length == 0; 
    . 
    . 
    . // blah blah .. more code 

   return newArray; 
   } 
}
公共静态类ArrayExtensions
{
公共静态T[][]拆分(此T[]数组长度,整数长度)
{ 
布尔偶数=arrayIn.Length%Length==0;
. 
. 
.//诸如此类..更多代码
返回新数组;
} 
}

你的类声明中缺少一个
关键字。你的类声明中缺少一个
关键字。O.R.Mapper——非常感谢,我甚至忘了看程序def…我想把注意力集中在扩展代码上。我都修好了!:-)@plditallo:你能接受这个或其中一个吗如果你的问题已经解决,他会给出其他等价的答案吗?谢谢。O.R.Mapper——非常感谢,我甚至忘了看程序def…我想专注于扩展代码。我都修好了!:-)@plditallo:如果你的问题已经解决,你能接受这个或其他等价的答案吗?谢谢。