Professional Documents
Culture Documents
Methods:: Sathya Technologies
Methods:: Sathya Technologies
Methods:: Sathya Technologies
Methods :
1. What is Method ?
A method is a code block that contains a series of statements. A program causes the
statements to be executed by calling the method and specifying any required method
arguments.
When a method declaration includes a static modifier, that method is said to be a static
method and a static method invoked using class name.
The difference between static and instance method is that multiple instance of a class can
be created and each instance has its own separate method . where as a method is static
,there are no instance of that method and invoke only that one definition of the static
method.
Class:
7. What is the class ?
A class consist of data and behavior . data represents its fields and behavior represents its
methods.
KannaBabu Page 1
SATHYA TECHNOLOGIES
1.fields
2.methods
3properties
4.events
5.indexers
6.constructors
9.
When a class member includes static modifiers that member is called static member and
static member invoked using class name.
When a class member no static modifiers that member is called instance method and
instance member invoked using object of the class.
The purpose of the class constructor is to initialize class fields. A class constructor is
automatically called when instance of class is created.
Inheritance
12. What is the inheritance?
Creating new classes from existing classes such that the new classes will acquire all the
features of existing classes is called inheritane. Main advantage of inheritance
is reusability of the code
13. What are the 4 pillars of any object oriented programming language?
1. Abstraction
KannaBabu Page 2
SATHYA TECHNOLOGIES
2. Inheritance
3. Encapsulation
4. Polymorphism
KannaBabu Page 3
SATHYA TECHNOLOGIES
when a single derived class is created from a single base class then the
inheritance is called as single inheritance.
when more than one derived class are created from a single base class, then
that inheritance is called as hierarchical inheritance.
when a derived class is created from another derived class, then that
inheritance is called as multi level inheritance.
KannaBabu Page 4
SATHYA TECHNOLOGIES
polymorphisum
24. What is the polymorphisum
Polymorphism allows to invoke derived class methods through a base class reference
during run-time. In base class method is declared virtual and in the derived class
override the same method.
26. What is the difference between a virtual method and an abstract method?
A virtual method must have a body where as an abstract method should not have a body
28. Can you access a hidden base class method in the derived class?
Yes, Hidden base class methods can be accessed from the derived class by casting the
instance of the derived class to an instance of the base class .
Method overloading allows a class to have multiple methods with the same name , but
with a different signature. The signature of a method consist of the name of the method
and type,kind and number of its formal parameters.ths signature of method does not
include the reture type and param modifier.
32. What is the difference between method hidding and method overriding?
In the method overriding a base class reference variable pointing to a child class object
will invoke the overridden method in the child class.
KannaBabu Page 5
SATHYA TECHNOLOGIES
In the method hiding a base class reference variable pointing to a child class object
will invoke the hidden method in the base class.
properties
33. What are Properties in C#. Explain with an example?
Properties in C# are class members that provide a flexible mechanism to read, write, or
compute the values of private fields. Properties can be used as if they are public data
members, but they are actually special methods called accessors. This enables data to be
accessed easily and still helps promote the safety and flexibility of methods.
The code block for the get accessor is executed when the property is read and the code
block for the set accessor is executed when the property is assigned a new value.
A property that is marked with a static keyword is considered as static property. This
makes the property available to callers at any time, even if no instance of the class exists.
In the example below PI is a static property.
using System;
class Circle
KannaBabu Page 6
SATHYA TECHNOLOGIES
{
private static double _pi = 3.14;
public static double PI
{
get
{
return _pi;
}
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}
KannaBabu Page 7
SATHYA TECHNOLOGIES
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
// FullName is virtual
public virtual string FullName
{
get
{
return _lastName + ", " + _firstName;
}
}
}
class BankCustomer : Customer
{
// Overiding the FullName virtual property derived from customer class
public override string FullName
{
get
{
return "Mr. " + FirstName + " " + LastName;
}
}
}
class MainClass
{
public static void Main()
{
BankCustomer BankCustomerObject = new BankCustomer();
BankCustomerObject.FirstName = "David";
BankCustomerObject.LastName = "Boon";
KannaBabu Page 8
SATHYA TECHNOLOGIES
using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
KannaBabu Page 9
SATHYA TECHNOLOGIES
BankCustomerObject.FullName);
}
}
40. Can you use virtual, override or abstract keywords on an accessor of a static
property?
No, it is a compile time error to use a virtual, abstract or override keywords on an
accessor of a static property.
KannaBabu Page 10
SATHYA TECHNOLOGIES
structures
41. What is the structure?
KannaBabu Page 11
SATHYA TECHNOLOGIES
What is the base type from which all structs inherit directly?
All structs inherit directly from System.ValueType, which inherits from System.Object.
2. Struct are stored on stack memory where as class are stores on heap memory.
3. Values types are destroyed immediately after the scope is lost where as for reference
types only the reference variable destroyed after the scope is lost . the objects is later
destroyed by garbage collector.
5. Struct can not inherit from anthor class where as a class can. Both struct and class
can inherit from an interface.
Interface
43. Explain what is an Interface in C#?
46. If a class inherits an interface, what are the 2 options available for that class?
Option 1: Provide Implementation for all the members inheirted from the interface.
Option 2:: If the class does not wish to provide Implementation for all the members
inheirted from the interface, then the class has to be marked as abstract.
A class inherits from 2 interfaces and both the interfaces have the same method
name as shown below. How should the class implement the drive method for both
KannaBabu Page 12
SATHYA TECHNOLOGIES
To implement the Drive() method use the fully qualified name as shown in the example
below. To call the respective interface drive method type cast the demo object to the
respective interface and then call the drive method.
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
interface Bus
{
void Drive();
}
KannaBabu Page 13
SATHYA TECHNOLOGIES
void Bus.Drive()
{
Console.WriteLine("Drive Bus");
}
using System;
namespace Interfaces
{
interface Car
{
void Drive();
}
KannaBabu Page 14
SATHYA TECHNOLOGIES
//DemoObject.Drive();
// Error: Cannot call explicitly implemented interface method
// using the class object.
// Type cast the demo object to interface type Car
((Car)DemoObject).Drive();
}
}
}
1.Abstract class can have implementation for some of its members but the interface can
not have implementation for any of its methods.
2.Interface can not have fields where as Abastract class can have fields.
3. An interface can inherit another interface only where as an Abstract class can
inherit another abstract class or another interface.
4. abstract class members can have Access modifiers where as interface members
can not have access modifiers.
Answer You can enclose code in Try/Catch/Finally block. You can catch all exceptions
in the catch block. The third part of this block is finally. It is executed irrespective of the
fact that an exception has been raised
Access Modifiers
50. What are the differenct Access modifiers?
KannaBabu Page 15
SATHYA TECHNOLOGIES
1.private
2.public
3.protected
4.internal
5 protected internal
Protected members are available, within the container type and to the types that derives
from the containing type.
56. Can you use all access modifiers for all types?
No, Not all access modifiers can be used by all types or members in all contexts, and in
some cases the accessibility of a type member is constrained by the accessibility of its
containing type.
Can derived classes have greater accessibility than their base types?
No, Derived classes cannot have greater accessibility than their base types. For example
the following code is illegal.
using System;
internal class InternalBaseClass
{
public void Print()
{
Console.WriteLine("I am a Base Class Method");
}
KannaBabu Page 16
SATHYA TECHNOLOGIES
}
public class PublicDerivedClass : InternalBaseClass
{
public static void Main()
{
Console.WriteLine("I am a Public Derived Class Method");
}
}
When you compile the above code an error will be generated stating "Inconsistent
accessibility: base class InternalBaseClass is less accessible than class
PublicDerivedClass".To make this simple, you cannot have a public class B that derives
from an internal class A. If this were allowed, it would have the effect of making A
public, because all protected or internal members of A are accessible from the derived
class.
using System;
private class Test
{
public static void Main()
{
}
}
No, a compile time error will be generated stating "Namespace elements cannot be
explicitly declared as private, protected, or protected internal"
Can the accessibility of a type member be greater than the accessibility of its
containing type?
No, the accessibility of a type member can never be greater than the accessibility of its
containing type. For example, a public method declared in an internal class has only
internal accessibility.
KannaBabu Page 17
SATHYA TECHNOLOGIES
What is the default access modifier for a class,struct and an interface declared
directly with a namespace?
internal
using System;
interface IExampleInterface
{
public void Save();
}
No, you cannot specify access modifer for an interface member. Interface members are
always public.
DataType Casting
Explicit conversions: Explicit conversions require a cast operator. The source and destination
variables are compatible, but there is a risk of data loss because the type of the destination
KannaBabu Page 18
SATHYA TECHNOLOGIES
variable is a smaller size than (or is a base class of) the source variable.
What type of data type conversion happens when the compiler encounters the following
code?
ChildClass CC = new ChildClass();
ParentClass PC = new ParentClass();
Implicit Conversion. For reference types, an implicit conversion always exists from a class to
any one of its direct or indirect base classes or interfaces. No special syntax is necessary because
a derived class always contains all the members of a base class.
No, the above code will not compile. Double is a larger data type than integer. An implicit
conversion is not done automatically bcos there is a data loss. Hence we have to use explicit
conversion as shown below.
double d = 9999.11;
int i = (int)d; //Cast double to int.
If you want to convert a base type to a derived type, what type of conversion do you use?
Explicit conversion as shown below.
//Create a new derived type.
Car C1 = new Car();
// Implicit conversion to base type is safe.
Vehicle V = C1;
// Explicit conversion is required to cast back to derived type. The code below will compile but
throw an exception at run time if the right-side object is not a Car object.
Car C2 = (Car) V;
What operators can be used to cast from one reference type to another without the risk of
throwing an exception?
KannaBabu Page 19
SATHYA TECHNOLOGIES
The is and as operators can be used to cast from one reference type to another without the risk of
throwing an exception.
If you define a user defined data type by using the struct keyword, Is it a a value type or
reference type?
Value Type
If you define a user defined data type by using the class keyword, Is it a a value type or
reference type?
Reference type
What is the base class from which all value types are derived?
System.ValueType
Delegates
What is a delegate?
A delegate is a type safe function pointer. Using delegates you can pass methods
as parameters. To pass a method as a parameter, to a delegate, the signature of
the method must match the signature of the delegate. This is why, delegates are
called type safe function pointers.
KannaBabu Page 20
SATHYA TECHNOLOGIES
When you click the Button now, both Method1 and Method2 will be executed. So, this capability
of calling multiple methods on a single event is called as chaining delegates. In the example, we
are using EventHandler delegate, to hook up Method1 and Method2 to the click event of the
button control. Since, the EventHandler delegate is now pointing to multiple methods, it is also
called as multicast delegate.
No, the code does not compile. For the code to compile, the signature of Method1 should match the signature of
SampleDelegate.
KannaBabu Page 21
SATHYA TECHNOLOGIES
The partial class allows to split a class into 2 or more files. All these parts are
then combined into
KannaBabu Page 22