Академический Документы
Профессиональный Документы
Культура Документы
1] Class:
A class is a collection of objects and represents description of objects that share same
attributes and actions. It contains characteristics of the objects such as objects
attributes, actions or behaviors.
For example, if you consider “Bike” as a class then its behavior is to get bike color,
engine, mileage etc. Here is the example of Method:
For example, A “Bike” usually has common elements such as bike color, engine, mileage
etc. In OOP terminology these would be called as a Class Properties or Attributes of a
Bike object. Here is the example of Object:
It is simple to create its object and access Bike class. You can create object of class via
single line of code as shown below.
Constructors in C#:
Constructor:
Constructor is a special method of a class which will invoke automatically whenever
instance or object of class is created. Constructors are responsible for object initialization
and memory allocation of its class. If we create any class without constructor, the
compiler will automatically create one default constructor for that class. There is always
at least one constructor in every class.
Here you need to remember that a class can have any number of constructors and
constructors don’t have any return type, not even void and within a class we can create
only one static constructor.
Generally constructor name should be same as class name. If we want to create
constructor in a class we need to create a constructor method name same as class name
check below sample method for constructor
Example:
class SampleA
{
public SampleA(){
Console.WriteLine("Sample A Test Method"); }}
Types of Constructor
Basically constructors are 5 types those are
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor
4. Static Constructor
5. Private Constructor
1.Default Constructor
In above method we can create object of class with parameters will work fine. If create
object of class without parameters it will not allow us create.
// it will works fine
Sample obj = new Sample("Welcome","to Aspdotnet-Suresh");
// it will not work because of inaccessability
Sample obj=new Sample();
Important points of private constructor
- One use of private construct is when we have only static member.
- Once we provide a constructor that is either private or public or any, the compiler
will not allow us to add public constructor without parameters to the class.
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:
When the above code is compiled and executed, it produces the following result:
Zara
Riz
Nuha
Asif
Davinder
Sunil
Rubic
Overloaded Indexers
Indexers can be overloaded. Indexers can also be declared with multiple parameters
and each parameter may be a different type. It is not necessary that the indexes have
to be integers. C# allows indexes to be of other types, for example, a string.
Program:
using System;
namespace IndexerApplication
{
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;
Accessors:
The accessor of a property contains the executable statements that helps in getting (reading or
computing) or setting (writing) the property. The accessor declarations can contain a get accessor, a set
accessor, or both. For example:
// 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:
Declaring Interfaces
Interfaces are declared using the interface keyword. It is similar to class declaration.
Interface statements are public by default. Following is an example of an interface
declaration:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
namespace InterfaceApplication
{
Abstract Class:
We cannot create an object of an abstract class and can call the method of abstract
class with the help of class name only.
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
abstract class M1
return (a + b); }}
class M2 :M1
15SE329E-Visual Programming Notes-Unit-II
{
return a * b; }}
class test
M2 ob = new M2();
Console.ReadLine();
}}}
Output:
The Result is 30
Interface:
interface
{ //Interface member
NOTE :
Properties are defined in an interface with the help of an access block get and set, which
are permitted for the property.
int myint
get;
set;
}}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication3
interface MyInterface
void myMethod();
cls.myMethod();}
Console.ReadLine();
}}}
1. An Abstract class doesn't provide full abstraction but an interface does provide
full abstraction; i.e. both a declaration and a definition is given in an abstract
class but not so in an interface.
2. Using Abstract we can not achieve multiple inheritance but using an Interface we
can achieve multiple inheritance.
3. We can not declare a member field in an Interface.
EXCEPTIONS:
An exception is a problem that arises during the execution of a program. A C# exception
is a response to an exceptional circumstance that arises while a program is running,
such as an attempt to divide by zero.
Exceptions provide a way to transfer control from one part of a program to another. C#
exception handling is built upon four keywords: try, catch, finally, and throw.
• try: A try block identifies a block of code for which particular exceptions is
activated. It is followed by one or more catch blocks.
• finally: The finally block is used to execute a given set of statements, whether
an exception is thrown or not thrown. For example, if you open a file, it must be
closed whether an exception is raised or not.
• throw: A program throws an exception when a problem shows up. This is done
using a throw keyword.
Basics of Exception Handling
Up normal conditions or unexpected condition in a program during runtime is called
EXCEPTION. Exceptions are run time anomalies or run time errors or unusual conditions that a
program may encounter while executing.
Exceptions are of two kinds, namely, synchronous exception and asynchronous exceptions
1. Asynchronous exception:
Excepections are caused by events or faults are unrelated to the program and beyond
the control of the program.
Eg: keyboard intrepts, disk failure, hardware malfunction.
2. Exception which occurs during the pgm execution due to some fault in input
data(Technique with in the program).
You can also define your own exception. User-defined exception classes are derived
from the Exception class. The following example demonstrates this:
Program:
using System;
namespace UserDefinedException
class TestTemperature
try
temp.showTemp();
catch(TempIsZeroException e)
Console.ReadKey();}}}
}
15SE329E-Visual Programming Notes-Unit-II
}
int temperature = 0;
if(temperature == 0)
else{
When the above code is compiled and executed, it produces the following result: