Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.

Similar presentations


Presentation on theme: "Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing."— Presentation transcript:

1 Lecture 2 Basics of C#

2 Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing type. Generally, you should use fields only for variables that have private or protected accessibility. Data that your class exposes to client code should be provided through methods, properties and indexers. A method is a code block that contains a series of statements. A program causes the statements to be executed by calling the method and specifying any required method arguments. In C#, every executed instruction is performed in the context of a method. A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. Indexers allow instances of a class to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

3 Allocating Memory for a Class Declaring the variable of the class type allocates the memory to hold the reference, but not the memory to hold the actual data of the class object. To allocate memory for the actual data, you use the new operator. The new operator allocates and initializes memory for an instance of the specified type. It allocates the memory from either the stack or the heap, depending on the type. Use the new operator to form an object-creation expression, which consists of the following: The keyword new. The name of the type of the instance for which memory is to be allocated. Matching parentheses, which might or might not include parameters. If the memory allocated is for a reference type, the object-creation expression returns a reference to the allocated and initialized instance of the object in the heap.

4 A class declaration acts as a blueprint from which you can create as many instances of the class as you like. Instance members: Each instance of a class is a separate entity that has its own set of data members, distinct from the other instances of the same class. These are called instance members since they are associated with an instance of the class. Static members: Instance members are the default, but you can also declare members called static members, which are associated with the class, not the instance. Instance Members

5 Public and Private Access Public members of an instance are accessible to other objects in the program. You must use the public access modifier to specify public access.

6 Accessing Members from Inside the Class

7 Accessing Members from Outside the Class

8 Putting It All Together

9 You can call other methods from inside a method body. The phrases call a method and invoke a method are synonymous. You call a method by using its name, along with the parameter list, which I’ll discuss shortly. For example, the following class declares a method called PrintDateAndTime, which is called from inside method Main: Method Invocations

10 In the following code, method GetHour is called in the WriteLine statement in Main and returns an int value to that position in the WriteLine statement. Method Returning a Value

11 Early Returns Often, however, you can simplify your program logic by exiting the method early when certain conditions apply. You can exit a void method at any time by using the following form of the return statement, with no parameters: return; This form of the return statement can be used only with methods declared void.

12 So far, you’ve seen that methods are named units of code that can be called from many places in a program and can return a single value to the calling code. Returning a single value is certainly valuable, In C# methods can accept and return multiple values. Parameters are a special kind of variable that can allow you to do both these things. Parameters public void moveXY( ref int x, ref int y, ref int vx, ref int vy, int maxx, int maxy) { x += vx; y += vy; if(x>maxx || x<0) vx = -vx; if(y>maxy || v<0) vy = -vy; }

13 A class can have more than one method with the same name. This is called method overloading. Each method with the same name must have a different signature than the others. The signature of a method consists of the following information from the method header of the method declaration: − The name of the method − The number of parameters − The data types and order of the parameters − The parameter modifiers The return type is not part of the signature—although it’s a common mistake to believe that it is. Notice that the names of the formal parameters are not part of the signature. Method Overloading

14 Besides instance fields, classes can have what are called static fields. A static field is shared by all the instances of the class, and all the instances access the same memory location. Hence, if the value of the memory location is changed by one instance, the change is visible to all the instances. Instance members come into existence when the instance is created and go out of existence when the instance is destroyed. Static members exist and are accessible even if there are no instances of the class. Static Fields

15 Besides static fields, there are also static function members. Static function members, like static fields, are independent of any class instance. Even if there are no instances of a class, you can still call a static method. Static function members cannot access instance members. They can, however, access other static members. For example, the following class contains a static field and a static method. Notice that the body of the static method accesses the static field. Static Function Members

16 In most of the examples so far, the property has been associated with a backing field, and the get and set accessors have referenced that field. However, a property does not have to be associated with a field. In the following example, the get accessor computes the return value. In the code, class RightTriangle represents, not surprisingly, a right triangle. Computed Read-Only Property


Download ppt "Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing."

Similar presentations


Ads by Google