Você está na página 1de 6

1.

Static Static Classes do not contain instance member other than those from object and do not have a callable constructor A static method, field, property or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy is created and shared by all the objects 2. Partial Classes Split the definition of class or struct, an interface or method over two or more source files. Each source file contains a section of the type or method definition and all parts are combined when the application is compiled When working with automatically generated source code can be added to the class without having to recreate the source file. No access specifier can be used Namespace sam { Public partial class MyClass { Int a=10; }} 3. Virtual Methods Abstract method are implicitly virtual. To override function we have to make it virtual in parent class. 4. Virtual and overriding By Declaring a base class function as virtual you allow function to be overridden in any derived classes: Ex: Class MyBaseClass { Public virtual string VirtualMethod()

{ return } Property as virtual Member fields and static functions cannot be declared a virtual. Except Constructors other methods can be declared as virtual. 5. Following are the two - OUTPUT SAME But executed differently Method Hiding - New keyword And Method Overriding (better) - RUNTIME 6. Interfaces C# pure abstract class Looks like class but has no implementation Definition of events, Indexers, methods or properties, Interfaces inherited b classes and structs which must provide and implementation for each interface member defined. Declared with keyword interface Possible to implement any number if interfaces in a single derived class but you should provide signatures to all methods In C# No multiple inheritance not supported but using interfaces it can be done All methpds are abstract Constants Only static final constants, NO CODE just implementation, Abstract class Both instance and static constants are possible, complete default code or just details Where to use interface and where to use abstract class Adding functionality to track all implementation for the new method in interface where in abstract class no needed Named parameters 7. Sealed Class You cannot override the method or cannot inherit from that class.

Sealed class FinalClass { } Class child: Finalclass { //Error } Class MyClass : MyClassBase { Public sealed override void FinalMethod() { } } Class derivedclass: myclass { Public override void FinalMethod() } } In order to use the sealed keyword on a method or property It must be first been overridden from the base class. If you do not want a method or property in a base class overridden then dont mark it as virtual. ###Operator, Operator Overloading , Event and delegates ### 8. Operator and types 9. IS KEYWORD to check a type If(a is object) 10. AS Operator To perform explicit type conversion of reference type. IF the type are compatible with specified type, conversion is performed successfully if incompatible then operator returns null. a. Object ob1 =Some string; b. Object obj2 = 5;

c. String s1 =obj2 as string; // s1 = Some string d. String s2 = obj2 as string; //S2 = NULL 11. Operator Overloading - allows you to implement types that behave like the built in types when using operator. 12. Syntax for implementing operator is like static method a. Ex Operator Keyword operator symbol () {define } Myclass a = new myclass(10); Myclass b = new myclass(5); Myclass c; C=a+b; Error b. Working method by operator overloading

Delegate A delegate is type that defines a method signature. When you instantiate a delegate, you can associate its instance with any method with a compatible signature. You can invoke or call the method through the delegate instance. Delegates are used to pass methods as arguments to other methods. Delegates are used to apass methods as arguments to other methods Event handlers are nothing more than method that are invoked through delegates you create a custom method and a class such as a window control can call your method when a certain event occurs Any method from any accessible class or struct that matches the delegates signature which consists of the return type and parameters can be assigned to the delegate The method can be either static or and instance method This makes it possible to programmatically change method calls and also PLUG NEW CODE INTO EXISTING CLASSES As long as you know the signature of delegate you can assign your own method Why DELEGATE ?? Consider a business scenario where you need to make a business decision in your program to make a decision you need data. To get data you need to call a method. However the method name is not known at design time. It will work only at runtime In this case you need to pass the unknown method as a parameter. The method that you are passing in is known as call back function pointer to methods .NET Implements concept of unction pointers using delegates Where are delegate used ?

You dont know function name You can call 5 functions invocation list add remove according to requirements Events Mouse click Method need to be invoked by runtime when the event occurs method u define is passed as parameter to delegate. Delegate is An objectified function o Inherit from System.Delegate o Sealed implicitly o Looks much like C C++ style function pointer o Example Delegate int Func(ref int x) o Defines a new type : Func, takes int, return int o Delecared like a function

uSAGE pattern

Declared like function Instantiated like reference type Take method parameter in constructor Modified with +,-,*,/ o I can add more than one instance of a method o Removes the last instance of the method in the list Called like function Multicasting Multicast delegate is an object that is capable if calling any number of function Multicast delegates must contain only method that return void else there is a run time exception Anonymous functions C# events Class sends publisher Class receives subscriber IDE automatically- empty handler Base class only access event , to access from derived class access by protected o Delegate Name space

Você também pode gostar