5
5

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.

yieldを使ってみる

Last updated at Posted at 2013-03-02

正規化されていないDBからデータをひっぱってくるクラスがあるとする。

こんなやつ。

HogeTbl
public class HogeTbl
{
	public int colKey { get; set; }
	public string Hoge1 { get; set; }
	public string Hoge2 { get; set; }
	public string Hoge3 { get; set; }
	public string Hoge4 { get; set; }
	public string Hoge5 { get; set; }
	public string Hoge6 { get; set; }
	public string Hoge7 { get; set; }
	public string Hoge8 { get; set; }
	public string Hoge9 { get; set; }
	public string Hoge10 { get; set; }
	public string Hoge11 { get; set; }
	public string Hoge12 { get; set; }
	public string Hoge13 { get; set; }
	public string Hoge14 { get; set; }
	public string Hoge15 { get; set; }
	public string Hoge16 { get; set; }
}

Hoge1〜Hoge16に対して同じ処理を行いたい場合はIEnumertorを使えれば便利なのでyieldを使ってみる。

Fuga.cs
public class Fuga
{
	private void fuga(HogeTbl tbl)
	{
		foreach(String hoge in this.getHoge(tbl)
		{
			nya.nyan(hoge);
		}
	}
	private IEnumerator<String> getHoge(HogeTbl tbl)
	{
		if(tbl == null)
		{
			yield break;
		}
		yield return tbl.Hoge1;
		yield return tbl.Hoge2;
		yield return tbl.Hoge3;
		yield return tbl.Hoge4;
		yield return tbl.Hoge5;
		yield return tbl.Hoge6;
		yield return tbl.Hoge7;
		yield return tbl.Hoge8;
		yield return tbl.Hoge9;
		yield return tbl.Hoge10;
		yield return tbl.Hoge11;
		yield return tbl.Hoge12;
		yield return tbl.Hoge13;
		yield return tbl.Hoge14;
		yield return tbl.Hoge15;
		yield return tbl.Hoge16;
	}
}

各Hogeに対してどんな処理を行ってるかわかりやすいし、けっこういいんじゃないかなって思ってる。

yieldを使ってみたかったからってわけじゃないんだからねっ。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?