Buy Electronics

Tuesday, July 13, 2010

Static in C#

C# – Static Members

The main features of a static class are:

* They only contain static members.
* They cannot be instantiated.
* They are sealed.
* They cannot contain Instance Constructors .
* We can hide a base class static method in a derived class by using the keyword "new".

A C# class can contain both static and non-static members.
When we declare a member with the help of the keyword "static", it becomes a static member.A static member belongs to the class rather than to the objects of the class.

Hence static members are also known as class members and non-static members
are known as 'instance members'.


In C#, data fields, member functions, properties and events can be declared
either as static or non-static.
There is no concept of static indexers, even though static properties are there.

a) Static Fields
-------------------
Static fields can be declared by using the keyword " static ".
When we declare a static field inside a class, it can be initialized with a value as shown in the following example.
All un-initialized static fields automatically get initialized to their default values(0) when the class is loaded first time.

Example :

using System;
class MyClass
{
public static int xx= 20;
public static int yy;
public static int zz = 40;

// values first come to constructor
public MyClass(int i)
{
xx = i; // assign xx = 50
}
}

class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1},{2}",MyClass.xx,MyClass.yy,MyClass.zz);
MyClass mc = new MyClass(50);
Console.WriteLine("{0},{1},{2}",MyClass.xx,MyClass.yy,MyClass.zz);
}
}

Output :
20,0,40
50,0,40

b) static constructor
--------------------------
The C# provides a special type of constructor known as static constructor to initialize the static data members when the class is loaded at first.

The name of a static constructor must be the name of the class and even they don't have any return type. The keyword "static" is used with the constructor.
The static constructor can't take any arguments. That means there is only one form of static constructor, without any arguments.
In other way it is not possible to overload a static constructor.

We can't use any access modifiers along with a static constructor.

Example :
// C# static constructor

using System;
class MyClass
{
public static int xx;
public static int yy = 500;

static MyClass ()
{
xx = 100;
yy = 200; // assign yy = 200 not 500
}
}
class MyClient
{
public static void Main()
{
Console.WriteLine("{0},{1}",MyClass.xx,MyClass.yy);
}
}

Output:
100,200

Note that static constructor is called when the class is loaded at the first time. They are called before an instance of the class is created, before a static member is called and before the static constructor of the derived class is called.

c) Static Member Functions
-----------------------------------
Inside a C# class, member functions can also be declared as static using "static" keyword.
But a static member function can access only other static members.
They can access non-static members only through an instance of the class.
We can invoke a static member only through the name of the class.
In C#, static members can't invoked through an object of the class as like in C++ or JAVA.

Example :
using System;
class MyClass
{
private static int x = 20;
private static int y = 40;
public static void Method()
{
Console.WriteLine("{0},{1}",x,y);
}
}
class MyClient
{
public static void Main()
{
MyClass.Method();
}
}

d) Static Properties
-------------------------
The properties can be declared as static in C#. The static properties are accessing using the class name.

Example :
using System;
class MyClass
{
public static int X
{
get
{
Console.Write("GET");
return 10;
}
set
{
Console.Write("SET");
}
}
}
class MyClient
{
public static void Main()
{
MyClass.X = 20; // calls setter displays SET
int val = MyClass.X;// calls getter displays GET
}
}

Output:
SET
GET

e) Static Members & Inheritance
------------------------------------------
A derived class can inherit a static member.

Example :
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("My Base static method");
}
}
class MyClass : MyBase
{
}
class MyClient
{
public static void Main()
{
MyClass.Method();
Console.WriteLine(MyClass.x);
}
}

Output :
My Base static method
25

A static member in C# can't be marked as override, virtual or abstract.
It is possible to hide a base class static method in a derived class
by using the keyword "new".

Example :
using System;
class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("My Base static method");
}
}
class MyClass : MyBase
{
public new static int x = 50;
public new static void Method()
{
Console.WriteLine(" My Derived static method");
}
}
class MyClient
{
public static void Main()
{
MyClass.Method();
Console.WriteLine(MyClass.x);
}
}

Output :
My Derived static method
50


Wednesday, July 7, 2010

Basics of WCF

Overview of WCF

-> Windows Communication Foundation.
-> It combines functionalities of Asp.Net Web services, remoting, MSMQ and
Enterprise Services.

-> It’s more reliable and secured then Web services.
-> All the major changes can be done in the configuration file instead of code file.

********************************************************
Differences between WCF & Webservices

Hosting
---------
WCF can be hosted on IIS, WAS(Windows Activation Service), self-hosting.
WS can be hosted on IIS.

Attribute
----------
WCF has [ServiceContract] attribute attached with the class.
WS has [WebService] attribute attached with the class.
WCF has [OperationContract] attribute attached with methods.
WS has [WebMethod] attribute attached with methods.

WCF has [DataContract] attribute attached with the class.
WCF has [DataMember] attribute attached for a properties in the class,.

XML
-----
WCF uses System.Runtime.Serialization namespace for serialization.
WS uses System.Xml.Serialization namespace for serialization.


Transports
------------
WCF supports Http, Tcp, Msmq, P2P transport protocols.
Ex: The sample address for above transport schema may look like
http://localhost:81/MyService
net.tcp://localhost:82/MyService
net.pipe://localhost/MyPipeService
net.msmq://localhost/private/MyMsMqService

WS supports Http protocol.

Security
----------
WCF provides reliable security for messaging and transactions over net.
WS doesn’t provide method level security & message delivery isn’t assured & can be lost without acknowledgment.

There are three major Points in WCF

i)Address
WCF service location that client can use to expose its features

ii)Binding
Configuration for Transport protocol, security and encoding mechanism.

iii)Contract
It defines type of data and type of operations, a client application can use from WCF service.

***********************************************************
WCF configurations in config file :
-------------------------------------------
It declares information about endpoint name, address, binding and contract.

< system.serviceModel >
< client >
< endpoint name = "MyEndpoint"
address = "http://localhost:8000/MyService/"
binding = "wsHttpBinding"
contract = "IMyContract"
/>
</ client >
< / system.serviceModel >


Endpoint :
------------
In WCF the relationship between Address, Contract and Binding is called Endpoint.
The Endpoint is the fusion of Address, Contract and Binding.

Contracts :
-------------
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does.

WCF defines four types of contracts.
1) Service contracts
Describe which operations the client can perform on the service.
There are two types of Service Contracts.
ServiceContract - This attribute is used to define the Interface.
OperationContract - This attribute is used to define the method inside Interface.

Ex:
[ServiceContract]
interface IMyContract
{
[OperationContract]
string MyMethod( );
}
class MyService : IMyContract
{
public string MyMethod( )
{
return "Hello World";
}
}

2) Data contracts
Define which data types are passed to and from the service.
WCF defines implicit contracts for built-in types such as int and string,
but we can easily define explicit opt-in data contracts for custom types.

There are two types of Data Contracts.
DataContract - attribute used to define the class
DataMember - attribute used to define the properties.

[DataContract]
class Contact
{
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
}
If DataMember attributes are not specified for a properties in the class, that
property can't be passed to-from web service.

3) Fault contracts
Define which errors are raised by the service, and how the service handles and
propagates errors to its clients.


4 )Message contracts
Allow the service to interact directly with messages.
Message contracts can be typed or untyped, and are useful in interoperability cases
and when there is an existing message format we have to comply with.







Mobiles