Anda di halaman 1dari 4

II) 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#, its not
required.

In C++, we have private section, public section and protected section


separately. 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)

.NET 3.5 and Visual Studio 2008

Hour 14 - Page 1 of 4

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: client code. This functional modifier can be used with data members

only. The value of the readonly data members cant be changed from the

vi)

Object (Instance) Construction Syntax:


classname objname = new classname(); (or)

classname objname; objname = new classname();

.NET 3.5 and Visual Studio 2008

Hour 14 - Page 2 of 4

vii)

Accessing Object Members:


obj.datamember; obj.method();

Application 14: OOP Demo


using using using using System; System.Collections.Generic; System.Linq; 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(); } } } Output:

.NET 3.5 and Visual Studio 2008

Hour 14 - Page 3 of 4

Application 15: Demo on Functional Modifiers (static and readonly)


using using using using System; System.Collections.Generic; System.Linq; 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(); } } } Output:

.NET 3.5 and Visual Studio 2008

Hour 14 - Page 4 of 4

Anda mungkin juga menyukai