Professional Documents
Culture Documents
class TwoDShape
{
public double Width;
public double Height;
public void ShowDim()
{
Console.WriteLine("Width and height are " + Width + " and " + Height);
}
}
// Triangle is derived from TwoDShape.
class Triangle : TwoDShape
{
public string Style;
public double Area()
{
return Width * Height / 2;
}
public void ShowStyle()
{
Console.WriteLine("Triangle is " + Style);
}
}
class Shapes
{
static void Main()
{
Triangle t1 = new Triangle();
t1.Width = 4.0;
t1.Height = 4.0;
t1.Style = "isosceles";
Console.WriteLine("Info for t1: ");
t1.ShowStyle();
t1.ShowDim();
Console.WriteLine("Area is " + t1.Area());
Console.WriteLine();
Triangle t2 = new Triangle();
t2.Width = 8.0;
t2.Height = 12.0;
t2.Style = "right";
Console.WriteLine("Info for t2: ");
t2.ShowStyle();
t2.ShowDim();
Console.WriteLine("Area is " + t2.Area());
}
}
Case-3
using System;
abstract class TwoDShape
{
public double Width;
public double Height;
public void ShowDim()
{
Console.WriteLine(" ----TwoDShape--- ");
Console.WriteLine("Width and height are " + Width + " and " + Height);
}
// Now, Area() is abstract.
public abstract double Area();
}
class Triangle : TwoDShape
{
public override double Area()
{
return Width * Height / 2;
}
}
class Rectangle : TwoDShape
{
public override double Area()
{
return Width * Height;
}
}
class AbsShape
{
static void Main()
{
Rectangle obj = new Rectangle();
obj.ShowDim();
obj.Width = 10;
obj.Height = 11;
obj.ShowDim();
double x = obj.Area();
Console.WriteLine("Area of Rectangle is " + x);
Console.WriteLine();
Triangle obj1 = new Triangle();
obj1.ShowDim();
obj1.Width = 100;
obj1.Height = 110;
obj1.ShowDim();
double y = obj1.Area();
Console.WriteLine("Area of Triangle is " + y);
}
}
case-4
using System;
abstract class A
{
public int x;
public abstract void show()
{
Console.WriteLine(" X= " + x);
}
}
class Test:A
{
public override void show()
{
Console.WriteLine("This method is overrided");
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.show();
}
}
OutPut :'A.show()' cannot declare a body because it is marked abstract
Case-5
using System;
abstract class A
{
public int x;
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.x=10;
obj.GenShow();
//obj.show();
}
}
If abstract methods are not overrided in the derived classes then it will
through an error as follows:
'Test' does not implement inherited abstract member 'A.show()'
Case-7
It is allowed to inherit one abstract class by two normal classes
using System;
abstract class A
{
public abstract void show();
}
class Test:A
{
public override void show()
{
Console.WriteLine(" Inside Test Class");
}
}
class Test1 : A
{
public override void show()
{
Console.WriteLine(" Inside Test1 Class");
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.show();
Test1 obj1 = new Test1();
obj1.show();
}
}
Case-8
It is not allowed to inherit two abstract classes by one General class. It will throw
an error as
Class 'Test' cannot have multiple abstract base classes:'A' and 'B'
using System;
abstract class A
{
public abstract void aShow();
}
abstract class B
{
public abstract void bShow();
}
class Test:A,B
{
public override void aShow()
{
Console.WriteLine(" calls aShow abstract method");
}
public override void bShow()
{
Console.WriteLine(" calls bShow abstract method");
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.aShow();
obj.bShow();
}
}
Case-9
It is allowed to implement multiple interfaces by a single class. But it is not
allowed to inherit multiple classes to be inherited by single class.
Case-10
In a single namespace it is allowed to declare a multiple classes with the same
name. How?
By using partial key word we can write a single class at multiple places and when
work is completed it is combined to a single unit when compiled. This the main
class Test:A
{
public void show()
{
Console.WriteLine(" value of x is -" + StX);
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.StX = 10;
obj.show();
}
}
Case-13
By declaring variable and method as protected we can make them private to
other classes and public to derived classes. You can not allowed to access
variables and methods directly out side of the class by using the object of the
class it it is not inherited to the corresponding class.
class A
{
protected int x;
}
class Test : A
{
public void show()
{
Console.WriteLine(" value of x is -" + x);
}
public void setX(int val)
{
x = val;
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.setX(100);
obj.show();
}
}
Case-14
This is illegal to access a protected member and member function out side the
class directly. It will through an error as 'A.x' is inaccessible due to its
protection level
using System;
class A
{
protected int x;
}
class Test:A
{
public void show()
{
Console.WriteLine(" value of x is -" + x);
}
}
class AbsTest
{
public static void Main()
{
Test obj = new Test();
obj.x = 10;
obj.show();
}
}
Case-15
Display max value among three.
class MyTest
{
public static int Max(int a, int b, int c)
{
Console.Write("Max(int, int, int): ");
a = a > b ? a : b;
return a > c ? a : c;
}
public static void Main(String[] args)
{
Console.WriteLine(Max(5, 8, 7));
}
}
Case-16
It is allowed to use the reserved keywords to be use as normal variable. Use @
symbol with the reserved keywords for declaring such type of variables.
class School
{
const int @class = 2004;
const bool @public = true;
String @delegate = "J. Smith ";
public static int @double(int i)
{
return 2 * @i;
}
public static void Main(String[] args)
{
School school = new School();
Console.WriteLine(school.@delegate.Trim() + " " + School.@class);
}
}
Case-17
Check the out for the below code.
class MyTest {
public static void Main(String[] args)
{
Object o1 = new Object();
Object o2 = new Object();
Object o3 = o1;
Console.WriteLine(o1.Equals(o3) + " " + o1.Equals(o2));
// True False
Console.WriteLine(o1.GetHashCode() == o3.GetHashCode());
// True
Console.WriteLine(o1.GetHashCode() == o2.GetHashCode());
// Usually False
Console.WriteLine(o1.GetHashCode() + " " + o2.GetHashCode()); // Usually
distinct
Console.WriteLine(o1.GetType());
// System.Object
String s1 = "abc";
String s2 = "ABC";
String s3 = s1 + "";
Console.WriteLine(s1.Equals(s3) + " " + s1.Equals(s2));
// True False
Console.WriteLine(s1.GetHashCode() == s3.GetHashCode());
// True
Console.WriteLine(s1.GetHashCode() == s2.GetHashCode());
// Usually False
Console.WriteLine(s1.GetHashCode() + " " + s2.GetHashCode()); // Usually
distinct
Console.WriteLine(s1.GetType());
Console.WriteLine(117.GetHashCode());
Console.WriteLine(5.GetType());
Console.WriteLine(5.0.GetType());
// System.String
// 117
// System.Int32
// System.Double
}
}
Case-18
Check the out put of x
class Scope
{
void M1(int x)
{
x = 7;
// Hide the input value of x
Console.WriteLine("M1 x=" + x);
}
void M3()
{
int x;
x = 7;
//Simply print the x's value
Console.WriteLine("M3 x=" + x);
}
void M4()
{
x = 7;
// int x;
// would be ILLEGAL, giving a new meaning to x
Console.WriteLine("M4 x=" + x);
}
void M5()
{
{
int x;
x = 7;
//Simply print the x's value
Console.WriteLine("M5-1 x=" + x);
}
{
x = 7;
//Simply print the x's value
Console.WriteLine("M5-2 x=" + x);
}
}
public int x;
public static void Main(String[] args)
{
Scope s = new Scope();
s.x = 88;
s.M1(8);
s.M3();
s.M4();
s.M5();
}
}
Case -19
Q: How to pass optional parameters to a procedure ?
A: When we are declaring an input parameter type as params it will become
optional parameter at the time of method call. If params parameter is not passed
then it will silently ignore that parameter value and consider that as a optional
parameter.
class ADemo
{
class test
{
public int x;
public int y;
public void display(int a, int b, params string[] str)
{
Console.WriteLine(this.x = a);
Console.WriteLine(this.y = b);
//Console.WriteLine(str[0]); // This is optional parameter
}
}
static void Main()
{
test t = new test();
t.display(1, 2);
//t.display(1, 2,"9"); //This is for optionla parameter call
}
}
In C# 4.0 the same can be achieved as follows by declaring the input parameters
as null.
class ADemo
{
class test
{
public int x;
public int y;
string literal
that spans several lines.
");
Console.WriteLine(@"Here is some tabbed output:
1234
5678
");
Console.WriteLine(@"Programmers say, ""I like C#.""");
}
}
The output from this program is shown here:
This is a verbatim
string literal
that spans several lines.
Here is some tabbed output:
1234
5678
Programmers say, "I like C#."
Q: Is a string consisting of a single character the same as a character
literal? For
example, is k the same as k?
A: No. You must not confuse strings with characters. A character literal represents
a single
letter of type char. A string containing only one letter is still a string. Although
strings
consist of characters, they are not the same type.
Q:What is implicitly typed variable ?
A:With C# 3.0, it is possible to let the compiler determine the type of a variable
based on the value used to initialize it. An implicitly typed variable is declared
using the keyword var, and it must be initialized.
The compiler uses the type of the initializer to determine the type of the variable.
Example:
var pi = 3.1416;
Because pi is initialized with a floating-point literal (whose type is double by
default), the type of pi is double.
If pi been declared like this:
var pi = 3.1416M;
then pi would have the type decimal .
It is important to emphasize that an implicitly typed variable is still a strongly
to see whether it equals 10, but no further action takes place. Of course, since i is
incremented
within the body of the loop, the loop runs normally, displaying the following
output:
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass
#0
#1
#2
#3
#4
#5
#6
#7
#8
#9
Pass #7
Pass #8
Pass #9
Q:What is the out put of the following ?
A:
using System;
class Empty2
{
static void Main()
{
for (; ; ) // intentionally infinite loop
{
Console.WriteLine("infinite loop");
}
}
}
class Empty3
{
static void Main()
{
int i;
int sum = 0;
// Sum the numbers through 5.
for (i = 1; i <= 5; sum += i++) ;
Console.WriteLine("Sum is " + sum);
}
}
The output from the program is shown here:
Sum is 15
Q: Given the flexibility inherent in all of C#s loops, what criteria should I use
when selecting a loop? That is, how do I choose the right loop for a specific job?
A: Use a for loop when performing a known number of iterations. Use the do-while
when you
need a loop that will always perform at least one iteration. The while is best used
when the loop will repeat an unknown number of times.
Q:What is a Class ?
A:A class is a template that defines the form of an object. It typically specifies
both code and data, with the code acting on the data. C# uses a class
specification to construct objects. Objects are instances of a class. Thus, a class is
essentially a set of plans that specify how to build an object. It is important to be
clear on one issue: A class is a logical abstraction. It is not until an object of that
class has been created that a physical representation of that class exists in
memory.
Methods and variables that constitute a class are called members of the class.
Q: Check this
class A
{
public int x;
public int y;
int z;
}
class ADemo
{
static void Main()
{
A a = new A();
a.x = 7;
a.y = 16;
//a.z = 100;
//A.z is inaccessible due to its protection level.
Console.WriteLine("X" + a.x);
Console.WriteLine("Y" + a.y);
// Console.WriteLine("X" + x); //The name 'x' does not exist in the current
context
// Console.WriteLine("Y" + y); //The name 'y' does not exist in the current
context
// Console.WriteLine("Z" + a.x); //error
}
}
Q: Check this
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
a.x = 7;
a.y = 16;
A a1 = new A();
a1 = a;
Console.WriteLine(" The value of x ="+ a1.x);
Console.WriteLine(" The value of y ="+ a1.y);
}
}
Out Put :
Q:Check this
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
a.x = 7;
a.y = 16;
A a1 = a;
Console.WriteLine(" The value of x =", a1.x);
Console.WriteLine(" The value of y =", a1.y);
}
}
Out Put :
Check This:
class A
{
public int x;
public int y;
}
class ADemo
{
Q: Check this
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
A a1 = new A();
A a2 = new A();
a2 = a1;
a1 = a;
a.x = 10;
a.y = 12;
Console.WriteLine(" The value of x ="+ a.x);
Console.WriteLine(" The value of y ="+ a.y);
Console.WriteLine(" The value of x =" + a1.x);
Console.WriteLine(" The value of y =" + a1.y);
Q:Check this
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
A a1 = new A();
A a2 = new A();
a2 = a1;
a1 = a;
a.x = 10;
a.y = 12;
a2.x = 100;
a2.y = 120;
Console.WriteLine(" The value of x ="+ a.x);
Console.WriteLine(" The value of y ="+ a.y);
Console.WriteLine(" The value of x =" + a1.x);
Console.WriteLine(" The value of y =" + a1.y);
Console.WriteLine(" The value of x =" + a2.x);
Console.WriteLine(" The value of y =" + a2.y);
}
}
Out put :
Case Study:
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
A a1 = new A();
A a2 = new A();
a2 = a1;
a1 = a;
a.x = 10;
a.y = 12;
a1.x = 100;
a1.y = 120;
Console.WriteLine(" The value of a.x =" + a.x);
Console.WriteLine(" The value of a.y =" + a.y);
Console.WriteLine(" The value of a1.x =" + a1.x);
Console.WriteLine(" The value of a1.y =" + a1.y);
Console.WriteLine(" The value of a2.x =" + a2.x);
Console.WriteLine(" The value of a2.y =" + a2.y);
}
}
Out put :
Case Study :
class A
{
public int x;
public int y;
}
class ADemo
{
static void Main()
{
A a = new A();
A a1 = new A();
A a2 = new A();
a2 = a1;
a1 = a;
a1.x = 100;
a1.y = 120;
a.x = 10;
a.y = 12;
Console.WriteLine(" The value of a.x =" + a.x);
Console.WriteLine(" The value of a.y =" + a.y);
Console.WriteLine(" The value of a1.x =" + a1.x);
Console.WriteLine(" The value of a1.y =" + a1.y);
Console.WriteLine(" The value of a2.x =" + a2.x);
Console.WriteLine(" The value of a2.y =" + a2.y);
}
}
Out Put :
of indirection that adds overhead to each object access. This overhead is avoided
by a
value type.
As a point of interest, it is permitted to use new with the value types, as shown
here:
int i = new int();
Doing so invokes the default constructor for type int, which initializes i to zero. In
general,
invoking new for a value type invokes the default constructor for that type. It does
not,
however, dynamically allocate memory. Frankly, most programmers do not use
new with
the value types.
1. What is the difference between a class and an object?
A: A class is a logical abstraction that describes the form and behavior of an
object. An object is a
physical instance of the class.
2. How is a class defined?
A: A class is defined by using the keyword class. Inside the class statement, you
specify the code and data that comprise the class.
3. What does each object have its own copy of?
A: Each object of a class has its own copy of the class instance variables.
4. Using two separate statements, show how to declare an object called
counter of a class called MyCounter, and assign it a reference to an
object.
A: MyCounter counter;
counter = new MyCounter();
5. Show how a method called MyMeth( ) is declared if it has a return
type of double and has two int parameters called a and b.
A: double MyMeth(int a, int b) { // ...
6. How must a method return if it returns a value?
A: A method that returns a value must return via the return statement, passing
back the return
value in the process.
7. What name does a constructor have?
A: A constructor has the same name as its class.
8. What does new do?
A: The new operator allocates memory for an object and initializes it using the
objects constructor.
9. What is garbage collection and how does it work? What is a
destructor?
A: Garbage collection is the mechanism that recycles unused objects so that their
memory can be
reused. A destructor is a method that is called just prior to an object being
recycled.
10. What is this?
A: For a method, the this keyword is a reference to the object on which a method
is invoked. For a constructor, this is a reference to the object being constructed.
11. What is Jagged Arrays ?
A: A jagged array is an array of arrays in which the length of each array can differ.
Thus, a jagged array can be used to create a table in which the row lengths are
not the same.
Jagged arrays are declared by using sets of square brackets to indicate each
dimension. For example, to declare a two-dimensional jagged array, you will use
this general form:
type[ ] [ ] array-name = new type[size][ ];
Here, size indicates the number of rows in the array. The rows themselves have
not been allocated. Instead, the rows are allocated individually. This allows for the
length of each row to vary. For example, the following code allocates memory for
the first dimension of jagged when it is declared. It then allocates the second
dimensions manually.
int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[3];
jagged[2] = new int[4];
After this sequence executes, jagged looks like this:
i = i * i;
}
public void Sqr1(int j)
{
j = j * j;
}
}
class RefDemo
{
static void Main()
{
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.Sqr(ref a);
Console.WriteLine("a after call: " + a);
int b = 11;
ob.Sqr1(b);
Console.WriteLine("a Second call: " + b);
}
}
Output:
An argument passed by ref must be assigned a value prior to the call. The reason
for this is that the method that receives such an argument assumes that the
parameter refers to a valid value. Thus, using ref, you cannot use a
method to give an argument an initial value.
As you know, a return statement enables a method to return a value to its caller.
However, a
method can return only one value each time it is called. What if you need
to return two or more
pieces of information? For example, what if you want to create a method that
computes the area
of a rectangle and also determines if that rectangle is a square? To do this
requires that two pieces
of information be returned: the area and a value indicating square-ness. This
method cannot be
written using only a single return value. The out modifier solves this problem.
// Use an out parameter.
using System;
class Rectangle
{
int side1;
int side2;
public Rectangle(int i, int j)
{
side1 = i;
side2 = j;
}
// Return area and determine if square.
public int RectInfo(out bool isSquare)
{
if (side1 == side2) isSquare = true;
else isSquare = false;
return side1 * side2;
}
}
class OutDemo
{
static void Main()
{
Rectangle rect = new Rectangle(10, 23);
int area;
bool isSqr;
area = rect.RectInfo(out isSqr);
if (isSqr) Console.WriteLine("rect is a square.");
else Console.WriteLine("rect is not a square.");
Console.WriteLine("Its area is " + area + ".");
}
}
Q: Can ref and out be used on reference-type parameters, such as when passing a
reference to an object?
A: Yes. When ref or out modifies a reference-type parameter, it causes the
reference itself to be passed by reference. This allows a method to change what
the reference is referring to. Consider the following program: