Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Lifecycle of an Object

Similar presentations


Presentation on theme: "The Lifecycle of an Object"— Presentation transcript:

1 The Lifecycle of an Object

2 The lifecycle includes two important stages: Construction and Destruction.
When an object is first instantiated it needs to be initialized. This initialization is known as construction and is carried out by a constructor function.

3 Basic initialization of an object is automatic.
Table . Primitive types and their default values Default value Type numeric (int, long, etc.) false bool '\0' (null) char enum null reference

4 EX: Time object1=new Time ();
All objects have a default constructor, which is a parameterless method with the same name as the class itself. EX: Time object1=new Time (); In addition, a class definition might include several constructor methods with parameters, known as nondefault constructors, that uses a parameter at instantiation: Time object1=new Time(CurrentTime);

5 Construction EX1: using System; class xxx {
public int x; // int x; or private int x; public int y; public xxx(int p1, int p2) x = p1; y = p2; } static void Main() xxx mC = new xxx(1, 2); Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);

6 EX2: using System; public class Test { class xxx public int x; // int x public int y; public xxx(int p1, int p2) x = p1; y = p2; } static void Main() xxx mC = new xxx(11, 22); Console.WriteLine("x = {0}, y = {1}", mC.x, mC.y);

7 EX3 using System; class Circle { const double PI = ; public Circle() // default constructor radius = 5; } public double Area() // return * radius * radius; return PI * radius * radius; private double radius; static void Main() Circle c; c = new Circle(); double areaOfCircle = c.Area(); Console.WriteLine(areaOfCircle);

8 Methods The syntax of a method is as follows:
access-modifiers returnType methodName ( parameterList )      // method body statements   }

9 EX.1: public void DisplayCurrentTime( ) { System.Console.WriteLine( "{0}/{1}/{2} {3}:{4}:{5}", Month, Date, Year, Hour, Minute, Second ); } EX.2: double Area()      {         return   * radius * radius;      }

10 The syntax of a method call is as follows:
objectname.methodName ( argumentList ) EX.1: Time t = new Time( ); t.DisplayCurrentTime( );

11 Calling the method Using amethode name by itself to call method of the same class. Using avariable that contains arefrence to an object followed by (.) & method name to call non-static method. Using the class name followed by (.) & method name to call static method.

12 Passing Parameters Parameter Types
C# allows four types of parameters in a parameter list: • Input parameters • Output parameters • Reference parameters • Parameter arrays

13 Input parameters are passed by value into methods
Input parameters are passed by value into methods. It is not allowed to change the value supplied by the caller. Output parameters are parameters whose values are not set when the method is called. The out keyword must precede the parameter's type in the parameter list. Reference parameters are passed by reference into methods. The ref keyword must precede the parameter's type in the parameter list. Parameter arrays enable you to specify that your method accept a variable number of arguments. using the C# keyword params,

14 ref class RefExample { void Method(ref int i) i = 44; }
static void Main() RefExample ob=new RefExample(); int val = 0; ob.Method(ref val); Console.WriteLine(val); // val is now 44

15 out class OutExample { void Method(out int i) i = 44; }
static void Main() OutExample ob=new OutExample(); int value; ob.Method(out value); // value is now 44

16 class CS0663_Example { // compiler error CS0663: "cannot define overloaded // methods that differ only on ref and out" public void SampleMethod(ref int i) { } public void SampleMethod(out int i) { } }

17 Example: method to return multiple values
class OutReturnExample { void Method(out int i, out string s1, out string s2) i = 44; s1 = "I've been returned"; s2 = null; } static void Main() { OutReturnExample ob= new OutReturnExample(); int value; string str1, str2; ob.Method(out value, out str1, out str2); // value is now 44 // str1 is now "I've been returned" // str2 is (still) null;


Download ppt "The Lifecycle of an Object"

Similar presentations


Ads by Google