1 / 21

Microsoft .NET

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

kalkin
Download Presentation

Microsoft .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. Enums public enum MyEnum { MyValue1, MyValue2, MyValue3} //... MyEnum val = MyEnum.MyValue1; //... switch(val) { case MyEnum.MyValue1: //... break; case MyEnum.MyValue2: //... break; }

  5. Массивы 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];

  6. Методы с переменным количеством параметров 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);

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

  8. Использованиеперегруженных операторов static void Main(string[]args) { Complex z1 = 0; Complex z2 = z1 + z1.Re ; }

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

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

  11. Оператор foreach static void Main(string[]args) { foreach (string s in args) Console.WriteLine(s); }

  12. IEnumerable interface interface IEnumerable { IEnumerator GetEnumerator(); } interface IEnumerator { public bool MoveNext(); public void Reset(); public object Current { get; } }

  13. Delegates public 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; }

  14. Exceptions try { throw new Exception("Hello, world!!!"); } catch(Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Finally block executed."); }

  15. IDisposable interface public class Device : IDisposable { public Device() { /* ... */ } public void Dispose() { GC.SuppressFinalize(this); /* ... */ } ~Device() { Dispose(); } }

  16. Оператор using using (Device dev = new Device()) { // ... throw new Exception("Hello, world!!!"); }

  17. 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

  18. Reflection static void PrintMethods(Type type) { foreach(MethodInfo method in type.GetMethods()) { Console.WriteLine(method); } } static void Main(string[]args) { PrintMethods(typeof(int)); }

  19. Attributes class EnumValueAttribute : Attribute { private readonly string _valueName; public EnumValueAttribute(string valueName) { _valueName = valueName; } public string ValueName { get { return _valueName; } } }

  20. Attributes public enum WindowMessage { [EnumValue("Изменение размеров")]Resize, [EnumValue("Перерисовка")]Paint, [EnumValue("Нажатие кнопки")]KeyDown } [Serializable] public class MyClass { [NonSerialized] protected int i; public double D; }

  21. Reflection.Emit Возможность создавать во время выполнения новые • Сборки • Типы • Методы • и т.д. А также добавлять новые сущности к уже существующим

More Related