Object Oriented Programming Features of C#

C#.NET derives the object oriented programming features from C++.

C++ (vs) C#
 In C++, the class definition syntax should end with ; whereas in C#, it’s not required.
 In C++, we have private section, public section and protected section separate
ly. But in C#, there no such kind of sections and the access modifier should be specified for each member of the class.  In C++, it is not possible to initialize the data members in the class def. But whereas in C#, it is possible.

List of other OOP languages
• C++
• Java
• VC++

OOP Development

i) Class Definition Syntax:

class classname
{
//Data members go here
//Methods go here
}

ii) Data Members Declaration Syntax:

accessmodifier functionalModifier datatype variablename;
     (or)
accessmodifier functionalModifier datatype variablename = value;

iii) Methods (Member Functions) Declaration Syntax:
accessmodifier functionalModifier returntype/void methodname(args)
{
//method body
}

iv) Access Modifiers

a) private: This is the default access modifier. The private member is accessible only inside of the self class. It won't be accessible in the other classes. In other words, it offers limited accessibility.
b) public: The public member is accessible inside of the self class and also in other classes. In other words, it offers un-limited accessibility.
c) protected: The protected member is accessible inside of the self class and also in relevant sub classes. It is meant for only "Inheritance".
v) Functional Modifiers

a) static: The static members are accessible without creating any object. Those can be accessible with the class name. Syn: classname.membername
b) readonly: This functional modifier can be used with data members only. The value of the readonly data members can’t be changed from the client code.

vi) Object (Instance) Construction Syntax:
classname objname = new classname();
   (or)
classname objname;
objname = new classname();

vii) Accessing Object Members:
obj.datamember;
obj.method();

OOP Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOPDemo
{
class MyClass
{
//data member
public string MyDataMember = "This is my sample data member.";
//method
public void MyMethod()
{
Console.WriteLine("This is my sample method.");
}
}
class Program
{
static void Main(string[] args)
{
//construct an object
MyClass mc = new MyClass();
//access data member
Console.WriteLine(mc.MyDataMember);
//access method
mc.MyMethod();
Console.Read();
}
}
}
C# OOPS

Demo on Functional Modifiers (static and readonly)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FunctionalModifiersDemo
{
class Sample
{
//read only data member
public readonly string CompanyName = "Wirpo";
//static data member
public static string Location = "Hyderabad";
//static method
public static void ShowAddress()
{
Console.WriteLine("Hi-Tech City, Madapur.");
}
}
class Program
{
static void Main(string[] args)
{
Sample s = new Sample();
Console.WriteLine(s.CompanyName);
//s.CompanyName = "TCS"; // is not allowed bcoz it is the readonly member.
Sample.ShowAddress();
Console.WriteLine(Sample.Location);
Console.Read();
}
}
}
C# OOPS