Upload
christopher-small
View
234
Download
0
Embed Size (px)
Citation preview
Microsoft .NET
Вторая лекция
Reference and value types
public class RefType{ }
public struct ValueType : IDisposable{
public int A, B;public ValueType(int a, int b) { A = a; B = b; }
public void Dispose(){ /* Do some work */ }
}
Встроенные типыValue types:
bool b; int i;byte by; uint ui; sbyte sby; long l; char c; ulong ul;decimal dec; short s;double d; ushort us;float f;
Reference types:object o; string str;
Enumspublic enum MyEnum{ MyValue1, MyValue2, MyValue3 }//...MyEnum val = MyEnum.MyValue1;//...switch (val){
case MyEnum.MyValue1: //...break;
case MyEnum.MyValue2: //...break;
}
Массивы
int[] arr = new int[4];
int[] arr2 = new int[] { 1, 2, 3 };
int[,] arr_2d = new int[,] { {1, 3}, {5, 7} };
int[][] arr_jagged = new int[3][];
for (int i = 0; i < arr_jagged.Length; ++i)
arr_jagged[i] = new int[i + 10];
Методы с переменным количеством параметров
public static int Max(params int[] numbers){
if (numbers.Length < 1)throw new ArgumentException();
int cur_max = numbers[0];foreach (int number in numbers)
cur_max = Math.Max(cur_max, number);return cur_max;
}// ...int i = Max(3, 5, 4, 234);int[] arr = new int[] { 2, 3, 6, 3, 98 };i = Max(arr);
Перегрузка операторовpublic struct Complex{
public double Re, Im;
public Complex(double re, double im){ Re = re; Im = im; }public Complex(double re) : this(re, 0){ }
public static Complex operator + (Complex l, Complex r){ return new Complex(l.Re + r.Re, l.Im + r.Im); }
public static implicit operator Complex(double re){ return new Complex(re); }
}
Использование перегруженных операторов
static void Main(string[] args)
{
Complex z1 = 0;
Complex z2 = z1 + z1.Re;
}
Generics
public class Wrapper<T>{
private readonly T _val;
public Wrapper(T val) { _val = val; }
public static implicit operator T(Wrapper<T> wrapper){ return wrapper._val; }public static implicit operator Wrapper<T> (T val){ return new Wrapper<T>(val); }
}
Generics
public T Max<T>(params T[] values)where T : IComparable<T>
{if (values.Length < 1)
throw new ArgumentException();T cur_max = values[0];foreach (T val in values)
if (cur_max.CompareTo(val) < 0) cur_max = val;
return cur_max;}
Оператор foreach
static void Main(string[] args)
{
foreach (string s in args)
Console.WriteLine(s);
}
IEnumerable interface
interface IEnumerable{
IEnumerator GetEnumerator();}
interface IEnumerator{
public bool MoveNext();public void Reset();public object Current { get; }
}
Delegatespublic class GDIContext { /* ... */ }
public class Window{
public delegate void OnPaintProc(GDIContext gdi);public OnPaintProc OnPaint;// ...
}
private static void DrawCircle(GDIContext gdi){ /* ... */ }
static void Main(string[] args){
Window wnd = new Window();wnd.OnPaint += DrawCircle;
}
Exceptionstry{
throw new Exception("Hello, world!!!"); }catch (Exception ex){
Console.WriteLine(ex.Message); }finally {
Console.WriteLine("Finally block executed."); }
IDisposable interfacepublic class Device : IDisposable{
public Device(){ /* ... */ }
public void Dispose(){
GC.SuppressFinalize(this);/* ... */
}
~Device(){ Dispose(); }
}
Оператор using
using (Device dev = new Device())
{
// ...
throw new Exception("Hello, world!!!");
}
Reflection• Assemblies
– MyClassLibrary.dll– MyExecutable.exe
• Types– MyClassLibrary.SomeNamespace.MyClass– MyClassLibrary.SomeNamespace.MyClass.Nested
• Methods, Properties, etc– MyClassLibrary.SomeNamespace.MyClass.DoWork()– MyClassLibrary.SomeNamespace.MyClass.SomeValue
Reflection
static void PrintMethods(Type type)
{
foreach(MethodInfo method in type.GetMethods())
{ Console.WriteLine(method); }
}
static void Main(string[] args)
{
PrintMethods(typeof(int));
}
Attributes
class EnumValueAttribute : Attribute{
private readonly string _valueName;
public EnumValueAttribute(string valueName){ _valueName = valueName; }
public string ValueName { get { return _valueName; } }
}
Attributespublic enum WindowMessage{
[EnumValue("Изменение размеров")] Resize,[EnumValue("Перерисовка")] Paint,[EnumValue("Нажатие кнопки")] KeyDown
}
[Serializable]public class MyClass{
[NonSerialized] protected int i;public double D;
}
Reflection.Emit
Возможность создавать во время выполнения новые
• Сборки• Типы• Методы• и т.д.
А также добавлять новые сущности к уже существующим