Loading…
Transcript

Creating and Managing Classes and Objects

- comes from root word classification

-represents an entity

- things that share common behaviour and posses common attributes

- .NET framework supports thousands of classes

Purposes

- combine methods and data in a class

- Control the accessibility of methods and data

(information hiding)

Defining and using a class

Controlling accessibility

default private

-special method that runs automatically when object is created

- same name as class

- can take parameters but does not return

-default constructors

- constructors need to be public

-but no default will be provided by compiler :(

use keyword partial

C#Program to demonstrate Point class and illustrate use of this keyword

Encapsulation

- static method does not depend on an instance of a class

- it cannot access any instance fields or methods defined in a class

- It can use only fields and other methods that are marked as static

- used to create shared fields

-static fields can also be created using const keyword

static with using

problem is less maintainable code

classes

ao=new {Name="Johny",Age=25}

var ao=new {Name="Johny",Age=25}

Restrictions:

- contain only public fields

- all fields must be initialized

- cannot be static

- methods cannot be defined

Understanding Values and References

C# example program to demonstrate value and reference parameters

value used to initialize reference types

null condition operator

nullable types

properties: HashValue and Value

-method parameters,variables get memory on stack

- block,loop also on stack

- value types:stack, reference types:heap

-nullable types:heap

System.object -master class

all the classes inherited from this master class

Boxing and Unboxing

Casting data safely:

is, as

unsafe pointers

enumerations - more readable and robust

declaring and using enumeration

Chosing enumeration literal values

chosing enumeration underlying type

byte, sbyte, short, ushort,int, uint, long, ulong

In some cases class may contain so little data that overhead of managing heap becomes disproportionate - solution use "structures"

like classes, struct can have its own methods and fields

most of primitive types are aliases for structures

e.g int- System.Int32, long- Sytsem.Int64

useful methods- ToString(), Parse()

fields-MaxValue

declaring and using structures

Difference between structures and class:

- default constructor declaration

- explicit initialization of all fields in non-default constructor

- initialization of instance fields at point of declaration

Structure initalization picture

structure copy

(only if right hand side is fully initialized)

Array - unordered sequence of items,

each item homogeneous,

allocated contigious memory,

accessed using index

Array declaration

type[ ] name

Creating array instance:

- arrays are reference types

-declaration: stack

-instance creation:stack and heap

-dynamic size allocation possible

Array population

-values need not be constants

-number of elements should match array size

Arrays

Array declarations

var names=new[ ]{"ena","meena","dika"}

var data=new[ ]{"smg",23};

var num=new[ ]{1,2.5,3}

-index to access as well as change content

-IndexOutOfBound Exception

for loop for array iteration

for-each loop for array iteration

for useful: portion access,

backside traversal,

print index,

modify elements

Passing Arrays as parameters

public void process(int [ ]data)

{

foreach(int i in data)

{

}

}

Arrays as method return values

public int[ ] read()

{

int[ ] a;

....

return a;

}

Array Copy

reference copy

element by element copy

instance method CopyTo()

static method Copy()

instance method Clone()

Jagged Arrays

Use:

in creating arrays of anonymous types

Enumerations and Structures

Classes

Shallow and deep copy