简体   繁体   English

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

[英]static/generic/non-generic definitions error

Techies-- I think I'm defining this static extension correctly for Split, I obviously am not because the message: Extension method must be defined in a non-generic static class. 技术人员-我想我为Split正确定义了此静态扩展,我显然不是因为以下消息:扩展方法必须在非通用静态类中定义。

This is a simple c# console program to test something. 这是一个简单的c#控制台程序,用于测试某些内容。 Here's what I have: 这是我所拥有的:

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;
   }

What is it that I'm doing incorrectly? 我做错了什么?

Your class Program is not static as requested by the error message. 您的类Program不是错误消息所要求的静态。

  • Add the static directive to the class declaration: static指令添加到类声明中:

      static class Program { // ... 
  • or move Split into another static class altogether. 或将Split移到另一个静态类中。

Then your code should compile again. 然后,您的代码应再次编译。

Hello your container class must be static 您好您的容器类必须是静态的

Set you method in static class 在静态类中设置方法

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;
   }

}

You need to define your extension method in a class, like this: 您需要在类中定义扩展方法,如下所示:

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; 
   } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM