Monday, July 26, 2004

 

Indexers In C#



Indexers In C#


C# introduces a new concept known as Indexers
which are used for treating an object as an array. The indexers are
usually known as smart arrays in C# community. Defining a C# indexer is
much like defining properties. We can say that an indexer is a member
that enables an object to be indexed in the same way as an array.

 <modifier> <return type> this [argument list]

{
get
{
// Get codes goes here
}
set
{
// Set codes goes here
}
}


Where the modifier can be private, public,
protected or internal. The return type can be any valid C# types. The
'this' is a special keyword in C# to indicate the object of the current
class. The formal-argument-list specifies the parameters of the
indexer. The formal parameter list of an indexer corresponds to that of
a method, except that at least one parameter must be specified, and
that the ref and out parameter modifiers are not permitted.
Remember that indexers in C# must have at least one parameter. Other
wise the compiler will generate a compilation error.


The following program shows a C# indexer in action

// C#: INDEXER

// Author: miteshvmehta@gmail.com

using System;
using System.Collections;

class MyClass
{
private string []data = new string[5];
public string this [int index]
{
get
{
return data[index];
}
set
{
data[index] = value;
}
}
}


class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc[0] = "Rajesh";
mc[1] = "A3-126";
mc[2] = "Snehadara";
mc[3] = "Irla";
mc[4] = "Mumbai";
Console.WriteLine("{0},{1},{2},{3},{4}",mc[0],mc[1],mc[2],mc[3],mc[4]);
}
}


The indexers in C# can be overloaded just like
member functions. The formal parameter list of an indexer defines the
signature of the indexer. Specifically, the signature of an indexer
consists of the number and types of its formal parameters. The element
type is not part of an indexer's signature, nor is the names of the
formal parameters. The signature of an indexer must differ from the
signatures of all other indexers declared in the same class. C# do not
have the concept of static indexers. If we declare an indexer static,
the compiler will show a compilation time error.

Indexers & Inheritance


Just like any other class members, indexers
can also participate in inheritance. A base class indexer is inherited
to the derived class.

//C#: Indexer : Inheritance

//Author: miteshvmehta@gmail.com
using System;
class Base
{
public int this[int indxer]
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}

class Derived : Base
{

}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1[0] = 10;
Console.WriteLine(d1[0]);//Displays 'Base SET Base GET 10'
}
}


Indexers & Polymorphism


A Base class indexer can be polymorphicaly
overridden in a Derived class. But remember that the modifiers like
virtual, override etc are using at property level, not at accessor
level.

//C#: Indexer : Polymorphism

//Author: miteshvmehta@gmail.com

using System;

class Base
{
public virtual int this[int index]
{
get
{
Console.Write("Base GET");
return 10;
}
set
{
Console.Write("Base SET");
}
}
}

class Derived : Base
{
public override int this[int index]
{
get
{
Console.Write("Derived GET");
return 10;
}
set
{
Console.Write("Derived SET");
}
}
}

class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1[0]= 10;
Console.WriteLine(b1[0]);//Displays 'Derived SET Derived GET 10'
}
}


Abstract Indexers


An indexer inside a class can be declared as
abstract by using the keyword abstract. Remember that an abstract
indexer in a class carries no code at all. The get/set accessors are
simply represented with a semicolon. In the derived class we must
implement both set and get assessors.


If the abstract class contains only set accessor, we can implement only set in the derived class.


The following program shows an abstract indexer in action.

//C#: Indexer : Abstract

//Author: miteshvmehta@gmail.com

using System;

abstract class Abstract
{
public abstract int this[int index]
{
get;
set;
}
}

class Concrete : Abstract
{
public override int this[int index]
{
get
{
Console.Write(" GET");
return 10;
}
set
{
Console.Write(" SET");
}
}
}

class MyClient
{
public static void Main()
{
Concrete c1 = new Concrete();
c1[0] = 10;
Console.WriteLine(c1[0]);//Displays 'SET GET 10'
}
}




Indexers & Properties


1. An index is identified by it's signature. But a property is identified it's name.

2. An indexer is always an instance member, but a property can be static also.

3. An indexer is accessed through an element access. But a property is through a member access.


Conclusion


The indexer is one of the key concepts of C#
and are very interesting aspects of the language.



Friday, July 23, 2004

 

Seconds since the Unix epoch in C#

Please find the code below to convert currentdatetime to epoch time in c#

public long GetEpochTime()
{
DateTime dtCurTime = DateTime.Now;
DateTime dtEpochStartTime = Convert.ToDateTime("1/1/1970 8:00:00 AM");
TimeSpan ts = dtCurTime.Subtract(dtEpochStartTime);

long epochtime;
epochtime = ((((((ts.Days * 24) + ts.Hours) * 60) + ts.Minutes) * 60) + ts.Seconds);
return epochtime;
}


With Best Regards,

Mitesh Mehta
miteshvmehta@gmail.com

Thursday, July 22, 2004

 

Const vs. Readonly in C#








Const vs. readonly in C#

const (Constants) are variables whose values are set at compile time, either
by the programmer or by the compiler... It cannot be modified there after,
so if you need a field whose value is known at compile time and should not
be changed anytime later then you should declare that field as const...
Interesting note is that const fields are static by default and so you do
not need to instantiate a class to access them...

readonly (Read Only) are variables whose values are set at run time, but
only once... The value of such variables are set in the constructors and
cannot be modified there on... These variables are useful when you want the
value of the variable to be fixed but fixed to a value which is known only
at run time... See this is what C# offers... :-)

Now if you need a static variable whose value is known only at runtime then
make it static readonly... :-)


Mitesh Mehta



Thursday, July 08, 2004

 

Integration and Patterns online @ MSDN


Integration and Patterns online @ MSDN












Integrations and Patterns, a Patterns and Practices Group publication, is up on the MSDN site.

18 common (?) integration patterns and implementation examples.

Rgds,
Varad.



 

Static Members of a class









Static Members of a class

A static member of a class can be accessed without creating an instance of
the class... Everyone knows this right!! so what's next...??
A static member cannot be marked as abstract or virtual, trying to do so
will result into an error...
Inside a static member you cannot call any non-static member, trying to do
so will also result in an error...

regards,
Mitesh Mehta



This page is powered by Blogger. Isn't yours?