Friday, 20 July 2012
Thursday, 19 July 2012
JAGGED ARRAYS
Jagged
arrays are often called array of arrays. An element of a jagged array itself is
an array. For example, you can define an array of names of students of a class
where a name itself can be an array of three strings - first name, middle name
and last name. Another example of jagged arrays is an array of integers
containing another array of integers. For example,
int[][]
numArray = new int[][] { new int[] {1,3,5}, new int[] {2,4,6,8,10} };
Again, you
can specify the size when you call the new operator.
PROGRAMMING ARRAYS IN C#
Arrays are
probably one of the most wanted topics in C#. The focus of this article is
arrays in C#. The article starts with basic definitions of different array
types and how to use them in our application. Later, the article covers the
Arrays class and its methods, which can be used to sort, search, get, and set
an array's items.
Introduction
In C#, an
array index starts at zero. That means the first item of an array starts at the
0th position. The position of the last item on an array will total number of
items - 1. So if an array has 10 items, the last 10th item is at 9th position.
In C#,
arrays can be declared as fixed length or dynamic.
A fixed
length array can store a predefined number of items.
Let's take a
look at simple declarations of arrays in C#. The following code snippet defines
the simplest dynamic array of integer types that does not have a fixed size.
int[]
intArray;
As you can
see from the above code snippet, the declaration of an array starts with a type
of array followed by a square bracket ([]) and name of the array.
The
following code snippet declares an array that can store 5 items only starting
from index 0 to 4.
int[]
intArray;
intArray =
new int[5];
Example
1.Matrix addition &substraction
Void main()
{
Int
i=0,j=0,row,col;
Int[,]a=new
int[100,100];
Int[,]b=new
int[100,100];
Int[,]result=new
int[100,100];
Console.writeline(“enter
the no.of rows”);
Row=convert.ToInt32(console.Readline());
Console.writeline(:enter
the no.of columns”);
col=convert.ToInt32(console.Readline());
Console.writeline(“enter
the 1st matrix”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
A[I,j]=
convert.ToInt32(console.Readline());
}
}
Console.writeline(“enter
the 2nd matrix”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
b[I,j]=
convert.ToInt32(console.Readline());
}
}
Console.writeline(“after
addition result is:”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Result[I,j]=a[I,j]+b[I,j];
}
}
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Console.writeline(“/t”
result[I,j]);
}
Console.writeline();
}
Console.writeline(“after
substraction result is”);
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Result[I,j]=a[I,j]-b[I,j];
}
}
For(i=0;i<row;i++)
{
For(j=0;j<col;j++)
{
Console.writeline(“/t”
result[I,j]);
}
Console.writeline();
Console.readline();
}
2.printstar
namespace PRINTSTAR
{
class Program
{
static void
Main(string[] args)
{
int n, r,
c, temp;
Console.WriteLine("enter the no.of rows");
n =
Convert.ToInt32(Console.ReadLine());
temp = n;
for (r = 1;
r <= n; r++)
{
for (c
= 1; c < temp; c++)
{
Console.Write(" ");
}
temp--;
for (c
= 1; c <=2 * r - 1; c++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output
*
* *
* * *
SEALED CLASSES
Sealed
classes are used to restrict the inheritance feature of object oriented
programming. Once a class is defined as sealed class, this class cannot be inherited.
The
following class definition defines a sealed class in C#:
// Sealed
class
sealed class
SealedClass
{
}
Example
using
System;
class Class1
{
static void
Main(string[] args)
{
SealedClass
sealedCls = new SealedClass();
int total =
sealedCls.Add(4, 5);
Console.WriteLine("Total
= " + total.ToString());
}
}
// Sealed
class
sealed class
SealedClass
{
public int
Add(int x, int y)
{
return x +
y;
}
}
INTERFACE
An interface
is a reference type object with no implementation. You can think of it as an abstract class with
all the implementation stripped out and everything that is not public
removed. Abstract classes are classes
that can not be instantiated. No
properties or methods are actually coded in an interface, they are only
defined. So the interface doesn't
actually do anything but only has a signature for interaction with other
classes or interfaces. A good analogy
for an interface is a pencil and a pencil sharpener. When we talk of a pencil and a pencil
sharpener we generally know what they do and how they interact. We don't have
to know the inner workings of a particular sharpener or pencil, only how to use
them through their interface.
Example
public
interface IPencil
{
void
Write();
bool IsSharp
{ get; }
}
public
interface IPencilSharpener
{
void
Sharpen(IPencil pencil);
}
ABSTRACT CLASS
Abstract
classes are Classes which cannot be instantiated. This means one cannot make a
object of this class or in other way cannot create object by saying ClassAbs
abs = new ClassAbs(); where ClassAbs is abstract class.Abstarct classes
contains have one or more abstarct methods, ie method body only no
implementation.
abstract
class human
{
//Abstract methods
public
abstract void hair();
//Concrete method
public void display()
{
System.Console.WriteLine("hello");
}
}
class boys :
human
{
public override void hair()
{
System.Console.WriteLine("Small");
}
}
class girls
: human
{
public override void hair()
{
System.Console.WriteLine("LONG");
}
}
class a
{
public static void Main()
{
boys b=new boys();
girls g=new girls();
b.hair();
g.hair();
b.display();
g.display();
}
}
STRUCTURES
A structure is an enhanced version of the primitive data types we have
used in previous lessons. Like a class, a structure is created from one
variable of a primitive type or by combining various variables of primitive
types.
To create a structure, you use the same formula as for a class but with
the struct keyword. Here is an example of a structure:
struct Integer
{
}
Like a class, a structure can have fields. They are listed in the body of
the structure. Here is an example:
struct Integer
{
private int val;
}
A structure can also have properties. Here is an example:
struct Integer
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
}
A structure can also have methods. Here is an example:
struct Integer
{
private int val;
public int Value
{
get { return val; }
set { val = value; }
}
public int Read()
{
return
int.Parse(Console.ReadLine());
}
Differences between Class and Structure
· Classes are Reference types and
Structures are Values types.
· Classes will support an Inheritance
whereas Structures won’t.
· Classes can have explicitly
parameterless constructors whereas structures can’t.
· Member variable initialization is
possible in class whereas in Structures, it is not.
· It is not possible to declare
destructor in structure but in class it is possible.
· Process of converting structure type
into object type is called boxing and process of converting object type into
structure type is called unboxing.
Subscribe to:
Posts (Atom)