45
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyとC#の配列操作対応表

Last updated at Posted at 2014-03-20

C#の配列操作を勉強していて、rubyのあのmethodに対応するのってなんだろうと思って調べたときのメモ書きです

配列の用意

ruby
list = [1,2,3]
C#
List<int> list = new List<int>() {1, 2, 3};

配列の要素数を調べる

ruby
result = list.count
C#
var result = list.Count

各要素に処理をして配列を返す

ruby
result_list = list.map{|elem| elem * 10}
C#(Linq)
var resultList = list.Select(elem => elem * 10).ToList();

条件に一致する配列を返す

ruby
result_list = list.select{|elem| elem == 1}
C#(Linq)
var resultList = list.Where(elem => elem == 1).ToList();

各要素を使って何か処理する

ruby
list.each{|elem| puts elem * 10}
C#(Linq)
list.ForEach(elem => Console.WriteLine(elem * 10));

条件に一致した最初の要素を取得

ruby
result = list.find{|elem| elem == 2}
C#(Linq)
var result = list.FirstOrDefault(elem => elem == 2);

条件を満たす要素が配列内に含まれているか調べる

ruby
result = list.any?{|elem| elem == 1})
C#(Linq)
bool result = list.Any(elem => elem == 1);

ソートする

ruby
result = list.sort_by {|elem| -elem)
C#(Linq)
var resultList = list.OrderBy(elem => -elem).ToList();

畳み込み

ruby
result = list.reduce {|sum, elem| sum + elem)
C#(Linq)
var result = list.Aggregate ( (sum, elem) => sum + elem);

45
46
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?