using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Thread1
{
class TestThread
{
// THREAD_NUM個のスレッドを立てる。
// それぞれのスレッドの中でnumをROOP_NUM回インクリメントする。
static void Main()
{
const int ThreadNum = 20;
const int LoopNum = 20;
int num = 0;//複数のスレッドから同時にアクセスされる
var syncObject = new object();
Parallel.For(0, ThreadNum, i =>
{
for (int j = 0; j < LoopNum; j++)
{
//Monitorクラスを簡略化
lock (syncObject)
{
//クリティカルセクション
int tmp = num;
Thread.Sleep(1);
num = tmp + 1;
}
/*
Monitor.Enter(syncObject);//MonitorクラスのEnterでロックを取得
try
{
//クリティカルセクション
int tmp = num;
Thread.Sleep(1);
num = tmp + 1;
}
finally
{
//ロック解放
Monitor.Exit(syncObject);
}
*/
}
});
Console.Write("{0} (期待値{1})\n", num, ThreadNum * LoopNum);
Console.ReadLine();
}
}
//class TaskSample
//{
// static void Main(string[] args)
// {
// const int N = 3;
// //並列処理が行われる
// //idの箇所は、デリゲート。名前はなんでもよい。
// Parallel.For(0, N, id =>
// {
// Random rnd = new Random();
// for (int i = 0; i < 10; ++i)
// {
// // ランダムな間隔で処理を一時中断
// Thread.Sleep(rnd.Next(50, 100));
// Console.Write("{0} (ID: {1})\n", i, id);
// }
// });
// // 並行して動かしている処理がすべて終わるまで、自動的に待つ
// }
//}
}
2016年7月7日木曜日
オブジェクトを生成し、戻り値を返す
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReturnObject
{
class Customer
{
public int ID;
public string Name;
}
//構造体も返せるが、値コピーになるので、数が多いとコピーが多くなり、パフォーマンスでない
//struct Customer
//{
// public int ID;
// public string Name;
//}
class Program
{
private static Customer getData() {
return new Customer()
{
ID = 1,
Name = "Hiroshi"
};
}
static void Main(string[] args)
{
var dt = getData();
Console.WriteLine("Name={0}, ID={1}", dt.Name,dt.Name);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReturnObject
{
class Customer
{
public int ID;
public string Name;
}
//構造体も返せるが、値コピーになるので、数が多いとコピーが多くなり、パフォーマンスでない
//struct Customer
//{
// public int ID;
// public string Name;
//}
class Program
{
private static Customer getData() {
return new Customer()
{
ID = 1,
Name = "Hiroshi"
};
}
static void Main(string[] args)
{
var dt = getData();
Console.WriteLine("Name={0}, ID={1}", dt.Name,dt.Name);
}
}
}
デリゲートいろいろ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace definedLamda2
{
class Program
{
static void Main(string[] args)
{
Action<string> act = (x) => { Console.WriteLine(x); };
act("Hello!");
act("wowowow");
int i = 100;double d = 3.14159;
//Func<Σパラメータの型, 戻り値の型> 名前 =(Σパラメータ)=>{return 戻り値 }
//Func<int, double, double> func = (x, y) =>
//{
// return x * y;
//};
//{return ;}は省略できる
Func<int, double, double> func = (x, y) => ( x * y );
double ret = func(i, d);
Console.WriteLine(ret);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace definedLamda2
{
class Program
{
static void Main(string[] args)
{
Action<string> act = (x) => { Console.WriteLine(x); };
act("Hello!");
act("wowowow");
int i = 100;double d = 3.14159;
//Func<Σパラメータの型, 戻り値の型> 名前 =(Σパラメータ)=>{return 戻り値 }
//Func<int, double, double> func = (x, y) =>
//{
// return x * y;
//};
//{return ;}は省略できる
Func<int, double, double> func = (x, y) => ( x * y );
double ret = func(i, d);
Console.WriteLine(ret);
Console.ReadLine();
}
}
}
デリゲート書き方いろいろ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DefinedDelegate
{
class Program
{
////その1
//private static void Disp() {
// Console.WriteLine("Disp()");
//}
////定義済みdelegate
//private static void a(Action call) {
// call();
//}
//static void Main(string[] args)
//{
// a(Disp);
// Console.ReadLine();
//}
////その2
//private static void Disp()
//{
// Console.WriteLine("Disp()");
//}
//static void Main(string[] args) {
// Action act = Disp;
// act();
// Console.ReadLine();
//}
//その3
static void Main(string[] args) {
//デリゲートactに代入
Action act = () => { Console.WriteLine("Hello lamda Expression"); };
//act実行
act();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DefinedDelegate
{
class Program
{
////その1
//private static void Disp() {
// Console.WriteLine("Disp()");
//}
////定義済みdelegate
//private static void a(Action call) {
// call();
//}
//static void Main(string[] args)
//{
// a(Disp);
// Console.ReadLine();
//}
////その2
//private static void Disp()
//{
// Console.WriteLine("Disp()");
//}
//static void Main(string[] args) {
// Action act = Disp;
// act();
// Console.ReadLine();
//}
//その3
static void Main(string[] args) {
//デリゲートactに代入
Action act = () => { Console.WriteLine("Hello lamda Expression"); };
//act実行
act();
}
}
}
LINQ TO SQL SQLデバック
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace LINS_Samples
{
#if DEBUG
class OutputWriter : System.IO.TextWriter
{
public override System.Text.Encoding Encoding
{
get { return null; }
}
public override void WriteLine(string value)
{
// [出力]ウィンドウに表示
System.Diagnostics.Debug.WriteLine(value);
}
}
#endif
class Program
{
static void Main(string[] args)
{
var connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
Console.WriteLine("{0}", connectionString);
Console.ReadLine();
using (var db = new DataClasses1DataContext())
{
db.Log = Console.Out;//Oupput windowに表示する設定
var q = from orders in db.Orders
select new { orders.OrderDate,orders.ShipName };
var q2 = from orDetail in db.Order_Details
select new { orDetail.ProductID,orDetail.Quantity };
//Debug!
System.Diagnostics.Debug.WriteLine(q);
System.Diagnostics.Debug.WriteLine(q2);
Console.WriteLine(q);
Console.WriteLine(q2);
}
//http://blogs.bitlan.net/ito/?p=1474
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace LINS_Samples
{
#if DEBUG
class OutputWriter : System.IO.TextWriter
{
public override System.Text.Encoding Encoding
{
get { return null; }
}
public override void WriteLine(string value)
{
// [出力]ウィンドウに表示
System.Diagnostics.Debug.WriteLine(value);
}
}
#endif
class Program
{
static void Main(string[] args)
{
var connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
Console.WriteLine("{0}", connectionString);
Console.ReadLine();
using (var db = new DataClasses1DataContext())
{
db.Log = Console.Out;//Oupput windowに表示する設定
var q = from orders in db.Orders
select new { orders.OrderDate,orders.ShipName };
var q2 = from orDetail in db.Order_Details
select new { orDetail.ProductID,orDetail.Quantity };
//Debug!
System.Diagnostics.Debug.WriteLine(q);
System.Diagnostics.Debug.WriteLine(q2);
Console.WriteLine(q);
Console.WriteLine(q2);
}
//http://blogs.bitlan.net/ito/?p=1474
}
}
}
LINQ サンプル いろいろ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace LINQ_Query
{
class Fruit {
public string code;
public string name;
public int price;
}
class Program
{
static string[] getString(string[] str) {
return (from s in str where s.StartsWith("A") select s).ToArray();
}
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
//クエリ式バージョン
var query = from x in numbers
where x>= 2 && x <= 5
select x;
foreach (var o in query) {
Debug.WriteLine(o);
}
//ラムダ式バージョン
var lamdaQuery = numbers.Where((x) => x >= 3 && x <= 4);
foreach (var o in lamdaQuery)
{
Debug.WriteLine(o);//B
}
Debug.WriteLine("test");
Console.ReadLine();
string str = "ABC";
var qSkip = str.Skip(1);
foreach (var o in qSkip)
{
Debug.WriteLine(o);//BC
}
var qSkipTake = str.Skip(1).Take(1);
foreach (var o in qSkipTake)
{
Debug.WriteLine(o);//BC
}
Debug.WriteLine("test");
//Console.ReadLine();
string[] fruit = { "Orange", "Apple", "Grape"};
string[] result = getString(fruit);
foreach (var name in result) Debug.WriteLine(name);
int[] num = { 50, 200, 15, 43, 41, 333, };
int qCount = num.Select(x=>x).Count();
Debug.WriteLine(qCount);//6
int qSum = num.Select(x => x).Sum();
Debug.WriteLine(qSum);//682
var r = from n in num
where n >= 100
orderby n
select n ;
foreach (var n in r) Debug.WriteLine(n);
Debug.WriteLine("test");
var r2 = num.Where((n) => n >= 10).OrderBy((n) => n);
foreach (var n in r2) Debug.WriteLine(n);
Debug.WriteLine("test");
var r3 = from n in num
select n * 100 / 30;
foreach (var n in r3) Debug.WriteLine(n);
Debug.WriteLine("test");
var r4 = num.Select(n => n * 100 / 20);
foreach (var n in r4) Debug.WriteLine(n);
Debug.WriteLine("test");
Fruit[] f = {
new Fruit() { code="A100" , name ="Apple1", price=100},
new Fruit() { code="A200" , name ="Apple2", price=200},
new Fruit() { code="A300" , name ="Apple3", price=300},
new Fruit() { code="A400" , name ="Apple4", price=400},
};
var q = from x in f
select x;
foreach (var a in q) {
Debug.WriteLine(a);
Debug.WriteLine("code={0},name={1},price={2}", a.code, a.name, a.price);
};
//匿名オブジェクト
var lamda_q = f.Select((n) => new { N=n.name,P=n.price});
foreach (var a in lamda_q)
{
Console.WriteLine(a);
Console.WriteLine("name={0},price={1}", a.N, a.P);
};
Debug.WriteLine("test");
Console.ReadLine();
//匿名オブジェクト
var q2 = from n in f
select new { N = n.name, P =n.price};
foreach (var a in q2)
{
Debug.WriteLine(a);
Debug.WriteLine("name={0},price={1}", a.N, a.P);
};
Debug.WriteLine("test");
//Console.ReadLine();
}
}
}
退避でわかりやすい
クエリ式とラムダ式サンプル多し
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace LINQ_Query
{
class Fruit {
public string code;
public string name;
public int price;
}
class Program
{
static string[] getString(string[] str) {
return (from s in str where s.StartsWith("A") select s).ToArray();
}
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };
//クエリ式バージョン
var query = from x in numbers
where x>= 2 && x <= 5
select x;
foreach (var o in query) {
Debug.WriteLine(o);
}
//ラムダ式バージョン
var lamdaQuery = numbers.Where((x) => x >= 3 && x <= 4);
foreach (var o in lamdaQuery)
{
Debug.WriteLine(o);//B
}
Debug.WriteLine("test");
Console.ReadLine();
string str = "ABC";
var qSkip = str.Skip(1);
foreach (var o in qSkip)
{
Debug.WriteLine(o);//BC
}
var qSkipTake = str.Skip(1).Take(1);
foreach (var o in qSkipTake)
{
Debug.WriteLine(o);//BC
}
Debug.WriteLine("test");
//Console.ReadLine();
string[] fruit = { "Orange", "Apple", "Grape"};
string[] result = getString(fruit);
foreach (var name in result) Debug.WriteLine(name);
int[] num = { 50, 200, 15, 43, 41, 333, };
int qCount = num.Select(x=>x).Count();
Debug.WriteLine(qCount);//6
int qSum = num.Select(x => x).Sum();
Debug.WriteLine(qSum);//682
var r = from n in num
where n >= 100
orderby n
select n ;
foreach (var n in r) Debug.WriteLine(n);
Debug.WriteLine("test");
var r2 = num.Where((n) => n >= 10).OrderBy((n) => n);
foreach (var n in r2) Debug.WriteLine(n);
Debug.WriteLine("test");
var r3 = from n in num
select n * 100 / 30;
foreach (var n in r3) Debug.WriteLine(n);
Debug.WriteLine("test");
var r4 = num.Select(n => n * 100 / 20);
foreach (var n in r4) Debug.WriteLine(n);
Debug.WriteLine("test");
Fruit[] f = {
new Fruit() { code="A100" , name ="Apple1", price=100},
new Fruit() { code="A200" , name ="Apple2", price=200},
new Fruit() { code="A300" , name ="Apple3", price=300},
new Fruit() { code="A400" , name ="Apple4", price=400},
};
var q = from x in f
select x;
foreach (var a in q) {
Debug.WriteLine(a);
Debug.WriteLine("code={0},name={1},price={2}", a.code, a.name, a.price);
};
//匿名オブジェクト
var lamda_q = f.Select((n) => new { N=n.name,P=n.price});
foreach (var a in lamda_q)
{
Console.WriteLine(a);
Console.WriteLine("name={0},price={1}", a.N, a.P);
};
Debug.WriteLine("test");
Console.ReadLine();
//匿名オブジェクト
var q2 = from n in f
select new { N = n.name, P =n.price};
foreach (var a in q2)
{
Debug.WriteLine(a);
Debug.WriteLine("name={0},price={1}", a.N, a.P);
};
Debug.WriteLine("test");
//Console.ReadLine();
}
}
}
退避でわかりやすい
クエリ式とラムダ式サンプル多し
登録:
投稿 (Atom)