c# - 进行切换以针对数组中的项目进行操作

标签 c# arrays switch-statement

我正在使用authorize.net AIM,他们提供的示例代码打印响应值的有序列表。我不是将有序列表打印到客户可以看到所有这些信息的屏幕上,而是如何设置一个开关来访问数组的某些索引并根据为特定数组索引返回的文本执行某些操作?

String post_response;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
    post_response = responseStream.ReadToEnd();
    responseStream.Close();
}

// the response string is broken into an array
// The split character specified here must match the delimiting character specified above
Array response_array = post_response.Split('|');

// the results are output to the screen in the form of an html numbered list.
resultSpan.InnerHtml += "<OL> \n";
foreach (string value in response_array)
{
    resultSpan.InnerHtml += "<LI>" + value + "&nbsp;</LI> \n";
}
resultSpan.InnerHtml += "</OL> \n";

最佳答案

将response_array的类型更改为string[]:

string[] response_array = post_response.Split('|');

在 C# 中,您可以打开字符串:

switch (response_array[0])
{
    case "foo":
        // do something...
        break;
    case "bar":
        // do something else...
        break;
    default:
        // Error?
        break;
}

关于c# - 进行切换以针对数组中的项目进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3481263/

相关文章:

c# - 如何在 LINQ to Entities 查询中实现查询拦截? (C#)

c# - UWP - 绑定(bind)数据不起作用 - 空 ListView

java - 在一年中添加天数 - Java

c# - 列出从特定类/接口(interface)继承的所有类

c# - 以编程方式将应用程序池分配给iis7中的网站

javascript - 无法选择 Javascript 附加的 HTML 元素

java - 图书馆和图书类,构造函数

javascript - JavaScript 中的返回函数

C++ Switch 语句 - 给玩家另一个选择的机会

android - 我如何使用带微调器的开关?