DotNetBlog der DotNet-Blog Alles rund um Microsoft .NET – von  Sascha H. Baumann

Artikel mit ‘MCPD’ getagged

MCTS Examen 70-536 – Classes

Samstag, 26. September 2009

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 MyGeneric where 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:
    1. Create Delegate: public delegate void MyEventHandler(object sender, EventArgs e);
    2. Create event: public event MyEventHandler MyEvent;
    3. Fire Event
      if (MyEvent != null)
      {
      // Invoke Delegate
      MyEvent(this, new EventArgs());
      }
    4. 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:
    1. Add TypeForwardedTo attribute to source assembly
    2. Cut Typedefinition from source and paste to destination
    3. 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 ! :o )

    Next Topic will be “Type Conversion” .. .stay tuned !

MCTS Examen 70-536 – Common Reference Types

Montag, 21. September 2009

Hello everyone !


Hallo zusammen,

ein neuer Anlauf in Richtung Examen und die Entscheidung, die Blogsprache auf Englisch umzustellen, um schlichtweg mehr Leser anzusprechen. Ich hoffe, es stört nicht zu sehr :o )


During todays power-cut (don’t know what happened, just no electricity for half an hour) I decided to continue my journey toward Microsoft exam 70-735, the first step to become MCPD.

I will post a summary of any chapter I work through, therefore its not exactly for beginners, more like a little learning help for me and hopefully other people reading it. By the way, my language of choice is C#, so this is what you will read about.

(weiterlesen…)


Bad Behavior has blocked 35 access attempts in the last 7 days.