Techaray

Shailendra Saxena's Technical Blog

What is a Partial Classes

clock January 30, 2013 18:02 by author ShailendraSaxena

Partial Classes

 

A class can contain a number of methods, fields, and constructors. A highly functional class can become quite large. With C#, you can split the source code for a class into separate files so that you can organize the definition of a large class into smaller, easier to manage pieces. This feature is used by Microsoft Visual Studio 2010 for Windows Presentation Foundation (WPF) applications, where the source code that the developer can edit is maintained in a separate file from the code that is generated by Visual Studio whenever the layout of a form changes.

When you split a class across multiple files, you define the parts of the class by using the partial keyword in each file. For example, if the Circle class is split between two files called circ1.cs (containing the constructors) and circ2.cs (containing the methods and fields), the contents of circ1.cs look like this:

 

partial class Circle

{

public Circle() // default constructor

{

this.radius = 0;

}

public Circle(int initialRadius) // overloaded constructor

{

this.radius = initialRadius;

}

}

 

The contents of circ2.cs look like this:

 

partial class Circle

{

private int radius;

public double Area()

{

return Math.PI * this.radius * this.radius;

}

}

 

When you compile a class that has been split into separate files, you must provide all

the files to the compiler.

There are several rules to follow when working with partial class definitions:

·         All partial-type definitions meant to be parts of the same type must be modified with partial. For example, the following class declarations generate an error:

publicpartialclass A { }
//public class A { }  // Error, must also be marked partial
 

·         The partial modifier can only appear immediately before the keywords class, struct, or interface.

·         Nested partial types are allowed in partial-type definitions, for example:

partialclass ClassWithNestedClass
{
    partialclass NestedClass { }
}
 
partialclass ClassWithNestedClass
{
    partialclass NestedClass { }
}

·         All partial-type definitions meant to be parts of the same type must be defined in the same assembly and the same module (.exe or .dll file). Partial definitions cannot span multiple modules.

·         The class name and generic-type parameters must match on all partial-type definitions. Generic types can be partial. Each partial declaration must use the same parameter names in the same order.

·         The following keywords on a partial-type definition are optional, but if present on one partial-type definition, cannot conflict with the keywords specified on another partial definition for the same type:

o    public

o    private

o    protected

o    internal

o    abstract

o    sealed

o    base class

o    new modifier (nested parts)

o    generic constraints (For more information, see Constraints on Type Parameters (C# Programming Guide).)

Example 1

In the following example, the fields and the constructor of the class, “CoOrds”, are declared in one partial class definition, while the member, “PrintCoOrds”, is declared in another partial class definition.

publicpartialclass CoOrds
{
    privateint x;
    privateint y;
 
    public CoOrds(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}
 
publicpartialclass CoOrds
{
    publicvoid PrintCoOrds()
    {
        System.Console.WriteLine("CoOrds: {0},{1}", x, y);
    }
 
}
 
class TestCoOrds
{
    staticvoid Main()
    {
        CoOrds myCoOrds = new CoOrds(10, 15);
        myCoOrds.PrintCoOrds();
    }
}
 

Output

CoOrds: 10,15

Example 2

The following example shows that you can also develop partial structs and interfaces.

partialinterface ITest
{
    void Interface_Test();
}
 
partialinterface ITest
{
    void Interface_Test2();
}
 
partialstruct S1
{
    void Struct_Test() { }
}
 
partialstruct S1
{
    void Struct_Test2() { }
}


Delegates

clock December 14, 2012 03:21 by author ShailendraSaxena

 


A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates are often used to implement callbacks and event listeners. A delegate does not need to know anything about classes of methods it works with.

A delegate is a reference type. But instead of referring to an object, a delegate refers to a method.

Delegates are used in the following cases:

  • Event handlers
  • Callbacks
  • LINQ
  • Implementation of design patterns

There is nothing that is done with delegates that cannot be done with regular methods. Delegates are used, because they bring several advantages. They foster flexibility of the application and code reuse. Like interfaces, delegates let us decouple and generalize our code. Delegates also allow methods to be passed as parameters. When we need to decide which method to call at runtime, we use a delegate. Finally, delegates provide a way of specializing behavior of a class without sub classing it. Classes may have complex generic behavior, but are still meant to be specialized. Classes are specialized either through inheritance or via delegates.

Delegates are of two types

1.       Simple delegate


Delegate keyword is used to create delegate.
The delegate name can be anything but definition must be same as the signature of method which we want to point to. E.g. the following delegate can point to any method taking two integers and returning an integer.

public delegate int DelegateName(int x, int y);

 

 

 

 

 

 

2.       Multi Cast Delegate

A multicast delegate maintains a list of functions that will all be called when the delegate is invoked. If you want to add a method to the invocation list of a delegate object, you simply make use of the overloaded += operator, and if you want to remove a method from the invocation list you make use of the overloaded operator -=.



Abstract Class in .Net

clock December 7, 2012 03:04 by author ShailendraSaxena

 


Abstract class is a class which is declared with abstract keyword and it cannot be instantiated it can be inherited only.

Abstract class can have completed or uncompleted methods in it. If all the methods are uncompleted then it is similar to an interface.



Search

Calendar

<<  August 2017  >>
SuMoTuWeThFrSa
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archive

Tags

Categories


Visitors

FlagCounter

Flag Counter

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2017

Sign In