What is a constructor? Explain the New Keyword. What is a Private Constructor?
Constructor - A constructor is a function with the same name as that of the class. The Default Constructor of a class is without an argument. The default constructor ensures that every member data is initialized to a default value. Constructors provide a way for classes to initialize a state for their members. Note that constructors dont have a return type(not even void).
public SomeClass(){Console.Writeline("Vishal says, Default Constructor is called");}\public SomeClass(string str){Console.Writeline("Vishal says, Custom Constructor is called" + str);}
When a custom constructor is defined, the Default Constructor is not called. A constructor may also be overloaded.
New - This keyword may be used as a modifier and as an operator. When used as an operator, it creates an object on a heap to invoke constructors. When used an a modifier, it hides an inherited member from the base class member.
As an operator, it can be used to create an object and then to invoke the constructor of the class. See example below.
ExampleSomeClass objSomeClass = new SomeClass(); //Creating a class object and invoking its constructor
float amount = new float(); //Creating an object of the type, and invoking its constructor
As a modifier, it is used to explicitly hide a member from the base class. See example.
Examplepublic class MamaClass{public void SomeMethod() { ... }}
public class BabyClass : MamaClass{new public void SomeMethod() { .... }}
Private Constructor - When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class. They are usually used in classes that contain static members only. It is also used to create Singleton classes.
"Just a collection of random thoughts and articles but not plagiarized, I think so! "
Thursday, January 31, 2008
Abstract clas Vs Interface
What is the difference between abstract class and interface?
If a class is to serve the purpose of providing common fields and members to all subclasses, we create an Abstract class. For creating an abstract class, we make use of the abstract keyword. Such a class cannot be instantiated. Syntax below:
abstract public class Vehicle { }
Above, an abstract class named Vehicle has been defined. We may use the fields, properties and member functions defined within this abstract class to create child classes like Car, Truck, Bike etc. that inherit the features defined within the abstract class. To prevent directly creating an instance of the class Vehicle, we make use of the abstract keyword. To use the definitions defined in the abstract class, the child class inherits from the abstract class, and then instances of the Child class may be easily created.Further, we may define abstract methods within an abstract class (analogous to C++ pure virtual functions) when we wish to define a method that does not have any default implementation. Its then in the hands of the descendant class to provide the details of the method. There may be any number of abstract methods in an abstract class. We define an abstract method using the abstract keyword. If we do not use the abstract keyword, and use the virtual keyword instead, we may provide an implementation of the method that can be used by the child class, but this is not an abstract method.Remember, abstract class can have an abstract method, that does not have any implementation, for which we use the abstract keyword, OR the abstract class may have a virtual method, that can have an implementation, and can be overriden in the child class as well, using the override keyword. Read example below
Example: Abstract Class with Abstract methodnamespace Automobiles{ public abstract class Vehicle { public abstract void Speed() //No Implementation here, only definition }}
Example: Abstract Class with Virtual methodnamespace Automobiles{ public abstract class Vehicle { public virtual void Speed() //Can have an implementation, that may be overriden in child class { ... } }
Public class Car : Vehicle{Public override void Speed() //Here, we override whatever implementation is there in the abstract class { ... //Child class implementation of the method Speed() }}}
An Interface is a collection of semantically related abstract members. An interface expresses through the members it defines, the behaviors that a class needs to support. An interface is defined using the keyword interface. The members defined in an interface contain only definition, no implementation. The members of an interface are all public by default, any other access specifier cannot be used. See code below:
Public interface IVehicle //As a convention, an interface is prefixed by letter I{ Boolean HasFourWheels()}
Time to discuss the Difference between Abstract Class and Interface
1) A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.2) Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.3) Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.
REFERENCES
http://www.dotnetuncle.com/Difference/4_abstract_class_interface.aspx
If a class is to serve the purpose of providing common fields and members to all subclasses, we create an Abstract class. For creating an abstract class, we make use of the abstract keyword. Such a class cannot be instantiated. Syntax below:
abstract public class Vehicle { }
Above, an abstract class named Vehicle has been defined. We may use the fields, properties and member functions defined within this abstract class to create child classes like Car, Truck, Bike etc. that inherit the features defined within the abstract class. To prevent directly creating an instance of the class Vehicle, we make use of the abstract keyword. To use the definitions defined in the abstract class, the child class inherits from the abstract class, and then instances of the Child class may be easily created.Further, we may define abstract methods within an abstract class (analogous to C++ pure virtual functions) when we wish to define a method that does not have any default implementation. Its then in the hands of the descendant class to provide the details of the method. There may be any number of abstract methods in an abstract class. We define an abstract method using the abstract keyword. If we do not use the abstract keyword, and use the virtual keyword instead, we may provide an implementation of the method that can be used by the child class, but this is not an abstract method.Remember, abstract class can have an abstract method, that does not have any implementation, for which we use the abstract keyword, OR the abstract class may have a virtual method, that can have an implementation, and can be overriden in the child class as well, using the override keyword. Read example below
Example: Abstract Class with Abstract methodnamespace Automobiles{ public abstract class Vehicle { public abstract void Speed() //No Implementation here, only definition }}
Example: Abstract Class with Virtual methodnamespace Automobiles{ public abstract class Vehicle { public virtual void Speed() //Can have an implementation, that may be overriden in child class { ... } }
Public class Car : Vehicle{Public override void Speed() //Here, we override whatever implementation is there in the abstract class { ... //Child class implementation of the method Speed() }}}
An Interface is a collection of semantically related abstract members. An interface expresses through the members it defines, the behaviors that a class needs to support. An interface is defined using the keyword interface. The members defined in an interface contain only definition, no implementation. The members of an interface are all public by default, any other access specifier cannot be used. See code below:
Public interface IVehicle //As a convention, an interface is prefixed by letter I{ Boolean HasFourWheels()}
Time to discuss the Difference between Abstract Class and Interface
1) A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.2) Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.3) Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.
REFERENCES
http://www.dotnetuncle.com/Difference/4_abstract_class_interface.aspx
Sunday, January 27, 2008
Automatically connect to database
Code for enumerating Instances of SQL Server (ADO.NET)
The following console application retrieves information about all of the visible SQL Server instances and displays the information in the console window
using System.Data.Sql;
class Program
{
static void Main()
{
// Retrieve the enumerator instance and then the data.
SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();
// Display the contents of the table.
DisplayData(table);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static void DisplayData(System.Data.DataTable table)
{
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
Console.WriteLine("============================");
}
}
}
The following console application retrieves information about all of the visible SQL Server instances and displays the information in the console window
using System.Data.Sql;
class Program
{
static void Main()
{
// Retrieve the enumerator instance and then the data.
SqlDataSourceEnumerator instance =
SqlDataSourceEnumerator.Instance;
System.Data.DataTable table = instance.GetDataSources();
// Display the contents of the table.
DisplayData(table);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static void DisplayData(System.Data.DataTable table)
{
foreach (System.Data.DataRow row in table.Rows)
{
foreach (System.Data.DataColumn col in table.Columns)
{
Console.WriteLine("{0} = {1}", col.ColumnName, row[col]);
}
Console.WriteLine("============================");
}
}
}
Subscribe to:
Posts (Atom)