簡體   English   中英

C#我可以優化此代碼?

[英]c# can i optimize this code?

我在按鈕上有這個:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string s = "x12y04";

    //make new instance for MyMath class
    MyMath dRet01 = new MyMath();

    //use the doubleArrayXY (in MyMath class) to get doubble array back
    double[] retD = dRet01.doubleArrayXY(s);

    //use the calcResultFromDoubleArray (in MyMath class) to get result
    MyMath dRet02 = new MyMath();
    double result = dRet02.calcResultFromDoubleArray(retD[0], retD[1]);

    //DEBUG!
    /*
    string TEST1 = Convert.ToString(returnedDouble[0]);
    MessageBox.Show(TEST1);
    string TEST2 = Convert.ToString(returnedDouble[1]);
    MessageBox.Show(TEST2);
    string TEST3 = Convert.ToString(result);
    MessageBox.Show(TEST3);
   */


}

“ MyMath”類為:

public double[] doubleArrayXY(string inputValue)
{
    //in case there are upper case letters, set all to lower
    string inpLow = inputValue.ToLower();

    //split the string on the Y (so this tech should also work for x89232y329)
    //so this will create res[0] that is x89232 and an res[1] that is 329
    string[] res = inpLow.Split(new string[] { "y" }, StringSplitOptions.None);

    //in the first string that looks like x89232, remove the x
    string resx = res[0].Replace("x", null);

    //now get the x value to a double
    double x = double.Parse(resx);
    //now get the y valye to a double
    double y = double.Parse(res[1]);

    //return in a double array the x and then the y (x=double[0] and y=double[1])
    return new double[] {x,y};
}

public double calcResultFromDoubleArray(double one, double two)
{
    return (one * two);
}

現在,我知道類中的“ calcResultFromDoubleArray”這一部分目前是無用的,但是我想稍后再做一些額外的工作。

我最想知道的是在主代碼中制作新的dRet10,然后再制作dRet02。

我一開始以為我可以做這樣的事情:

double result = dRet01.calcResultFromDoubleArray(retD[0], retD[1]);

因此,在那種情況下,我不需要創建MyMath的新實例,但這是行不通的。

所以我需要為該類調用一個新實例(就像我一樣),還是可以用一種更優雅的方式來做到這一點? 我仍然對C#還是陌生的,所以我試圖學習如何以一種優美而優雅的方式進行編程,除了使之工作。

由於您的方法除了傳遞的參數外實際上並沒有使用任何其他狀態信息,因此它們可能應該是static因此您根本不必創建類的任何實例:

double[] retD = MyMath.DoubleArrayXY(s);
double result = MyMath.CalcResultFromDoubleArray(retD[0], retD[1]);

如果MyMath中的所有方法都是靜態的,則將該類本身聲明為靜態-就像System.Math類一樣,因此您根本無法創建實例。

您可以將calcResultFromDoubleArray方法calcResultFromDoubleArray static ,然后通過MyMath.calcResultFromDoubleArray(val1, val2)進行調用

在您的代碼中,實際上沒有必要創建MyMath類的實例。 您可以使方法靜態

public static double[] doubleArrayXY(string inputValue) { ... }
public static double calcResultFromDoubleArray(double one, double two) { ... }

像這樣叫他們

double[] retD = MyMath.doubleArrayXY(s);
double result = MyMath.calcResultFromDoubleArray(retD[0], retD[1]);

如果將方法設為靜態,則可以在主類中執行以下操作:

double result = MyMath.calcResultFromDoubleArray(MyMath.doubleArrayXY(s));

並將calcResultFromDoubleArray更改為采用數組而不是兩個值(如其標題所示)。

僅供參考,您還可以鏈接String操作,因為它們會這樣返回Strings

string[] res = inputValue.ToLower().Split(new string[] { "y" }, StringSplitOptions.None);

無需創建double xdouble y 將方法的最后一部分更改為:

return new double[] {double.Parse(resx), double.Parse(res[1]};

盡管諸如此類的更改(會有更多的更改,但經常會有),但性能的提升卻很小,但它們卻會有所提高(靜態部分最多- new的相對昂貴)。

但最重要的是,它們使代碼更具可讀性和優雅性。

在我看來,MyMath上的兩個方法可能(或可能應該)都是靜態的,因為它們在方法外完全不依賴任何內容。 數學庫之類的情況經常發生。 似乎其他人也這么說。

另外,最好創建一個類或結構來表示您的X / Y。 可能是不合適的,但如果它代表事物,那么您可能希望一個類也代表該事物。 例如,參見Point和PointF類。 我建議使用其中一種,但它們的精度與您使用的精度不一樣(並且您的X / Y可能不是點,因此可能不合適)

您說的那行也沒有用:

double result = dRet01.calcResultFromDoubleArray(retD[0], retD[1]);

這應該已經與所示的代碼一起使用。 您遇到什么錯誤? dRet01存在,因此它應該與創建新實例一樣工作。 應該是靜態的注釋最適用,但是如果您是C#的新手,我認為有必要指出這一點,這樣您就不會對可能和不可能建立任何錯誤的想法。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM