gpt4 book ai didi

c# - 如何创建一个方法来创建一个新的列表并接受额外的参数

转载 作者:太空宇宙 更新时间:2023-11-03 19:12:40 25 4
gpt4 key购买 nike

有没有办法创建一个 Method 来创建一个 List 的新实例作为 Method Parameter 并用新的做一些事情列表?

    private void ApplyMaxValue(List<double> list, double myDouble, Border border) 
{
list = new List<double>();
list.Add(myDouble * 8);
border.Width = list.Max();
}

我希望能够传递这样的值,其中 someListToCreateMethod 中创建,而不是使用现有列表。

    ApplyMaxValue(someListToCreate, aDouble, myBorder);

我对 C# 还是有点陌生​​,不确定 Methods 是否能够做到这一点,或者我是否需要创建一个 Class

编辑

方法演示代码

<Window x:Class="NewListSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="525">
<Grid>
<StackPanel Margin="20">
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
<Border x:Name="borderForArray1" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFF57F7F" VerticalAlignment="Bottom"/>
<Border x:Name="borderForArray2" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FF6FA8D6" VerticalAlignment="Bottom"/>
<Border x:Name="borderForArray3" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFEEB382" VerticalAlignment="Bottom"/>
<Border x:Name="borderForArray4" BorderThickness="1" Height="1" HorizontalAlignment="Left" Width="20" Background="#FFB171E6" VerticalAlignment="Bottom"/>
</StackPanel>
<Button Content="Generate" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click"/>
<Button Content="Method" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click_1" />
<Button Content="Clear" HorizontalAlignment="Left" Padding="8,3" Margin="0,5,0,0" Click="Button_Click_2"/>
</StackPanel>
</Grid>
</Window>

CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace NewListSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();
}

string[] array1 = { "This is a word.", "This is a sentence.", "This is short.", "This is." };
string[] array2 = { "This is a word.", "This is a very very very very long sentence.", "This is the longest sentence in this array along with all other arrays in this sample.", "This is a string." };
string[] array3 = { "This.", "That.", "Those.", "These.", "Slightly longer string." };
string[] array4 = { "a.", "b.", "c.", "defg." };


//Prevent from writing this code
List<double> list1 = new List<double>();
List<double> list2 = new List<double>();
List<double> list3 = new List<double>();
List<double> list4 = new List<double>();
//Prevent code end


//Method to prevent writing longer code
private void ApplyMaxValue(string[] array, Border border)
{
List<double> someList = new List<double>();

foreach (string s in array)
{
someList.Add(s.Length * 3);
}

border.Height = someList.Max();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (string s in array1)
{
list1.Add(s.Length * 3);
}

foreach (string s in array2)
{
list2.Add(s.Length * 3);
}

foreach (string s in array3)
{
list3.Add(s.Length * 3);
}

foreach (string s in array4)
{
list4.Add(s.Length * 3);
}

borderForArray1.Height = list1.Max();
borderForArray2.Height = list2.Max();
borderForArray3.Height = list3.Max();
borderForArray4.Height = list4.Max();
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
ApplyMaxValue(array1, borderForArray1);
ApplyMaxValue(array2, borderForArray2);
ApplyMaxValue(array3, borderForArray3);
ApplyMaxValue(array4, borderForArray4);
}

private void Button_Click_2(object sender, RoutedEventArgs e)
{
borderForArray1.Height = borderForArray2.Height = borderForArray3.Height = borderForArray4.Height = 1;
}

}
}

最佳答案

听起来您应该返回列表引用,而不是将其用作参数 - 否则您在方法主体的第一条语句中为其分配了不同的值让它变得毫无意义:

private List<double> ApplyMaxValue(double myDouble, Border border) 
{
List<double> list = new List<double>();
list.Add(myDouble * 8);
border.Width = list.Max();
return list;
}

诚然,当唯一的元素是 myDouble 时,为什么您要在这里使用 list.Max() 对我来说并不明显...

如果你想接受一个列表作为方法参数,但你不需要创建一个新列表,你可以使用:

private void ApplyMaxValue(List<double> list, double myDouble, Border border) 
{
list.Add(myDouble * 8);
border.Width = list.Max();
}

仍然不完全清楚为什么您需要一种方法来做到这一点 - 感觉它并没有真正的责任。但它至少会起作用...

关于c# - 如何创建一个方法来创建一个新的列表并接受额外的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19478923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com