Posted on 01 April 2013.
Note: The real advantage of interface is not for supporting multiple inheritance.
Advantage1: We need to declare an interface when we have series of classes which are not having common behavior, still if you want to mandate a method name along with signature and return type and if you want to leave the implementation to the developers of the class.
Advantage2: Interfaces acts as contracts between two developers ( why or how this feature is helpful ?)
Explanation: Assume Developer D1 writing code in class A, this class A requires to call some of the methods present in Class B. Assume class B’s code has to be written by Developer D2. Assume some delay in writing the code happening from Developer D2. In this situation Developer D1 ccanot be able to complete his code since class A’s code is depending on the Class B’s Code. This kind of dependencied between developers code we can avoid by using interfaces .
Q:How to avoid Dependency?
Ans: Developer 1 D1 has to create an interface I1 with the required method names and signatures along with return types which D1 wanted to use present in Class B, then ask Developer 2 D2 to implement interface I1 in class B.
Public inteface I1
{
int M1(int x,int y);
}
Public class A
{
public int M2(I1 i1)
{
//wrie some code and call inteface i1′s method M1.
i1.M1(10,20);
}
}
Note: In the above sample D2 has to call the method present in class A by sending his class object as parameter to the method M2.
Posted in .Net Tutorials, C# FAQ, Java
Posted on 06 July 2012.
1. ReferenceEquals and Equals methods behaves same for Reference types but it differs for value types.
2.ReferenceEquals is a Static method and hence we cannot override this method where as Equals method we can override this method.
Ex1:
bool b=0.Equals(0);
bool b1=Object.ReferenceEquals(0, 0);
Response.Write(b);
Ex2:
//Write this code in the main method.
Palle p1 = new Palle(2);
Palle p2 = new Palle(2);
bool b1=p1.Equals(p1);
bool b2=object.ReferenceEquals(p1, p2);
//Both b1 & b2 will be false here
public class Palle
{
public int age;
public Palle(int age)
{
this.age = age;
}
}
Posted in .NET Framework FAQ, C# FAQ
Posted on 20 February 2012.
Ternary operators can be use full when we have only one simple if else condition.
Ternary Operator Syntax:
condition ? first_expression : second_expression;
Ex:
public class TernaryEx { public int GetBigNum(int a, int b) { return a > b ? a : b; //in the above syntax if a is greater than b //the above expression returns a else it returns b. } }
Note: SignUp for JobGuaranteed Training
Posted in C# FAQ
Posted on 20 February 2012.
is Operator:
- is operator Checks if an object is compatible with a given type
- An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.
- The as operator is used to perform conversions between compatible reference types
- The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception
public string GetSubjectName()
public int GetTestNumber()
public partial class CSHARPSAMPLES_Keywords : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
public void Test(object o)
UnitTest u = o as UnitTest;
Posted in C# FAQ
Posted on 20 February 2012.
protected void Page_Load(object sender, EventArgs e)
{
object s1 = “abc”;
object s2 = “abc”;
if (s1 == s2)
{
Response.Write(“value of s1 and s2 are equal”);
}
else if(s1.Equals(s2))
{
Response.Write(“Address of s1 and s2 are equal”);
}
/***************************************************************
* 1.==compares values of the variables.
* 2.Object.Eqals Method compares address of the variables
* **************************************************************/ }
Author: Raghavendra
Click here for job guarantee courses in bangalore
Posted in C# FAQ
Posted on 30 November 2010.
See the below example to understand the difference between post incrementation and pre incrementation.
int
x = 10;int z;//First assigns value of x (10) to z and then x value incremented to 11.
z=x++;MessageBox.Show(“Post Incement of x=” + z);int y = 20;int a;
//First increments value of y to (21) and assigns 21 to as.a=++y;
MessageBox.Show(“Pre incrementor Y=”+a);
Posted in C# FAQ
Posted on 30 October 2010.
1.C# is not allowing the multiple inheritance because of the below reason.
public class A
{
public virtual int m()
{
return 10;
}
}
public class B : A
{
public override int m()
{
return 20;
}
}
public class C : A
{
public override int m()
{
return 30;
}
}
public class D : B, C
{
//If C# supports multiple inheritence.
//then from which class ( B or C) the method will be inherited
//This is where compiler gets confusion.
//that is the reason why C# not supporting multiple inheritence.
public void test()
{
}
}
Posted in C# FAQ
Posted on 28 September 2010.
Consider the below example:
public class Mathematics
{
public int DivisionResult(int x, int y)
{
try
{
return x / y;
}
catch (Exception)
{
return -1;
}
Console.WriteLine(“Will this line be executed or not?”);
}
}
Question: In the above sample the code in Console.WriteLine will be executed or not?
Ans: No it wont since because of the return statement in the try and catch blocks.
If you want to execute some line of code always or at any point of time even if you have return codes. Put your code in finally block. This ensures that your code in finally block is always executed irrespective of written codes.
Posted in C# FAQ
Posted on 28 September 2010.
Order of Catch blocks is very importent while implementing the Catch Blocks.For Example if my code is expected to throw DivideByZeroException , If you know the exception type properly you have to write the specifice expected exception then write generic exception. See below example
Ex:
public class Mathematics
{
public int DivisionResult(int x, int y)
{
try
{
return x / y;
}
catch (DivideByZeroException de)
{
return -0;
}
catch (Exception)
{
return -1;
}
}
}
Note1: In the above example if you change the order of Catch you will get compile time error “A previous catch clause already catches all exceptions of this or of a super type (‘System.Exception’)”. This because of all the exception can be caught by the generic catchblock.
Note2: How many catch blocks can i have for a signgle try block . Answer is no limit.
Note3 : Can i have a try without catch. Ans: a try should always either have a catch or finally. try will never exist alone ( try cannot be a bachelor ).
Posted in C# FAQ
Posted on 28 September 2010.
public abstract class Surgeon
{
public abstract int Sal{get;set;}
}
public class SeniorSurgeon : Surgeon
{
private int _sal;
public override int Sal
{
get { return _sal; }
set { _sal = value; }
}
}
Posted in C# FAQ
Latest Comments