目录
C# 特性(Attribute) C# 反射(Reflection) C# 属性(Property) C# 索引器(Indexer) C# 委托(Delegate) C# 事件(Event) C# 集合(Collection) C# 泛型(Generic) C# 匿名方法 C# 不安全代码 C# 多线程
1. C# 特性(Attribute)
[ AttributeUsage ( AttributeTargets. Class | AttributeTargets. Method, Inherited = false , AllowMultiple = true ) ]
public class MyCustomAttribute : Attribute
{ public string Name { get ; set ; } public MyCustomAttribute ( string name) { Name = name; }
}
[ MyCustomAttribute ( "Hello" ) ]
class Program
{ static void Main ( string [ ] args) { var attributes = typeof ( Program ) . GetCustomAttributes ( false ) ; foreach ( var attribute in attributes) { var customAttribute = ( MyCustomAttribute) attribute; Console. WriteLine ( $"Attribute Name: { customAttribute. Name } " ) ; } }
}
2. C# 反射(Reflection)
using System ;
using System. Reflection ; class ReflectionExample
{ public void Display ( ) { Console. WriteLine ( "Reflection Example" ) ; } static void Main ( string [ ] args) { Type type = typeof ( ReflectionExample ) ; object obj = Activator. CreateInstance ( type) ; MethodInfo methodInfo = type. GetMethod ( "Display" ) ; methodInfo. Invoke ( obj, null ) ; }
}
3. C# 属性(Property)
class PropertyExample
{ private string _name; public string Name{ get { return _name; } set { _name = value ; } } static void Main ( string [ ] args) { var obj = new PropertyExample ( ) ; obj. Name = "Kimi" ; Console. WriteLine ( obj. Name) ; }
}
4. C# 索引器(Indexer)
class IndexerExample
{ private int [ ] _array = new int [ 5 ] ; public int this [ int index] { get { return _array[ index] ; } set { _array[ index] = value ; } } static void Main ( string [ ] args) { var obj = new IndexerExample ( ) ; obj[ 0 ] = 10 ; Console. WriteLine ( obj[ 0 ] ) ; }
}
5. C# 委托(Delegate)
using System ; class DelegateExample
{ public delegate void DisplayDelegate ( string message) ; public void Display ( string message) { Console. WriteLine ( message) ; } static void Main ( string [ ] args) { var obj = new DelegateExample ( ) ; DelegateExample. DisplayDelegate del = obj. Display; del ( "Hello, Delegate!" ) ; }
}
6. C# 事件(Event)
using System ; class EventExample
{ public event EventHandler OnEventOccurred; protected virtual void OnEvent ( ) { OnEventOccurred?. Invoke ( this , EventArgs. Empty) ; } static void Main ( string [ ] args) { var obj = new EventExample ( ) ; obj. OnEventOccurred += ( sender, e) => { Console. WriteLine ( "Event occurred!" ) ; } ; obj. OnEvent ( ) ; }
}
7. C# 集合(Collection)
using System ;
using System. Collections. Generic ; class CollectionExample
{ static void Main ( string [ ] args) { List< int > list = new List< int > { 1 , 2 , 3 , 4 , 5 } ; list. Add ( 6 ) ; foreach ( var item in list) { Console. WriteLine ( item) ; } }
}
8. C# 泛型(Generic)
using System ;
using System. Collections. Generic ; class GenericExample< T>
{ private T _value; public T Value{ get { return _value; } set { _value = value ; } } static void Main ( string [ ] args) { var genericObj = new GenericExample< int > ( ) ; genericObj. Value = 10 ; Console. WriteLine ( genericObj. Value) ; }
}
9. C# 匿名方法
using System ; class AnonymousMethodExample
{ static void Main ( string [ ] args) { Action< string > action = delegate ( string message) { Console. WriteLine ( message) ; } ; action ( "Hello, Anonymous Method!" ) ; }
}
10. C# 不安全代码
using System ; class UnsafeCodeExample
{ static void Main ( ) { unsafe { int number = 0 ; int * p = & number; * p = 1234 ; Console. WriteLine ( number) ; } }
}
11. C# 多线程
using System ;
using System. Threading ; class ThreadExample
{ static void ThreadMethod ( ) { for ( int i = 0 ; i < 10 ; i++ ) { Console. WriteLine ( "Thread: " + i) ; Thread. Sleep ( 1000 ) ; } } static void Main ( string [ ] args) { Thread thread = new Thread ( new ThreadStart ( ThreadMethod) ) ; thread. Start ( ) ; for ( int i = 0 ; i < 5 ; i++ ) { Console. WriteLine ( "Main Thread: " + i) ; Thread. Sleep ( 500 ) ; } thread. Join ( ) ; }
}