0
arunlalds's Avatar, Join Date: Mar 2010
Banned
The base class’s method or property must be declared virtual and
must be accessible from the derived class. The derived class will use the override
keyword.
Code:
public class Base
{
Int32 _x;
public virtual Int32 MyProperty
{
get
{
return _x;
}
}
public virtual void DoSomething()
{
_x = 13;
}
}
public class Derived : Base
{
public override Int32 MyProperty
{
get
{
return _x * 2;
}
}
public override void DoSomething()
{
_x = 14;
}
}
Base class references can refer to instances of the base class or any class derived from
it. For example, the following will print “28,” not “13.”
Code:
Base d = new Derived();
d.DoSomething();
Console.WriteLine(d.MyProperty().ToString());
You can also call base class functions from a derived class via the base keyword.
Code:
public class Base
{
public virtual void DoSomething()
{
Console.WriteLine(“Base.DoSomething”);
}
}
public class Derived
{
public virtual void DoSomething()
{
base.DoSomething();
Console.WriteLine(“Derived.DoSomething”);
}
}
Calling Derived.DoSomething will print out the following:
Code:
Base.DoSomething
Derived.DoSomething
Overriding Non-virtual Methods and Properties
You can still override it, but with a caveat: The override method will only be called through a reference to the derived class. To do this, use the new keyword (in a different context than you're probably used to).
Code:
class Base
{
public virtual void DoSomethingVirtual()
{
Console.WriteLine(“Base.DoSomethingVirtual”);
}
public void DoSomethingNonVirtual()
{
Console.WriteLine(“Base.DoSomethingNonVirtual”);
}
}
class Derived : Base
{
public override void DoSomethingVirtual()
{
Console.WriteLine(“Derived.DoSomethingVirtual”);
}
public new void DoSomethingNonVirtual()
{
Console.WriteLine(“Derived.DoSomethingNonVirtual”);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Derived via Base reference:”);
Base baseRef = new Derived();
baseRef.DoSomethingVirtual();
baseRef.DoSomethingNonVirtual();
Console.WriteLine();
Console.WriteLine(“Derived via Derived reference:”);
Derived derivedRef = new Derived();
derivedRef.DoSomethingVirtual();
derivedRef.DoSomethingNonVirtual();
}
}
Here is the output of this code:
Derived via Base reference:
Derived.DoSomethingVirtual
Base.DoSomethingNonVirtual
Derived via Derived reference:
Derived.DoSomethingVirtual
Derived.DoSomethingNonVirtual

Make sure you understand why the output is the way it is.
0
arunlalds's Avatar, Join Date: Mar 2010
Banned
Here’s a sample interface for some kind of playable object—perhaps an audio or video file, or even a generic stream. An interface does not specify what something is, but rather some behavior.
Code:
public interface IPlayable
{
void Play();
void Pause();
void Stop();
double CurrentTime { get; }
}
Note that interfaces can contain methods as well as properties. You do not specify access with interfaces’ members because, by definition, they are all public.
0
arunlalds's Avatar, Join Date: Mar 2010
Banned
A class can implement multiple interfaces, separated by commas:
Code:
public class AudioFile : IPlayable, IRecordable
{
...
}
However, you may run into instances where two (or more) interfaces define the same method. In our small example, suppose both IPlayable and IRecordable have a Stop() method defined. In this case, one interface must be made explicit.
Code:
public class AudioFile : IPlayable, IRecordable
{
void Stop()
{
//IPlayable interface
}
void IRecordable.Stop()
{
//IRecordable interface
}
}
Here is how to call these two methods:
Code:
AudioFile file = new AudioFile();
file.Stop();//calls the IPlayable version
((IRecordable)file).Stop();//calls the IRecordable version
Note that we arbitrarily decided that IRecordable’s Stop() method needed to be explicit—you could just have easily decided to make IPlayable’s Stop() method the explicit one.
0
arunlalds's Avatar, Join Date: Mar 2010
Banned
Unlike in C++, where structs and classes are functionally identical, in C#, there are
important and fundamental differences:
  1. Structs are value types as opposed to reference types, meaning that they exist on the stack. They have less memory overhead and are appropriate for small data structures. They also do not have to be declared with the new operator.
  2. Structs cannot be derived from. They are inherently sealed
  3. Structs may not have a parameterless constructor. This already exists implicitly and initializes all fields to zeros.
  4. All of a struct’s fields must be initialized in every constructor.
  5. Structs are passed by value, just like any other value-type, in method calls.
  6. Beware of large structs.
Solution: Defining a struct is similar to a class:
Code:
public struct Point
{
private Int32 _x;
private Int32 _y;
public Int32 X
{
get { return _x; }
set { _x = value; }
}
public Int32 Y
{
get { return _y; }
set { _y = value; }
}
public Point(int x, int y)
{
_x = x;
_y = y;
}
public Point() {} //Not allowed!
//Not allowed either! You're missing _y’s init
public Point(int x) { this._x = x; }
}
They can be used like so:
Code:
Point p;//allocates, but does not initialize
p.X = 13;//ok
p.Y = 14;
Point p2 = new Point();//initializes p2
int x = p2.X;//x will be zero
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
I have moved all your posts into single thread and into forum as we have strict guidelines for articles - http://www.go4expert.com/faq.php?faq=guidelines