MCTS Examen 70-536 – Classes
Hello everyone,
a new day, a new learning experience. Today I work on the “Classes” chapter of my exam-preperation and will share the most interesting parts with you.
Inheritance
- Inheritance and Interfaces provide a way to improve a systems consistency
- via Inheritance you can create new classes from existing once
- With inheritance there comes the possibility to use derived classes interchangeably. That is calles polymorphism. You can for example pass an object of type specialobject (Inheriting of object) to a method which excepts an object.
Interfaces
- Interfaces are contract defining required members the new class must provide. For example the IComparable Interface defines a member method called CompareTo. Every class that implements IComparable must implement this Method.
- Commonly used Interfaces: IComparable, IFormattable, IEuatable, ICloneable, IConvertible, IDisposable
- Example Interface:
public interface IMyInterface
{
bool DoSomething();
string Message {get; set; }
int MyNumber {get;set;}
}
- “Extract Interface” is a build in Refactoring-Method of Visual Studio
Partial Classes
- A Class-Definintion can be split into different .cs files. This must be declared by using “partial” keyword in class definition (ex. public partial class}
Generics
- With Generics you can define a type and leav some details unspecified. You dont have to specify the type of certain parameters or members, instead you can define them, when using the new type
- Generics will be a bigger part of the exam
- In System.Collections.Generic you can find some Generics like Dictionary, Queue, SortedDictionary or SortedList.
- Advantages: Reduced run-time-errors (More typesafe) and improve performance (avoids boxing and unboxing)
- Example of Definition:
class MyGeneric
{
public T t;
public U u;public MyGeneric(T _t, U _u)
{
t = _t;
u = _u;
}
}
- Important: Code needs to compile for every possible usage !
- Comsuming Example:
var test = new MyGeneric("hallo",12);
- Constraints for the Types used (T and U in the example above) can be defined.
class MyGenericwhere T : IDisposable
- You can use Interfaces, Baseclasses,Constructors and Reference of value types as constraints
Events
- object (Event senders) can trigger events when an action takes place (User clicks button, method completed etc.)
- Event Receivers can handle these events when they occure
- A Delegate is a reference (pointer) to a method that itself has no code.
- The Delegates signature must match the signature of the handling method
- Things to do, to create a custom event:
- Create Delegate: public delegate void MyEventHandler(object sender, EventArgs e);
- Create event: public event MyEventHandler MyEvent;
- Fire Event
if (MyEvent != null)
{
// Invoke Delegate
MyEvent(this, new EventArgs());
}
- You can create your own EventArgs. Just create a new Class, that inherits from EventArgs.
Attributes
- Attributes describe Methods or Properties. You can get this information by reflection.
- Commonly used attributes for: Security Privileges, capability (ex.Serialization), description (title, copyright…)
- Attribute Types derive from System.Attribute
- Example – AssemblyAttributes in AssemblyInfo.cs : [assembly: AssemblyTitle("title")]
- Declaring capabilities:
[Serializable]
class MyClass
{
}
Without this Attribute, a class is not serializable. - Declaring Security Requirements
[assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum, Read=@"C:\settings.ini")]
namespace MyNamespace
{
...
}
Type Forwarding
- Move a type from one assembly to another without needing recompile
- TypeForwardedTo – Attribute
- Example:
using System.Runtime.CompilerServices;
[assembly:TypeForwardedTo(typeof(TargetLib.MyType))]
- Works only for components referenced by existing applications
- To use Forwarding, do the following:
- Add TypeForwardedTo attribute to source assembly
- Cut Typedefinition from source and paste to destination
- Re-Compile both.
What have I learned ?
I havent used Attributes very often, so it was a good brush-up, and the Type-Forwarding Part was new for me, so I learned something today – Hurray !
)


