C# Interview Questions on Methods / Functions
Is the following code legal?
using System;
namespace Demo
{
class Program
{
public static void Main()
{
}
public void Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
public int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
}
}
}
No, The above code does not compile. You
cannot overload a method based on the return type. To overload a method in C#
either the number or type of parameters should be different. In general the
return type of a method is not part of the signature of the method for the
purposes of method overloading. However, it is part of the signature of the
method when determining the compatibility between a delegate and the method that
it points to.
What is the difference between method
parameters and method arguments. Give an example?
In the example below FirstNumber and
SecondNumber are method parameters where as FN and LN are method arguments. The
method definition specifies the names and types of any parameters that are
required. When calling code calls the method, it provides concrete values
called arguments for each parameter. The arguments must be compatible with the
parameter type but the argument name (if any) used in the calling code does not
have to be the same as the parameter named defined in the method.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int FN = 10;
int SN = 20;
//FN and LN are method arguments
int Total = Sum(FN, SN);
Console.WriteLine(Total);
}
//FirstNumber and SecondNumber are method parameters
public static int Sum(int FirstNumber, int SecondNumber)
{
int Result = FirstNumber + SecondNumber;
return Result;
}
}
}
Explain the difference between passing
parameters by value and passing parameters by reference with an example?
We can pass parameters to a method by value
or by reference. By default all value types are passed by value where as all
reference types are passed by reference. By default, when a value type is
passed to a method, a copy is passed instead of the object itself. Therefore,
changes to the argument have no effect on the original copy in the calling
method.An example is shown below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
int K = Function(I);
Console.WriteLine("I = " + I);
Console.WriteLine("K = " + K);
}
public static int Function(int Number)
{
int ChangedValue = Number + 1;
return ChangedValue;
}
}
}
By default, reference types are passed by
reference. When an object of a reference type is passed to a method, the
reference points to the original object, not a copy of the object. Changes made
through this reference will therefore be reflected in the calling method.
Reference types are created by using the class keyword as shown in the example
below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
ReferenceTypeExample Object = new ReferenceTypeExample();
Object.Number = 20;
Console.WriteLine("Original Object Value = " + Object.Number);
Function(Object);
Console.WriteLine("Object Value after passed to the method= "
+ Object.Number);
}
public static void Function(ReferenceTypeExample ReferenceTypeObject)
{
ReferenceTypeObject.Number = ReferenceTypeObject.Number + 5;
}
}
class ReferenceTypeExample
{
public int Number;
}
}
Can you pass value types by reference to a
method?
Yes, we can pass value types by by
reference to a method. An example is shown below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
int I = 10;
Console.WriteLine("Value of I before passing to the method = "
+ I);
Function(ref I);
Console.WriteLine("Value of I after passing to the method by
reference= " + I);
}
public static void Function(ref int Number)
{
Number = Number + 5;
}
}
}
If a method's return type is void, can you
use a return keyword in the method?
Yes, Even though a method's return type is
void, you can use the return keyword to stop the execution of the method as
shown in the example below.
using System;
namespace Demo
{
class Program
{
public static void Main()
{
SayHi();
}
public static void SayHi()
{
Console.WriteLine("Hi");
return;
Console.WriteLine("This statement will never be executed");
}
}
}
--------------
C# Interview Questions on Properties
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.
In the example below _firstName and
_lastName are private string variables which are accessible only inside the
Customer class. _firstName and _lastName are exposed using FirstName and LastName
public properties respectively. The get property accessor is used to return the
property value, and a set accessor is used to assign a new value. These
accessors can have different access levels. The value keyword is used to define
the value being assigned by the set accessor. The FullName property computes
the full name of the customer. Full Name property is readonly, because it has
only the get accessor. Properties that do not implement a set accessor are read
only.
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.
using System;
class Customer
{
//
Private fileds not accessible outside the class.
private string _firstName = string.Empty;
private string _lastName = string.Empty;
private string _coutry = string.Empty;
//
public FirstName property exposes _firstName variable
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
//
public LastName property exposes _lastName variable
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
//
FullName property is readonly and computes customer full name.
public string FullName
{
get
{
return _lastName + ", " + _firstName;
}
}
//Country Property is Write Only
public string Country
{
set
{
_coutry = value;
}
}
}
class MainClass
{
public static void Main()
{
Customer CustomerObject = new Customer();
//This line will call the set accessor of FirstName Property
CustomerObject.FirstName = "David";
//This line will call the set accessor of LastName Property
CustomerObject.LastName = "Boon";
//This line will call the get accessor of FullName Property
Console.WriteLine("Customer Full Name is : " +
CustomerObject.FullName);
}
}
Explain the 3 types of properties in C#
with an example?
1. Read Only Properties: Properties without
a set accessor are considered read-only. In the above example FullName is read
only property.
2. Write Only Properties: Properties
without a get accessor are considered write-only. In the above example Country
is write only property.
3. Read Write Properties: Properties with
both a get and set accessor are considered read-write properties. In the above
example FirstName and LastName are read write properties.
What are the advantages of properties in
C#?
1. Properties can validate data before
allowing a change.
2. Properties can transparently expose data
on a class where that data is actually retrieved from some other source such as
a database.
3. Properties can take an action when data
is changed, such as raising an event or changing the value of other fields.
What is a static property. Give an example?
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
{
private static double _pi = 3.14;
public static double PI
{
get
{
return _pi;
}
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine(Circle.PI);
}
}
What is a virtual property. Give an
example?
A property that is marked with virtual
keyword is considered virtual property. Virtual properties enable derived
classes to override the property behavior by using the override keyword. In the
example below FullName is virtual property in the Customer class. BankCustomer
class inherits from Customer class and overrides the FullName virtual property.
In the output you can see the over riden implementation. A property overriding
a virtual property can also be sealed, specifying that for derived classes it
is no longer virtual.
using System;
class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
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";
Console.WriteLine("Customer Full Name is : " +
BankCustomerObject.FullName);
}
}
What is an abstract property. Give an
example?
A property that is marked with abstract
keyword is considered abstract property. An abstract property should not have
any implementation in the class. The derived classes must write their own
implementation. In the example below FullName property is abstract in the
Customer class. BankCustomer class overrides the inherited abstract FullName
property with its own implementation.
using System;
abstract class Customer
{
private string _firstName = string.Empty;
private string _lastName = string.Empty;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
}
}
//
FullName is abstract
public abstract string FullName
{
get;
}
}
class BankCustomer : Customer
{
//
Overiding the FullName abstract 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";
Console.WriteLine("Customer Full Name is : " +
BankCustomerObject.FullName);
}
}
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.
No comments:
Post a Comment