如何索引一个字符串数组 [英] How to index a string array

查看:141
本文介绍了如何索引一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#3.5的字符串数组:

I have a string array in C# 3.5:

string [] times = new[] {"08:00 am" , "10:00 am", "120"} ; 



我想创建索引,以时间:开始时间结束时间 ElapsedTime 所以,当我的代码:

I would like to create indexes to times: StartTime, EndTime , ElapsedTime so that when I code:

StartTime= "09:00 am" ; 
EndTime= "11:00 am" ; 



然后倍[0] 设置为09:00 AM

我可以创建3种方法:

private void StartTime(string time)
{    times[0] = time;  }
private void EndTime(string time)
{    times[1] = time;  }
private void ElapsedTime(string time)
{    times[2] = time;   }

和代码

StartTime("09:00");  



但有一个简单的办法做到这一点?

but is there a simpler way to do it?

推荐答案

你真的应该做的是什么创建一个新的类来做到这一点。使两个时间属性

What you should really do is create a new class to do this. Make the two times properties.

和经过的时间是在开始和结束时间的函数。

And the time elapsed is a function of the start and end times.

class Time 
{
    public DateTime StartTime{ get; set; }
    public DateTime EndTime{ get; set; }

    public String[] ToStringArray() 
    {
        String[] ret = new String[3];
        ret[0] = StartTime.ToString();
        ret[1] = EndTime.ToString();
        ret[2] = ElapsedTime().ToString();
        return ret;
    }

    public TimeSpan ElapsedTime() 
    {
        return EndTime.subtract(StartTime);
    }
}

这篇关于如何索引一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆