简体   繁体   English

C#委托字符串和双精度数组

[英]c# delegate array with string & double

I got a problem, I'm studying С# . 我遇到了问题,正在研究С# I'm begginer then. 那我开始

I have 1 abstract class Figure and 2 classes inherited: Circle and Rectangle 我有1个抽象类Figure和2个继承的类: CircleRectangle

On each class, I calculate the surface of each figure. 在每个班级,我都会计算每个图形的表面。

And I have to return SurfaceTotal = Circle.Surface() + Rectangle.Surface() 我必须返回SurfaceTotal = Circle.Surface() + Rectangle.Surface()

My problem is I need to have this surface with other string values in my array for display them in the console. 我的问题是我需要在数组中将此表面和其他字符串值一起显示在控制台中。

See some function to understand it: 看一些功能来理解它:

From Figure : Figure

 abstract public double Surface();

From Rectangle : Rectangle

public override double Surface()
  {
     double resultat = longueur * largeur;  
     return resultat;
  }

From Circle : 来自Circle

public override double Surface()
{
    double resultat = Math.PI * (rayon * rayon);
    return resultat; 
}

So I decide to use delegate for it, 所以我决定使用委托

public delegate string resultat();

and then I did this array : 然后我做了这个数组:

resultat[] res = new resultat[4];

            res[0] = new resultat(rectangle.Informations);
            res[1] = new resultat(cercle.Informations);
            res[2] = new resultat(rectangle.Surface);
            res[3] = new resultat(cercle.Surface);

And for the display : 对于显示:

        Console.WriteLine("Rectangle centré au point  : {0} \n", res[0]());
        Console.WriteLine("Cercle centré au point  : {0} \n", res[1]());
        Console.WriteLine("Surface total   : {0} \n", res[2]() + res[3]());

The problem is : in res[2] and res[3] I have double values, but in the other ones I have strings values. 问题是:在res[2]res[3]我有double值,但在其他参数中,我有strings值。

So like that, the values aren't added. 因此,不会添加值。

And I have to use 1 array, then I wanted added the 2 values before to insert in this array. 而且我必须使用1个数组,然后我想在插入该数组之前添加2个值。 But I don't know how... Do you have an idea guys ? 但是我不知道怎么...你们有想法吗? Thanks 谢谢

It's unclear why you are using delegates here at all. 目前还不清楚为什么在这里使用委托。 You could simply use a string array. 您可以简单地使用字符串数组。

string[] res = new string[4];
res[0] = rectangle.Informations();
res[1] = cercle.Informations();
res[2] = rectangle.Surface().ToString();
res[3] = cercle.Surface().ToString();

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

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