ジェネリック
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[+C#+言語セクション]]
#contents
* ジェネリッククラス [#yfbfd7cf]
ジェネリッククラスの定義[[VB.NET版>+VB.NET+ジェネリック]]
#codeprettify{{
class MyGenericClass1<T> where T : struct {
// 制約:Tは構造体
}
}}
#codeprettify{{
class MyGenericClass2<T> where T : class {
// 制約:Tはクラス
}
}}
#codeprettify{{
class MyGenericClass3<T> where T : new() {
// 制約:Tはインスタンス化可能
}
}}
#codeprettify{{
class MyGenericClass4<T> where T : MyOtherClass {
// 制約:TはMyOtherClassクラスを継承
}
}}
#codeprettify{{
class MyGenericClass5<T> where T : IMyInterface {
// 制約:TはIMyInterfaceインターフェイスを実装
}
}}
#codeprettify{{
class MyGenericClass6<T, U> where T : U {
// 制約:Tは別の型パラメータUを継承
}
}}
#codeprettify{{
class MyGenericClass7<T, F> : IDisposable
where T : MyOtherClass<F>, IDisposable, new()
where F : class
{
// 制約:T,Fは別々のクラス
}
}}
#codeprettify{{
class MyOtherClass {
// あるクラス
}
}}
#codeprettify{{
interface IMyInterface {
// あるインターフェイス
}
}}
* ジェネリックメソッド [#i9646ef7]
#codeprettify{{
void SwapIfGreater<T>(ref T lhs, ref T rhs) where T : Sys...
{
T temp;
if (lhs.CompareTo(rhs) > 0)
{
temp = lhs;
lhs = rhs;
rhs = temp;
}
}
}}
return
#codeprettify{{
public static T LoadFromXml<T>(string fileName, bool logg...
{
FileStream fs = null;
try
{
var serializer = new XmlSerializer(typeof(T));
fs = new FileStream(fileName, FileMode.Open, File...
//反序列化对象
return (T)serializer.Deserialize(fs);
}
catch (Exception e)
{
if (loggingEnabled)
{
//文件异常,写入日志
LogLoadFileException(fileName, e);
return null;
}
else
{
throw new Exception(e.Message);
}
}
finally
{
if (fs != null) fs.Close();
}
}
}}
#hr();
コメント:
#comment_kcaptcha
終了行:
[[+C#+言語セクション]]
#contents
* ジェネリッククラス [#yfbfd7cf]
ジェネリッククラスの定義[[VB.NET版>+VB.NET+ジェネリック]]
#codeprettify{{
class MyGenericClass1<T> where T : struct {
// 制約:Tは構造体
}
}}
#codeprettify{{
class MyGenericClass2<T> where T : class {
// 制約:Tはクラス
}
}}
#codeprettify{{
class MyGenericClass3<T> where T : new() {
// 制約:Tはインスタンス化可能
}
}}
#codeprettify{{
class MyGenericClass4<T> where T : MyOtherClass {
// 制約:TはMyOtherClassクラスを継承
}
}}
#codeprettify{{
class MyGenericClass5<T> where T : IMyInterface {
// 制約:TはIMyInterfaceインターフェイスを実装
}
}}
#codeprettify{{
class MyGenericClass6<T, U> where T : U {
// 制約:Tは別の型パラメータUを継承
}
}}
#codeprettify{{
class MyGenericClass7<T, F> : IDisposable
where T : MyOtherClass<F>, IDisposable, new()
where F : class
{
// 制約:T,Fは別々のクラス
}
}}
#codeprettify{{
class MyOtherClass {
// あるクラス
}
}}
#codeprettify{{
interface IMyInterface {
// あるインターフェイス
}
}}
* ジェネリックメソッド [#i9646ef7]
#codeprettify{{
void SwapIfGreater<T>(ref T lhs, ref T rhs) where T : Sys...
{
T temp;
if (lhs.CompareTo(rhs) > 0)
{
temp = lhs;
lhs = rhs;
rhs = temp;
}
}
}}
return
#codeprettify{{
public static T LoadFromXml<T>(string fileName, bool logg...
{
FileStream fs = null;
try
{
var serializer = new XmlSerializer(typeof(T));
fs = new FileStream(fileName, FileMode.Open, File...
//反序列化对象
return (T)serializer.Deserialize(fs);
}
catch (Exception e)
{
if (loggingEnabled)
{
//文件异常,写入日志
LogLoadFileException(fileName, e);
return null;
}
else
{
throw new Exception(e.Message);
}
}
finally
{
if (fs != null) fs.Close();
}
}
}}
#hr();
コメント:
#comment_kcaptcha
ページ名: