|
Date
|
Monday,
December 02, 2013
|
|
Time
|
Description
|
|
10.00 am to 12.00 pm
|
Structure,property,indexer
|
|
Learning:
|
|
|
Structure:-
Structures are used to represent a record.
User defined value types are known as
structures.Structures are stored on stack and contains values directly.
eg:-
struct cycle
{
Int val,
min_speed,max_speed;
};
Static void main(string[] args)
{
Cycle c;
c.val=10;
c.min_speed=30;
c.max_speed=50;
console..writeline(“max speed is {0}”,c.max_speed);
}
Features of c# structures:-
1)Structures
can have methods, fields, indexers, properties, events.
2)Structures
can have defined constructors and not destructors.however you cannot define a
default constructor to the structure.
3)Structure
members cannot be specified as abstract, virtual, protected.
Properties:-
Properties are named members of
classes,structures and interfaces.Member variable or methods in a class or
structure are called fields. Properties are an extension of fields and are
accessed using the same syntax. They use accessor through which the value can
be read and manipulated.
Accessor:- it contains executable
statements that helps in getting and setting the values of the properties.
Program:-
class Student
{
private string code = "N.A";
private string name = "not
known";
private int age = 0;
// Declare a Code property of type
string:
public string Code
{
get
{
return code;
}
set
{
code = value;
}
}
// Declare a Name property of type
string:
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
// Declare a Age property of type int:
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public override string ToString()
{
return "Code = " + Code +",
Name = " + Name + ", Age = " + Age;
}
}
class ExampleDemo
{
public static void Main()
{
// Create a new Student object:
Student s = new Student();
// Setting code, name and the age of
the student
s.Code = "001";
s.Name = "Zara";
s.Age = 9;
Console.WriteLine("Student
Info: {0}", s);
//let us increase age
s.Age += 1;
Console.WriteLine("Student
Info: {0}", s);
Console.ReadKey();
}
}
}
Output:-
Student Info: Code = 001, Name = Zara, Age = 9 Student Info: Code = 001, Name = Zara, Age = 10
Indexer:-
An indexer allows an object to be indexed as an
array. When an indexer is defined for a class then class behaves like a
virtual array. The instance of the class can be accessed by array access
operators([ ]).
Program:-
class IndexedNames { private string[] namelist = new string[size]; static public int size = 10; public IndexedNames() { for (int i = 0; i < size; i++) namelist[i] = "N. A."; } public string this[int index] { get { string tmp;
if( index >= 0 && index <= size-1 ) { tmp = namelist[index]; } else { tmp = ""; }
return ( tmp ); } set { if( index >= 0 && index <= size-1 ) { namelist[index] = value; } } }
static void Main(string[] args) { IndexedNames names = new IndexedNames(); names[0] = "Zara"; names[1] = "Riz"; names[2] = "Nuha"; names[3] = "Asif"; names[4] = "Davinder"; names[5] = "Sunil"; names[6] = "Rubic"; for ( int i = 0; i < IndexedNames.size; i++ ) { Console.WriteLine(names[i]); } Console.ReadKey(); } } }
Output :-
Zara Riz Nuha Asif Davinder Sunil Rubic N. A. N. A. N. A. |
|
Date Friday, November 29, 2013 Time Description 10.00 am to 12.00 pm Control structure in c# Learning : For Loops Why is this important to learn about? Control structures are one of the most fundamental concepts in any programming language you will come across. If you want to know C#, and if you want to learn to program with any language, you must have a firm grip on the control structures of the language. Definitions of terms used . Iteration – This is the act of repeating something (that ‘something’ being code statements in the programming context). Conditional – A conditional action is said to be one that is only performed if a certain condition is true. For Loops For loops allow us to specify the number of times to repeat a block of code. It is best demonstrated with an example: for (int i = 1; i <= 5; i++) { Console...
Comments
Post a Comment