自定义类库作为数组 C#

发布于 2024-11-05 23:41:49 字数 517 浏览 3 评论 0原文

我正在使用 C# 中的一个类库,它有自己的方法,我想从这个库创建一个数组,但是当我在主程序中调用它时,我看不到它的方法。

public class ClassLibrary1
{
    public int num;                       

    public ClassLibrary1 ()
    {

    }

    public void Readdata()
    { 
        Console.Write("write a number ");
        num = int.Parse(Console.ReadLine());
    }
}

program.cs:

ClassLibrary1[] arraynumbers = new ClassLibrary1[5];

arraynumbers.Readdata();

我无法使用Readdata()

谁能帮助我吗?

I'm working with a class library in C# with its own methods and I want to create an array from this library, but I when call it in the main program I cant see its methods.

public class ClassLibrary1
{
    public int num;                       

    public ClassLibrary1 ()
    {

    }

    public void Readdata()
    { 
        Console.Write("write a number ");
        num = int.Parse(Console.ReadLine());
    }
}

program.cs :

ClassLibrary1[] arraynumbers = new ClassLibrary1[5];

arraynumbers.Readdata();

And I can't use Readdata().

Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风筝在阴天搁浅。 2024-11-12 23:41:49

如果您想调用类中的方法,则必须至少创建一个实例。事实上,您所做的就是创建一个空引用数组,然后尝试在该数组上调用您的方法。这是您可以做到的一种方法。

ClassLibrary1[] arraynumbers = new ClassLibrary1[5];
for(int i = 0; i < 5; i++) 
{
    arraynumbers[i] = new ClassLibrary1();
}
arraynumbers[0].Readdata();

If you want to call methods in your class, you'll have to create at least one instance. As it is, all you've done is create an array of null references, and then attempt to call your method on the array. Here's one way you could do it.

ClassLibrary1[] arraynumbers = new ClassLibrary1[5];
for(int i = 0; i < 5; i++) 
{
    arraynumbers[i] = new ClassLibrary1();
}
arraynumbers[0].Readdata();
别再吹冷风 2024-11-12 23:41:49

您不能按照您的方式使用 Readdata,因为 ClassLibrary1[] 是一个 ARRAY 对象,而不是 ClassLibrary1 对象,其中你的方法已经定义了。

你必须做这样的事情:

arraynumbers[0].Readdata();

You can't use Readdata the way you've put it because ClassLibrary1[] is an ARRAY object, not a ClassLibrary1 object, in which your method is defined.

You'd have to do something like this instead:

arraynumbers[0].Readdata();
抠脚大汉 2024-11-12 23:41:49

Readdata()ClassLibrary1 实例的方法,而不是保存 ClassLibrary1 实例的数组。

Readdata() is a method of the ClassLibrary1 instance, not the array that holds ClassLibrary1 instances.

皓月长歌 2024-11-12 23:41:49

不能在该类的集合上调用在类上定义的方法。如果您想在集合上使用方法,请考虑创建一个扩展方法:

public static class ClassLibrary1Extensions
{
    public static Readdata(this ClassLibrary[] value)
    {
        ...
    }
}

第一个参数中的“this”关键字允许您“假装”此方法位于“ClassLibrary1[]”类型或数组上。即扩展该类型。

Methods that are defined on a class may not be called on a collection of that class. If you want to use a method on a collection, consider making an extension method:

public static class ClassLibrary1Extensions
{
    public static Readdata(this ClassLibrary[] value)
    {
        ...
    }
}

The "this" keyword in the first parameter allows you to "pretend" this method is on a type of "ClassLibrary1[]" or array. I.e. extending that type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文