4.Classed, Methods and Properties in C#.

Classed, Methods and Properties in C#.


A class is the fundamental topic to be discussed in any Object-Oriented Programming (OOP) language. A class is a combination of related objects whereas each object is an instance or a copy of the corresponding class. 

Lets use a figure to give you a clear idea about the two words called class and object.(Figure 1 - Structure of a Class)

Figure 1 - Structure of a Class

Telephone is a combination of Sony, Ericcson, Panasonic, and Siemens. Hence, Telephone is a class and the other four are its objects. Here, Compaq and HP can't be objects of the class Telephone. In C#, a class is a user-defined reference type. 

 We create an object out of the classes by using the "new" keyword and by applying the general syntax as shown in Listing 1:
Listing 1
  1. Classname objectname = new Constructor();

To create an object for our class Telephone, you have to code as shown in Listing 2:
Listing 2
  1. Telephone T= new Telephone();

To access any variable by using the above object name, we use the dot operator as shown in Listing 3:
Listing 3
  1. using System;
  2. class Access
  3. {
  4. //Variables declared outside the main.
  5. int x = 100;
  6. int y = 200;
  7. public static void Main()
  8. {
  9. //Object created
  10. Access a = new Access();
  11.  
  12. //Instance variables called
  13. Console.WriteLine(a.x);
  14. Console.WriteLine(a.y);
  15. }
  16. }
In Listing 3, the variables are declared outside the main method. Hence, they are called Instance Variables. You have to create an object only for accessing an Instance variable. The additional memory created out of the object creation are automatically destroyed by the C# Garbage Collector.

Methods


Methods are blocks of code that perform some kind of action. There are two types of methods.
  • Instance Method
  • Static Method
 Instance Method
Instance Methods are methods declared outside the main method and can be accessed only by creating an object of the corresponding class.

  1. using System;
  2. class Instmethod
  3. {
  4. //Method declared outside the main.
  5. void show()
  6. {
  7. int x = 100;
  8. int y = 200;
  9. Console.WriteLine(x);
  10. Console.WriteLine(y);
  11. }
  12.  
  13. public static void Main()
  14. {
  15. //Object created
  16. Instmethod a = new Instmethod ();
  17.  
  18. //Instance method called
  19. a.show();
  20. }



Declaring Methods with Parameters


You can declare methods with a variable name or names as a parameter. The complete code illustration is show below.

  1. using System;
  2. class Pmethod
  3. {
  4. //Method declared with two parameters x and y of type integer
  5. void show(int x , int y)
  6. {
  7. Console.WriteLine(x);
  8. Console.WriteLine(y);
  9. }
  10.  
  11. public static void Main()
  12. {
  13. //Object created
  14. Pmethod a = new Pmethod ();
  15.  
  16. //Method called by passing two integer values
  17. a.show(200,250);
  18. }
  19. }

Method Overloading


You can declare the signature of the same method once again in the same class but with different parameters. The parameters should be different. If not, the C# compiler would show errors. 
The complete code illustration is show below.

  1. using System;
  2. class Overloadmethod
  3. {
  4.  
  5. //Method declared with one integer parameter
  6. void show(int x)
  7. {
  8. Console.WriteLine(x);
  9. }
  10.  
  11. //Method declared with two integer parameters
  12. void show(int a, int b)
  13. {
  14. Console.WriteLine(a);
  15. Console.WriteLine(b);
  16. }
  17.  
  18. public static void Main()
  19. {
  20. //Object created
  21. Overloadmethod a = new Overloadmethod ();
  22.  
  23. //Methods called by passing respective values
  24. a.show(100);
  25. a.show(300,500);
  26. }
  27. }

Properties


Properties provide added functionality to the .NET Framework.

A C# property consists of:
  • Field declaration
  • Accessor Methods (getter and setter methods)
Getter methods are used to retrieve the field's value and setter methods are used to modify the field's value. C# uses a special Value keyword to achieve this. 
The sample code illustration is show below.
  1. using System;
  2. class Propertiesexample
  3. {
  4.  
  5. //Field "idValue" declared
  6. public string idValue;
  7.  
  8. //Property declared
  9. public string IdValue
  10. {
  11.  
  12. get
  13. {
  14. return idValue;
  15. }
  16. set
  17. {
  18. idValue = value;
  19. }
  20. }
  21.  
  22. public static void Main(string[] args)
  23. {
  24.  
  25. Propertiesexample pe = new Propertiesexample();
  26. pe.IdValue = "009878";
  27. string p = pe.IdValue;
  28. Console.WriteLine("The Value is {0}",p);
  29. }
  30. }














Lesson 05-->                                                                 Home

No comments:

Post a Comment