- 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)
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
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
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;
}
Use:
in creating arrays of anonymous types