4章 例外処理
・引数のないcatchブロックは使いべきではない。・階層が浅い所にいる関数でtry- catch or try - catch - finallyを行う。
・階層が深いところでは、リソースの解放とかが必要な場合に、try - finallyを行う。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// youtube
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
namespace YoutubeTest001
{
class Program
{
static void Main(string[] args)
{
string developerKey = "作成したデベロッパーキー";
YouTubeRequestSettings settings = new YouTubeRequestSettings("example app", developerKey);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/ADos_xW4_J0");
Video video = request.Retrieve
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// youtube
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
namespace YoutubeTest001
{
class Program
{
static void Main(string[] args)
{
}
}
}
①psi.RedirectStandardError = true; ②string error = p.StandardError.ReadToEnd(); ③Console.WriteLine(error);
class Program
{
static void Main(string[] args)
{
Kacho kousaku = new Kacho();
kousaku.Work();
Console.ReadLine();
}
}
class Kacho
{
// 部下を加える
Hira dameo = new Hira();
//(1)イベントハンドラ追加 (発行する人が定義する)
public event EventHandler DenwaEnd; // 電話が終わった
//(2)イベント実行用の関数を定義 (発行する人が定義する)
private void OnDenwaEnd(EventArgs e)
{
if (DenwaEnd != null)
{
DenwaEnd(this, e);
}
}
public Kacho()
{
//(6)だめおにイベントを受け取らせるための準備
dameo.Subscribe(this);
}
public void Work()
{
//課長仕事開始
for (int i = 0; i < 10; i++)
{
Console.WriteLine("課長電話中。");
System.Threading.Thread.Sleep(1000);
}
//(3)イベントを発生させる。
OnDenwaEnd(EventArgs.Empty);
}
}
class Hira
{
// (4)イベントを受け取る設定をする。
// 受け取るイベントと受け取ったときに呼ばれる関数をひもづける
public void Subscribe(Kacho kacho)
{
kacho.DenwaEnd += KachoDenwaEnd;
}
// (5)イベント発生時に実施する処理を定義
public void KachoDenwaEnd(object sender, EventArgs e)
{
Console.WriteLine("課長。例の件なのですが、AAAなので、BBBにします。よろしいでしょうか?");
}
}
delegate()
{
}
を
() =>
{
}
で置き換えるように使う。
static void Main(string[] args)
{
// 3人をお供に加える。
Dog dog = new Dog();
Monkey monkey = new Monkey();
Kiji kiji = new Kiji();
// 攻撃の作戦をたてる
Action[] attacks = new Action[4];
attacks[0] = dog.Attack;
attacks[1] = monkey.Attack;
//attacks[2] = delegate() // 匿名メソッドを
attacks[2] = () => // ラムダ式で書き換え
{
//ここで関数を直に定義している。
Console.WriteLine("ここで一休み");
};
attacks[3] = kiji.Attack;
// 攻撃する
for (int i = 0; i < attacks.Length; i++ )
{
attacks[i]();
}
Console.ReadLine();
}
() => {式}
(変数) => {式}
変数 => {式} // ()を省略
変数 => 式 // {}を省略
static void Main(string[] args)
{
// 3人をお供に加える。
Dog dog = new Dog();
Monkey monkey = new Monkey();
Kiji kiji = new Kiji();
// 攻撃の作戦をたてる
//Attack[] attacks = new Attack[3];
Action[] attacks = new Action[4];
attacks[0] = dog.Attack;
attacks[1] = monkey.Attack;
attacks[2] = delegate() //★匿名メソッド★
{
//ここで関数を直に定義している。
Console.WriteLine("ここで一休み");
};
attacks[3] = kiji.Attack;
// 攻撃する
for (int i = 0; i < attacks.Length; i++ )
{
attacks[i]();
}
Console.ReadLine();
}
delegate void Attack();という宣言が必要とのことだったけど、実は戻り値がvoidのようなデリゲートの関数は、.Net側で準備してあるのでわざわざ宣言しなくてもよいよというような機能。
namespace MomoTaro
{
class Program
{
static void Main(string[] args)
{
// 3人をお供に加える。
Dog dog = new Dog();
Monkey monkey = new Monkey();
Kiji kiji = new Kiji();
// 攻撃の作戦をたてる
//Attack[] attacks = new Attack[3]; // 不要になった
Action[] attacks = new Action[3];
attacks[0] = dog.Attack;
attacks[1] = monkey.Attack;
attacks[2] = kiji.Attack;
// 攻撃する
for (int i = 0; i < attacks.Length; i++ )
{
attacks[i]();
}
Console.ReadLine();
}
}
// delegate void Attack(); // 不要
class Dog
{
public void Attack()
{
Console.WriteLine("BowWow");
}
}
class Monkey
{
public void Attack()
{
Console.WriteLine("ウキー");
}
}
class Kiji
{
public void Attack()
{
Console.WriteLine("ケーン");
}
}
}
public delegate void Action();
と定義されていることがわかる。
delegate void Attack();そうすると、この例でいうとAttackを型として使える。 Attack型にはAttackと引数と戻り値が一致する関数がなんでも代入できる。この例でいうと引数が0で戻り値がvoidのものを代入できる。
class Program
{
static void Main(string[] args)
{
// 3人をお供に加える。
Dog dog = new Dog();
Monkey monkey = new Monkey();
Kiji kiji = new Kiji();
// 攻撃の作戦をたてる
Attack[] attacks = new Attack[3];
attacks[0] = dog.Attack;
attacks[1] = monkey.Attack;
attacks[2] = kiji.Attack;
// 攻撃する
for (int i = 0; i < attacks.Length; i++ )
{
attacks[i]();
}
Console.ReadLine();
}
}
delegate void Attack();
class Dog
{
public void Attack()
{
Console.WriteLine("BowWow");
}
}
class Monkey
{
public void Attack()
{
Console.WriteLine("ウキー");
}
}
class Kiji
{
public void Attack()
{
Console.WriteLine("ケーン");
}
}
class Cat
{
public void Cry()
{
Console.WriteLine("にゃお~");
}
}
class CatKing : Cat
{
public void Cry()
{
Console.WriteLine("にゃお~猫王だぞ~");
}
}
そうすると警告はでるが、コンパイルは通る。警告はnewを使用してください。というような内容。
class Program
{
static void Main(string[] args)
{
Cat tama = new CatKing();
tama.Cry();
Console.ReadLine();
}
}
class CatKing : Cat
{
public new void Cry()
{
Console.WriteLine("にゃお~猫王だぞ~");
}
}
class Cat
{
public void Cry()
{
Console.WriteLine("にゃお~");
}
}
結果は「にやお~」になる。
class Program
{
static void Main(string[] args)
{
Cat tama = new CatKing();
tama.Cry();
Console.ReadLine();
}
}
class CatKing : Cat
{
public override void Cry()
{
Console.WriteLine("にゃお~猫王だぞ~");
}
}
class Cat
{
public virtual void Cry()
{
Console.WriteLine("にゃお~");
}
}
結果は「にゃお~猫王だぞ~」になる。
private string myName;
public string MyName
{
get
{
return myName;
}
set
{
myName = value;
}
}
を、こんな感じで省略できるようになったこと。
public string MyName
{
get;
set;
}
C#コンパイラは、内部的には前者のようなコードを作ってるので、実装者からは見えないけど、プロパティに対応するフィールドが内部的には存在しているとのこと。
class Program
{
static void Main(string[] args)
{
cat tama = new cat("タマ");
tama.SayName();
Console.ReadKey();
}
}
class cat
{
//読み取り専用フィールド
private readonly string name;
public cat(string newName)
{
name = newName;
}
public void SayName()
{
//name = "にゃんこ"; // ここを有効にするとコンパイルエラーになる。
Console.WriteLine(name + "です。");
}
}
・クラスは値でなく参照型なので、代入しても実態は一つ。コピーされない。
cat tama = new cat("タマ");
tama.Age = 3;
cat tama2 = tama; // コピーはされない。tama2もtamaも同一。
tama2.Age = 4;
とすると、tamaもtama2も4歳になる。 struct position
{
public position(int x, int y, int z)
{
m_x = x;
m_y = y;
m_z = z;
}
public int m_x;
public int m_y;
public int m_z;
}
・なんだけどプロパティをもつ構造体の場合は、また制約があり、こういう風に書くとコンパイルエラーが発生する。デフォルトコンストラクタを呼ぶ前にメソッドを呼び出すとだめという制約がある。
struct position
{
public position(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
}
で、これに対応する場合はこうすればい。
struct position
{
public position(int x, int y, int z)
:this() // デフォルトコンストラクタを呼ぶ。
{
X = x;
Y = y;
Z = z;
}
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
}
・C#4からは省略可能な引数というのがある。引数の数だけことなるようなオーバーロードをするときの代替として使える。型がちがうオーバーロードの代わりにはならないので、オーバーロードがいらなくなるわけではないと思う。
・オブジェクト初期化子とは?
newするときにプロパティを設定できる機能。コンストラクタとの使い分けが難しそう。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace gridviewtest
{
///
/// MainWindow.xaml の相互作用ロジック
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
CollectionViewSource view = new CollectionViewSource();
ObservableCollection customers = new ObservableCollection();
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
int itemcount = 107;
for (int j = 0; j < itemcount; j++)
{
customers.Add(new Customer()
{
ID = j,
Name = "item" + j.ToString(),
Age = 10 + j
});
}
view.Source = customers;
this.DataGrid1.DataContext = view;
}
class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
}
これで見出しができる。ここまででこんな感じになる。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
///
/// MainWindow.xaml の相互作用ロジック
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
CollectionViewSource view = new CollectionViewSource();
ObservableCollection customers = new ObservableCollection();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
int itemcount = 107;
for (int j = 0; j < itemcount; j++)
{
customers.Add(new Customer()
{
ID = j,
Name = "item" + j.ToString(),
Age = 10 + j
});
}
view.Source = customers;
this.listView1.DataContext = view;
}
}
class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 192.168.XXX.XXX test.server.local server ← 追加書いたら保存する。
# localhost name resolution is handled within DNS itself. # 127.0.0.1 localhost # ::1 localhost 192.168.XXX.XXX test.server.local server fe80::XXXX:XXXX:XXXX:XXXX test.server.v6.local server2