Monday, January 12, 2009

Object Oriented Programming Question from C#

What is the difference between struct and class in C#?

Structs vs classes in C#
Structs may seem similar to classes, but there are important differences that you should be aware of. First of all, classes are reference types and structs are value types. By using structs, you can create objects that behave like the built-in types and enjoy their benefits as well.

  • When you call the New operator on a class, it will be allocated on the heap. However, when you instantiate a struct, it gets created on the stack. This will yield performance gains. Also, you will not be dealing with references to an instance of a struct as you would with classes. You will be working directly with the struct instance. Because of this, when passing a struct to a method, it's passed by value instead of as a reference.
  • Structs can declare constructors, but they must take parameters. It is an error to declare a default (parameterless) constructor for a struct. Struct members cannot have initializers. A default constructor is always provided to initialize the struct members to their default values.

  • When you create a struct object using the New operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the New operator. If you do not use New, the fields will remain unassigned and the object cannot be used until all the fields are initialized.
  • There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class object. A struct can implement interfaces, and it does that exactly as classes do,
  • Structs are simple to use and can prove to be useful at times. Just keep in mind that they're created on the stack and that you're not dealing with references to them but dealing directly with them. Whenever you have a need for a type that will be used often and is mostly just a piece of data, structs might be a good option.

How do I call one constructor from another in C#?

You use : base (parameters) or : this (parameters) just before the actual code for the constructor, depending on whether you want to call a constructor in the base class or in this class.

What is the difference between indexers and properties in C#?

Comparison Between Properties and Indexers
Indexers are similar to properties. Except for the differences shown in the following , all of the rules defined for property accessors apply to indexer accessors as well.

Properties

Indexers

Identified by its name.

Identified by its signature.

Accessed through a simple name or a member access.

Accessed through an element access.

Can be a static or an instance member.

Must be an instance member.

A get accessor of a property has no parameters.

A get accessor of an indexer has the same formal parameter list as the indexer.

A set accessor of a property contains the implicit value parameter.

A set accessor of an indexer has the same formal parameter list as the indexer, in addition to the value parameter.

Which interface(s) must a class implement in order to support the foreach statement?

Required interface for foreach statement:
A class must implement the IEnumerable and IEnumeratorinterfaces to support the foreach statement.

Explain Abstract, Sealed, and Static Modifiers in C#.

C# provides many modifiers for use with types and type members. Of these, three can be used with classes: abstract, sealed and static.

abstract
Indicates that a class is to be used only as a base class for other classes. This means that you cannot create an instance of the class directly. Any class derived from it must implement all of its abstract methods and accessors. Despite its name, an abstract class can possess non-abstract methods and properties.

sealed
Specifies that a class cannot be inherited (used as a base class). Note that .NET does not permit a class to be both abstract and sealed.

static
Specifies that a class contains only static members (.NET 2.0).

What's the difference between override and new in C#?

This is all to do with polymorphism. When a virtual method is called on a reference, the actual type of the object that the reference refers to is used to decide which method implementation to use. When a method of a base class is overridden in a derived class, the version in the derived class is used, even if the calling code didn't "know" that the object was an instance of the derived class. For instance:

public class Base
{
    public virtual void SomeMethod()
    {
    }
}
 
public class Derived : Base
{
    public override void SomeMethod()
    {
    }
}
 
...
 
Base b = new Derived();
b.SomeMethod();
 
 

will end up calling Derived.SomeMethod if that overrides Base.SomeMethod.


Now, if you use the new keyword instead of override, the method in the derived class doesn't override the method in the base class, it merely hides it. In that case, code like this:

public class Base
{
    public virtual void SomeOtherMethod()
    {
    }
}
 
public class Derived : Base
{
    public new void SomeOtherMethod()
    {
    }
}
 
...
 
 
Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod();
d.SomeOtherMethod();
 
 

Will first call Base.SomeOtherMethod , then Derived.SomeOtherMethod . They're effectively two entirely separate methods which happen to have the same name, rather than the derived method overriding the base method.

If you don't specify either new or overrides, the resulting output is the same as if you specified new, but you'll also get a compiler warning (as you may not be aware that you're hiding a method in the base class method, or indeed you may have wanted to override it, and merely forgot to include the keyword)

Name two ways that you can prevent a class from being instantiated.

Ways to prevent a class from instantiated:

A class cannot be instantiated if it is abstract or if it has a private constructor.

Explain some features of interface in C#.

Comparison of interface with class (interface vs class):

  • An interface cannot inherit from a class.
  • An interface can inherit from multiple interfaces.
  • A class can inherit from multiple interfaces, but only one class.
  • Interface members must be methods, properties, events, or indexers.

  • All interface members must have public access (the default).
  • By convention, an interface name should begin with an uppercase I.

Can an abstract class have non-abstract methods?

An abstract class may contain both abstract and non-abstractmethods. But an interface can contain only abstract methods.
How do I use an alias for a namespace or class in C#?

Use the using directive to create an alias for a long namespace or class. You can then use it anywhere you normally would have used that class or namespace. The using alias has a scope within the namespace you declare it in.

Sample code:

// Namespace:
using act = System.Runtime.Remoting.Activation;
 
// Class:
using list = System.Collections.ArrayList;
...
list l = new list(); // Creates an ArrayList
act.UrlAttribute obj; // Equivalent to System.Runtime.Remoting.Activation.UrlAttribute obj


What type of class cannot be inherited?

A sealed class cannot be inherited. A sealed class is used primarily when the class contains static members. Note that a struct is implicitly sealed; so they cannot be inherited.

What keyword must a derived class use to replace a non-virtual inherited method?

new keyword is used to replace (not override) an inherited method with one of the same name.

No comments:

Post a Comment