C#我有三个按钮,每个按钮都有相同的循环,但字符串不同。我可以将其缩短为一个循环吗?
我有三个按钮,每个按钮都有相同的循环,但字符串不同。我可以把它缩短为一个循环,这样我就不必重复使用循环了吗C#我有三个按钮,每个按钮都有相同的循环,但字符串不同。我可以将其缩短为一个循环吗?,c#,refactoring,C#,Refactoring,我有三个按钮,每个按钮都有相同的循环,但字符串不同。我可以把它缩短为一个循环,这样我就不必重复使用循环了吗 public static string VarOutput { get; set; } async private void btnCourse1_Click(object sender, RoutedEventArgs e) { string VarOutput = ""; string[] names = new string[3] { "COP3488C,",
public static string VarOutput { get; set; }
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string VarOutput = "";
string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
公共静态字符串VarOutput{get;set;}
异步私有void btnCourse1\u单击(对象发送方,路由目标)
{
字符串VarOutput=“”;
string[]names=新字符串[3]{“COP3488C”,“UWP1”,“本课程是移动应用程序开发。”};
for(int i=0;i
只需创建一个方法并使用适当的参数调用它,如下所示:
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
await WorkerAsync(names);
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await WorkerAsync(names);
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await WorkerAsync(names);
}
private async Task WorkerAsync(string[] names)
{
string VarOutput = "";
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse1\u单击(对象发送方,路由目标)
{
string[]names=新字符串[3]{“COP3488C”,“UWP1”,“本课程是移动应用程序开发。”};
等待工作同步(名称);
}
异步私有void btnCourse2\u单击(对象发送方,路由目标)
{
string[]names=新字符串[3]{“DOP3488B”,“UWC1”,“本课程是云计算。”};
等待工作同步(名称);
}
异步私有void btnCourse3\u单击(对象发送方,路由目标)
{
字符串[]名称=新字符串[3]{“BOP3589”,“UWP2”,“本课程计算机编程Java 1.”;
等待工作同步(名称);
}
专用异步任务WorkerAsync(字符串[]名称)
{
字符串VarOutput=“”;
for(int i=0;i
注意:代码未经测试。只需创建一个方法并使用适当的参数调用它,如下所示:
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "COP3488C,", "UWP1,", "This course is mobile app development." };
await WorkerAsync(names);
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await WorkerAsync(names);
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
string[] names = new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await WorkerAsync(names);
}
private async Task WorkerAsync(string[] names)
{
string VarOutput = "";
for (int i = 0; i < names.Length; i++)
{
VarOutput = VarOutput + names[i] + " ";
}
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
async private void btnCourse1\u单击(对象发送方,路由目标)
{
string[]names=新字符串[3]{“COP3488C”,“UWP1”,“本课程是移动应用程序开发。”};
等待工作同步(名称);
}
异步私有void btnCourse2\u单击(对象发送方,路由目标)
{
string[]names=新字符串[3]{“DOP3488B”,“UWC1”,“本课程是云计算。”};
等待工作同步(名称);
}
异步私有void btnCourse3\u单击(对象发送方,路由目标)
{
字符串[]名称=新字符串[3]{“BOP3589”,“UWP2”,“本课程计算机编程Java 1.”;
等待工作同步(名称);
}
专用异步任务WorkerAsync(字符串[]名称)
{
字符串VarOutput=“”;
for(int i=0;i
注意:代码未经测试。重构重复的代码以
private async Task displayCourseInfo(string[] names) {
//Replaced for loop with this line;
var message = String.Join(" ", names);
txtBoxCourse.Text = message;
var dialog = new MessageDialog(message);
await dialog.ShowAsync();
}
for循环只是构造一个带有空格的字符串,可以用字符串替换。Join
在事件处理程序中调用该方法
private async void btnCourse1_Click(object sender, RoutedEventArgs e) {
var names = new []{ "COP3488C,", "UWP1,", "This course is mobile app development." };
await displayCourseInfo(names);
}
private async void btnCourse2_Click(object sender, RoutedEventArgs e) {
var names = new []{ "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await displayCourseInfo(names);
}
private async void btnCourse3_Click(object sender, RoutedEventArgs e) {
var names = new []{ "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await displayCourseInfo(names);
}
重构重复的代码以
private async Task displayCourseInfo(string[] names) {
//Replaced for loop with this line;
var message = String.Join(" ", names);
txtBoxCourse.Text = message;
var dialog = new MessageDialog(message);
await dialog.ShowAsync();
}
for循环只是构造一个带有空格的字符串,可以用字符串替换。Join
在事件处理程序中调用该方法
private async void btnCourse1_Click(object sender, RoutedEventArgs e) {
var names = new []{ "COP3488C,", "UWP1,", "This course is mobile app development." };
await displayCourseInfo(names);
}
private async void btnCourse2_Click(object sender, RoutedEventArgs e) {
var names = new []{ "DOP3488B,", "UWC1,", "This course is Cloud Computing." };
await displayCourseInfo(names);
}
private async void btnCourse3_Click(object sender, RoutedEventArgs e) {
var names = new []{ "BOP3589,", "UWP2,", "This course Computer Programming Java 1." };
await displayCourseInfo(names);
}
我相信通过向string类添加扩展方法,您可以澄清代码,然后进行一些基本重构。这是一种方法
namespace ConsoleApplication2
{
public static class myExtensionMethods
{
public static string GetSubs(this string[] input)
{
string value = "";
input.Select(sub => value += $"{sub} ");
return value;
}
}
class Program
{
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[] { "COP3488C,", "UWP1,", "This course is mobile app development." });
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." });
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." });
}
private async Task ShowDialogAsync(string [] myStringArray)
{
string VarOutput = myStringArray.GetSubs();
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
我相信通过向string类添加扩展方法,您可以澄清代码,然后进行一些基本重构。这是一种方法
namespace ConsoleApplication2
{
public static class myExtensionMethods
{
public static string GetSubs(this string[] input)
{
string value = "";
input.Select(sub => value += $"{sub} ");
return value;
}
}
class Program
{
async private void btnCourse1_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[] { "COP3488C,", "UWP1,", "This course is mobile app development." });
}
async private void btnCourse2_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "DOP3488B,", "UWC1,", "This course is Cloud Computing." });
}
async private void btnCourse3_Click(object sender, RoutedEventArgs e)
{
await ShowDialogAsync(new string[3] { "BOP3589,", "UWP2,", "This course Computer Programming Java 1." });
}
private async Task ShowDialogAsync(string [] myStringArray)
{
string VarOutput = myStringArray.GetSubs();
txtBoxCourse.Text = VarOutput;
var dialog = new MessageDialog(VarOutput);
await dialog.ShowAsync();
}
我是否可以将其缩短为一个循环
yesBetter,然后将其替换为string VarOutput=string.Join(“,name”)
我能不能把它缩短为一个循环
yesBetter,然后用string VarOutput=string.Join(“,names)”替换它
您的工作者
方法需要异步并返回任务
。对它的调用需要等待它。(就此而言,它应该命名为WorkerAsync
)@joewite刚刚注意到最后一行您的Worker
方法需要异步并返回Task
。对它的调用需要等待它。(就此而言,它应该被命名为WorkerAsync
)@JoeWhite刚刚注意到最后一次投票是为了删除不必要的循环,最后一次投票是为了更改最终结果message@CamiloTerevinto在那些随意的否决票上安定下来,最糟糕的情况是,这些都是微不足道的。如果这个概念被传达,那么答案是正确的,尽管它没有遵循严格的风格指南。简单的评论就足够了。向下投票应该用于完全错误的答案或违反网站规则。我喜欢.Join方法。这很好地实现了。@gmiley不知道你在哪里看到了否决票,我只是根本没有投票。如果答案不正确,则会更改呈现给用户的最终消息user@CamiloTerevinto嗯,关于我对这个问题的回答,在你回答的同时,它立即被否决了,因为风格上的差异与我的回答的正确性完全没有关系。我决定删除m