簡體   English   中英

如何使這個簡單的C#代碼更高效/更優雅

[英]How to make this simple C# code more efficent/elegant

我有三個三個textBoxLectura1textBoxLectura2textBoxLectura2textBoxLectura3 當有人單擊按鈕button3我只想打印用戶編寫的條目。

我編寫了這段代碼,效果很好。 我想知道是否有一種更優雅/有效的方式來執行此操作而不使用太多的if語句。 我想維護arrayList結構。

    private void button3_Click(object sender, EventArgs e)
    {

        ArrayList myarray2 = new ArrayList();

        if (string.IsNullOrWhiteSpace(textBoxLectura1.Text) == false)
        {
            myarray2.Add(textBoxLectura1.Text);
        }

        if (string.IsNullOrWhiteSpace(textBoxLectura2.Text) == false)
        {
            myarray2.Add(textBoxLectura2.Text);
        }

        if (string.IsNullOrWhiteSpace(textBoxLectura3.Text) == false)
        {
            myarray2.Add(textBoxLectura3.Text);
        }

        if (myarray2.Count > 0)
        {
            foreach (string values in myarray2)
            {
                Console.WriteLine(values );
            }
        }
        else
        {
            MessageBox.Show("no entrys");
        }

    }

最簡單的方法可能是執行以下操作:

var strings = new List<string>() 
{
    textBoxLectura1.Text,
    textBoxLectura2.Text,
    textBoxLectura3.Text
};

var output = string.Join('\n',strings.Where(s => !string.IsNullOrEmpty(s));
if (!string.IsNullOrEmpty(output)) 
{
    Console.Writeline(output);
}
else
{
    MessageBox.Show("no entrys");
}

這里有幾點。 不要使用ArrayList 輸入List<T>幾乎在所有方面都更好。 沒有泛型時, ArrayList是C#的保留項。 使用ArrayList的唯一原因是,如果您正在處理一些需要它的舊版(2.0之前)代碼。 List<object>在功能上等效於ArrayList ,但是由於很多時候您要處理同類集合,因此使用List<T>會容易得多,其中T是您要使用集合填充此類型的任何類型(在此大小寫string ),並且不必每次嘗試從集合中獲取內容時都進行轉換。

我們在此處的代碼中所做的是創建一個List<string>並立即使用所有文本框.Text屬性的值對其進行初始化。 此時它們是否為空或為空並不重要。

然后,我們使用真正有用的string.Join方法。 這將收集字符串並將其與分隔符粘合在一起。 在這種情況下,我們使用換行符( \\n )。 但是,由於我們不想包含空值或空字符串(否則String.Join會插入額外的換行符),因此我們使用簡單的LINQ .Where語句僅選擇非空值或空字符串。

這是我的看法。

var notEmpty = new[]  { textBoxLectura1.Text, textBoxLectura2.Text, textBoxLectura3.Text}
    .Where(s => !String.IsNullOrEmpty(s)
    .ToArray();

if (!notEmpty.Any())
{
    MessageBox.Show("No Entries");
    return;
}

Console.WriteLine(string.Join(Environment.NewLine, notEmpty));

我也同意其他張貼者的觀點, ArrayListIEnumerable<T>實現容器是一件容易的事,您應該使用強類型的集合類型。

您可以使用功能來檢查並添加:。

private void button3_Click(object sender, EventArgs e)
        {

            ArrayList myarray2 = new ArrayList();

            ChecknAdd(textBoxLectura1, myarray2);
            ChecknAdd(textBoxLectura2, myarray2);
            ChecknAdd(textBoxLectura3, myarray2);

            if (myarray2.Count > 0)
            {
                foreach (string values in myarray2)
                {
                    Console.WriteLine(values);
                }
            }
            else
            {
                MessageBox.Show("no entrys");
            }

        }

        void ChecknAdd(TextBox txt, System.Collections.ArrayList Ary)
        {
            if (string.IsNullOrWhiteSpace(txt.Text) == false)
            {
                Ary.Add(txt.Text);
            }
        }

創建這樣的過程:

private void addOnArray(ArrayList array, String text) {

    if (string.IsNullOrWhiteSpace(text) == false)
    {
        array.Add(text);
    }

}

並在呼叫者中:

private void button3_Click(object sender, EventArgs e)
{

    ArrayList myarray2 = new ArrayList();
    addOnArray(myarray2, textBoxLectura1.Text);
    addOnArray(myarray2, textBoxLectura2.Text);
    addOnArray(myarray2, textBoxLectura3.Text);

    if (myarray2.Count > 0)
    {
        foreach (string values in myarray2)
        {
            Console.WriteLine(values );
        }
    }
    else
    {
        MessageBox.Show("no entrys");
    }

  }

您可以將if(s)縮短為三元運算符...添加到列表時,它將返回添加到的索引,否則只需將-1賦給接收變量

然后使用相同的三元邏輯來知道您是否要打印ArrayList的內容,還是只打印“無條目”

        ArrayList myarray2 = new ArrayList();

        int index = !string.IsNullOrWhiteSpace(textBoxLectura1.Text) ? myarray2.Add(textBoxLectura1.Text) : -1;
        index = !string.IsNullOrWhiteSpace(textBoxLectura2.Text) ? myarray2.Add(textBoxLectura2.Text) : -1;
        index = !string.IsNullOrWhiteSpace(textBoxLectura3.Text) ? myarray2.Add(textBoxLectura3.Text) : -1;

        Console.WriteLine(myarray2.Count > 0 ? string.Join("\n", myarray2.ToArray()) : "No Entries");

我真的很欣賞其他答案中的方法,以及它們中的幾種方法解釋了為什么要使用泛型與ArrayLists的方式。 這就是我的執行方式( 感謝SO的幫助 ):

IEnumerable<TextBox> textBoxCollection = this.Controls.OfType<TextBox>();

        foreach (TextBox box in textBoxCollection)
        {
            if (!string.IsNullOrWhiteSpace(box.Text))
            {
                MessageBox.Show(box.Text);   
            }
        }

暫無
暫無

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

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