microsoft .net

21
Microsoft .NET Вторая лекция

Upload: kalkin

Post on 05-Jan-2016

26 views

Category:

Documents


1 download

DESCRIPTION

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 */ } }. Встроенные типы. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Microsoft .NET

Microsoft .NET

Вторая лекция

Page 2: 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 */ }

}

Page 3: Microsoft .NET

Встроенные типы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;

Page 4: Microsoft .NET

Enumspublic enum MyEnum{ MyValue1, MyValue2, MyValue3 }//...MyEnum val = MyEnum.MyValue1;//...switch (val){

case MyEnum.MyValue1: //...break;

case MyEnum.MyValue2: //...break;

}

Page 5: Microsoft .NET

Массивы

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];

Page 6: Microsoft .NET

Методы с переменным количеством параметров

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);

Page 7: Microsoft .NET

Перегрузка операторов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); }

}

Page 8: Microsoft .NET

Использование перегруженных операторов

static void Main(string[] args)

{

Complex z1 = 0;

Complex z2 = z1 + z1.Re;

}

Page 9: Microsoft .NET

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); }

}

Page 10: Microsoft .NET

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;}

Page 11: Microsoft .NET

Оператор foreach

static void Main(string[] args)

{

foreach (string s in args)

Console.WriteLine(s);

}

Page 12: Microsoft .NET

IEnumerable interface

interface IEnumerable{

IEnumerator GetEnumerator();}

interface IEnumerator{

public bool MoveNext();public void Reset();public object Current { get; }

}

Page 13: Microsoft .NET

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;

}

Page 14: Microsoft .NET

Exceptionstry{

throw new Exception("Hello, world!!!"); }catch (Exception ex){

Console.WriteLine(ex.Message); }finally {

Console.WriteLine("Finally block executed."); }

Page 15: Microsoft .NET

IDisposable interfacepublic class Device : IDisposable{

public Device(){ /* ... */ }

public void Dispose(){

GC.SuppressFinalize(this);/* ... */

}

~Device(){ Dispose(); }

}

Page 16: Microsoft .NET

Оператор using

using (Device dev = new Device())

{

// ...

throw new Exception("Hello, world!!!");

}

Page 17: Microsoft .NET

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

Page 18: Microsoft .NET

Reflection

static void PrintMethods(Type type)

{

foreach(MethodInfo method in type.GetMethods())

{ Console.WriteLine(method); }

}

static void Main(string[] args)

{

PrintMethods(typeof(int));

}

Page 19: Microsoft .NET

Attributes

class EnumValueAttribute : Attribute{

private readonly string _valueName;

public EnumValueAttribute(string valueName){ _valueName = valueName; }

public string ValueName { get { return _valueName; } }

}

Page 20: Microsoft .NET

Attributespublic enum WindowMessage{

[EnumValue("Изменение размеров")] Resize,[EnumValue("Перерисовка")] Paint,[EnumValue("Нажатие кнопки")] KeyDown

}

[Serializable]public class MyClass{

[NonSerialized] protected int i;public double D;

}

Page 21: Microsoft .NET

Reflection.Emit

Возможность создавать во время выполнения новые

• Сборки• Типы• Методы• и т.д.

А также добавлять новые сущности к уже существующим