Interface vs Abstract Class

Abstract classes
Abstract class is class which contain one or more abstract methods, which is implemented by sub classes. An abstract class can contain no abstract methods but also containe mehtod with body. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses.Abstract class can contain private as well as protected members.
When to use Abstract Class-If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality.

Interface is extremely useful when you don’t want a big hierarchical type framework. As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class which implements the said interface. The class must implement all of the methods described in the interface, or be an abstract class. An Interface can only have public members. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.Maintainability–if you want to add a new feature (method) in its contract, then you must implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.

When To use interface – Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.It is used where there of chances adding new method in future. Interfaces are more used to set standards. interface gave merely a specification,nothing implemented for any standalone project which can be changed at will its more design flexible and it can be utilized to model multiple inheritance.

Ref –

kick it on

Shout it

pimp it

Pin it

Creating a New Xml Document from Scratch in C#

To create a new XmlDocument, start by creating an XmlDocument object. The XmlDocument object contains CreateElement and CreateAttribute methods that are used to add nodes to the XmlDocument object. The XmlElement contains the Attributes property, which is an XmlAttribute-Collection. The XmlAttributeCollection inherits from the XmlNamedNodeMap class, which is a collection of names with corresponding values.
The following code shows how an XmlDocument can be created from scratch and saved to a file:

protected void Button1_Click(object sender, EventArgs e)
{ //Declare and create new XmlDocument XmlDocument xmlDoc = new XmlDocument();
XmlElement el;
int childCounter;
int grandChildCounter;
//Create the xml declaration first
xmlDoc.CreateXmlDeclaration(“1.0”, “utf-8”, null));
//Create the root node and append into doc
el = xmlDoc.CreateElement(“myRoot”);
//Child Loop
for (childCounter = 1; childCounter <= 4; childCounter++)
XmlElement childelmt;
XmlAttribute childattr;
//Create child with ID attribute
childelmt = xmlDoc.CreateElement(“myChild”);
childattr = xmlDoc.CreateAttribute(“ID”);
childattr.Value = childCounter.ToString();
//Append element into the root element
for (grandChildCounter = 1; grandChildCounter <= 3; grandChildCounter++)
//Create grandchildren childelmt.AppendChild(xmlDoc.CreateElement(“GrandChild”));}}
//Save to file

This code started by creating an instance of an XmlDocument. Next, the XML declaration is created and placed inside the child collection. An exception is thrown if this is not the first child of the XmlDocument. The following is the XML file that was produced by running the code sample:

<?xml version="1.0" encoding="utf-8"?>
<myChild ID="1">
<GrandChild />
<GrandChild />
<GrandChild />
<myChild ID="2">
<GrandChild />
<GrandChild />
<GrandChild />
<myChild ID="3">
<GrandChild />
<GrandChild />
<GrandChild />
<myChild ID="4">
<GrandChild />
<GrandChild /> <GrandChild /> </myChild></myRoot>

kick it on