public class AddingNumbers { /// /// Method Overloading /// /// Example : Console.WriteLine() & System.Math.Sign() /// overloading a method when you for some reason need a couple of methods that take different perameters, /// but conceptually do the same thing. /// /// /// /// public int Add(int a, int b) { return a + b; } public int Add(int a, int b, int c) { return a + b + c; } // Error'OOPS_Overloading.Program.AddingNumbers' already defines a member called 'Add' with the same parameter types C:\Documents and Settings\milindm\My Documents\Visual Studio 2005\Projects\OOPS_Overloading\OOPS_Overloading\Program.cs 21 27 OOPS_Overloading /* public string Add(int a, int b, int c) { return Convert.ToString(a + b + c); }*/ }
Interface Example interface IPoint { // Property signatures: int x { get; set;} int y { get; set;} // method signature void print_points(); }
class Point : IPoint { // Fields: private int _x; private int _y; // Constructor: public Point(int x, int y) { _x = x; _y = y; } // Property Implementation: public int x { get { return _x; }
set { _x = value; } }
public int y { get { return _y; } set { _y = value; } } // Method Implementation public void print_points() { Console.WriteLine("{0} {1}", _x, _y); } }
class Program { /// /// Interface /// /// -An interface contains only the signatures of methods, properties, events or indexers. /// -An Interface can not be intiated /// -An Interface can be inherited /// -All the members are public /// -multiple inheritace is possible /// -An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members. /// -An interface cannot be instantiated directly. /// -Interfaces contain no implementation of methods. /// -Classes and structs can inherit from more than one interface. /// -An interface can itself inherit from multiple interfaces.
abstract class BaseClass // Abstract class { protected int _x = 100; protected int _y = 150; public abstract void AbstractMethod(); // Abstract method public abstract int X { get; } public abstract int Y { get; } }
class Program : BaseClass { /// /// Abstract Class /// /// Use the abstract modifier in a method or property declaration to indicate that the method /// or property does not contain implementation. /// Use the abstract modifier in a class declaration to indicate that a class is intended only /// to be a base class of other classes. /// The abstract modifier can be used with classes, methods, properties, indexers, and events. /// /// -An abstract class cannot be instantiated. /// -An abstract class class cannot be inherited. /// -An abstract class may contain abstract methods and accessors. /// -A non-abstract class derived from an abstract class must include actual implementations /// of all inherited abstract methods and accessors. /// /// Abstract methods /// /// -An abstract method is implicitly a virtual method. /// -Abstract method declarations are only permitted in abstract classes. /// -It is an error to use the static or virtual modifiers in an abstract method declaration. /// /// Abstract properties /// /// -Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax. /// -It is an error to use the abstract modifier on a static property. /// -An abstract inherited property can be overridden in a derived class by including a property declaration that uses /// the override modifier. /// /// /// An abstract class must provide implementation for all interface members. /// /*interface I { void M(); } abstract class C: I { public abstract void M(); } */
public override void AbstractMethod() { _x++; _y++; }
public override int X // overriding property { get { return _x + 10; } }
public override int Y // overriding property { get { return _y + 10; } }
static void Main(string[] args) { Program o = new Program(); o.AbstractMethod(); Console.WriteLine("x = {0}, y = {1}", o.X, o.Y); Console.ReadKey(); /*Output x = 111, y = 161*/
/// /// A static constructor is used to initialize any static data, or to perform a particular action that /// needs performed once only. It is called automatically before the first instance is created or any /// static members are referenced. /// /// -A static constructor does not take access modifiers or have parameters. /// -A static constructor is called automatically to initialize the class before the first instance is created /// or any static members are referenced. /// -A static constructor cannot be called directly. /// -The user has no control on when the static constructor is executed in the program. /// -A typical use of static constructors is when the class is using a log file and the constructor is used /// to write entries to this file. /// -Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor /// can call the LoadLibrary method. /// /// ///
class A { static A() { System.Console.WriteLine("The static constructor invoked."); } public static void Test() { System.Console.WriteLine("The Drive method invoked."); } }
static void Main(string[] args) { A.Test(); // OutOut // The static constructor invoked. // The Drive method invoked. }
/// /// Sealed Class /// /// You can Initiate the class /// You can not Inherit the class : class B : A { } /// Structs are implicitly sealed; therefore, they cannot be inherited. /// System.Threading.Thread /// ///
sealed class A { public A() { } public void Test1() { } public static void Test2() { }
} /*//Error : 'OOPS_Sealed.Program.B': cannot derive from sealed type 'OOPS_Sealed.Program.A' class B : A { }*/
static void Main(string[] args) { A obj = new A(); A.Test2(); }
/// /// Static Class /// /// You can not Initiate : A obj = new A(); /// You can not Inherited : class B : A { } /// All the members in Static class are static /// They cannot contain Instance Constructors : public A { } /// /// static class A { /* Error 'test': cannot declare instance members in a static class public void test() { } */ public static void test() { } } /*Error cannot derive from static class 'OOPS_Concept.Program.A' class B : A { } */ static void Main(string[] args) { /* Error 1 Cannot declare variable of static type 'OOPS_Concept.Program.A' Error 2 Cannot create an instance of the static class 'OOPS_Concept.Program.A' A obj = new A();*/ A.test(); }
I am Milind Mahajan, having 3+ years of experience using Microsoft Technologies. Hire me for Dotnet, SQL server requirement.
My Expertise includes ASP.NET (using C# & VB), C#.NET, Web services, WCF, WPF, WWF, LINQ, XML programming, ADO.NET, JavaScript. I am proficient in building web as well as windows applications too.