- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有没有办法创建一个 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();
}
我希望能够传递这样的值,其中 someListToCreate
在 Method
中创建,而不是使用现有列表。
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/
我遇到了一个奇怪的问题。我的应用程序的 Release 版本似乎运行良好,但最近当我切换到 Debug 版本时,我在启动时立即遇到访问冲突。当释放分配的内存块时,就会发生访问冲突。所有这些都发生在静态
我在 C# 中偶然发现了这种奇怪的语法形式,并试图弄清楚它的含义以及如何使用它。网络上似乎没有关于此的任何文档。 object data = new { var1 = someValue, var2
我正在尝试使用浏览器的内置类型 CSSStyleDeclaration 以编程方式传递和修改样式(由于 .cssText 属性,这很方便)。 但是,new CSSStyleDeclaration()
我有现成的代码: internal bool firstAsSymbol(out Symbol s) { return (s = first as Symbol) !=
在新的 Eclipse 版本 2022-03 中,一些(但不是全部)java 项目在 Project Explorer View 中的外观发生了变化。尽管 Package Presentation 设
我正在尝试使用 FormData 通过获取 API 在 POST 请求中发送用户输入的数据。问题是,当我用我创建的表单创建一个新的 FormData 对象时,它一直在创建一个空对象——没有条目/键/值
我有一个用一些 intel-intrinsincs 编写的 C 代码。在我先用 avx 然后用 ssse3 标志编译后,我得到了两个完全不同的汇编代码。例如: AVX: vpunpckhbw %xm
最近,discord 为您自己的应用程序添加了对斜杠命令的支持。我通读了它的文档,并尝试搜索一些视频(但是该功能刚刚出现),但我不明白我实际上需要做什么才能使其正常工作。我正在使用 WebStorm(
我想使用 JRI 从 Java 调用 R。 我在 eclipse 下在主类中运行它: Rengine c = new Rengine(new String[] { "--vanilla" },
我正在使用新的 Place Autocomplete那是来自新的静态Google Places SDK 客户端库 (here)。所以它真的很容易使用,我刚得到this tutorial它按预期工作。
我刚刚更新到 flutter 版本 1.25.0-5.0.pre.92,我的代码中出现了很多与空安全相关的错误,这些错误以前运行良好。我没有以任何方式选择空安全,我所做的只是运行 flutter 升级
我已经使用 React Native 有一段时间了,但我想我会在网络上试用 React。所以我遵循了这个指南:https://reactjs.org/docs/create-a-new-react-a
周六早上在这里。尝试学习新的 Scala 编译器 dotty。 安装在我的 Mac 上使用 brew install lampepfl/brew/dotty 安装成功。我有版本 dotr -versi
我使用了谷歌地方的新依赖。单击自动完成 View 时应用程序崩溃。错误如下。, java.lang.NullPointerException: Place Fields must be set.
我关注了这个博客-> https://medium.com/@teyou21/training-your-object-detection-model-on-tensorflow-part-2-e9e
在哪里可以找到用于在此架构上进行组装的新寄存器的名称? 我指的是 X86 中的寄存器,如 EAX、ESP、EBX 等。但我希望它们是 64 位的。 我认为它们与我反汇编 C 代码时不同,我得到的是 r
新的服务总线库 Azure.Messaging.ServiceBus 使用 ServiceBusReceivedMessage 来接收消息 https://learn.microsoft.com/en
需要使用实时流媒体 channel 的实时编码类型在新的 Azure 门户中配置广告插入和石板图像。请帮忙解决这个问题,因为我找不到该功能。 最佳答案 此处描述了 Azure 媒体服务的广告插入选项
我正在使用新的 GitHub 操作,下面的工作流程的想法是在打开或同步 pr 时运行,它应该首先检查并安装依赖项,然后运行一些 yarn 脚本 name: PR to Master on: pul
我听说 DMD 2.058 中将有一个用于匿名函数的新语法,但我找不到任何相关信息。新语法是什么?旧语法是否会被弃用? 最佳答案 我相信它就像 C#'s . 以下内容是等效的: delegate(i,
我是一名优秀的程序员,十分优秀!