■匿名類:匿名類用來表示臨時使用的只讀數據,所以必須在創建時初始化各字段的數據,并且只能讀取這些字段的值,而不能設置這些字段的值
如:var val=new{StrVal="a String",IntVal=12};
val.StrVal
val.IntVal
val.ToString()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AnonymousClass
{
class Program
{
static void Main(string[] args)
{
//定義匿名類
var val = new { StrVal = "HelloWorld", IntVal = 200 };
System.Console.WriteLine("first variable is: {0}; theSecondVariable is:{1}",val.StrVal,val.IntVal);
System.Console.WriteLine("val.ToString() is:{0}",val.ToString());
System.Console.ReadLine();
}
}
}
結果:
first variable is: HelloWorld; theSecondVariable is:200
val.ToString() is:{ StrVal = HelloWorld, IntVal = 200 }
posted on 2009-10-26 20:00
期待明天 閱讀(3194)
評論(0) 編輯 收藏 所屬分類:
CSharp