2016-11-15 2 views
0

int [] 또는 double [] 배열에서 indexOf를 사용하여 특정 값/객체의 색인을 찾을 수 있습니다. 그러나 dateTime [] 배열의 경우는 아닙니다. 특정 날짜의 인덱스를 날짜 배열로 찾는 방법은 무엇입니까? 내가 더 좋은 방법이가 할 수있는 인덱스dateTimeArray에서 특정 날짜의 색인 찾기

//Find the index of October 7,2016 
     DateTime dateTimeFindIndex = new DateTime(2016, 10, 7); 
     for (int i = 0; i < 10; i++) 
     { 
      if(dateTimeFindIndex==myDates[i]) 
      { 
       index = i; 
       break; 
      } 

     } 

을 찾으려고 여기에서

using System; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     DateTime[] myDates = new DateTime[10]; 
     int index = 0; 

     for(int i=0;i<10;i++) 
     {myDates[i] = new DateTime(2016, 10, i + 1);} 
    } 
} 
} 

?

답변

0

당신은 Array.IndexOf 사용할 수 있습니다

var ind = Array.IndexOf(myDates,dateTimeFindIndex);