Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

How to round off decimal to two decimal places in C# ?

There are 3 ways to round off decimal to two decimal places :

1. Use ToString()
 double  d = 2.445;
 d.ToString("0.00")

Output : 2.45

2. Use Math.Round

double d = 2.445;

Math.Round(d, 2)

Output : 2.44

3. Create a Custom logic method like below :

N = 2.445 I want to show "2.45" only.

Step a. N*100 + 0.5 = 245
Step b. Divide System.Math.Floor(245) by 100
Step c. The result will be 2.45

What is params keyword in C# ?

The params keyword lets you specify a method parameter that takes a variable number of arguments.

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments

Example :

public static void UseParams(params int[] list)

{

for (int i = 0; i < list.Length; i++)

{

Console.Write(list[i] + " ");

}

Console.WriteLine();

}

We can call above method

Way 1 :

// You can send a comma-separated list of arguments of the specified type.

UseParams(1, 2, 3, 4);

Way 2 :

// An array argument can be passed, as long as the array


// type matches the parameter type of the method being called.

int[] myIntArray = { 5, 6, 7, 8, 9 };

UseParams(myIntArray);

Way 3:
UseParams();

What is CharEnumerator in C# ?

CharEnumerator is an object in C# that can be used to enumerate through all the characters of a string.

Sample Code :

string str = "santosh";

CharEnumerator chs = str.GetEnumerator();
while(chs.MoveNext())
{
Response.Write(chs.Current);
}

Difference between Static and Instance Constructor ?

The static constructor executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:
• An instance of the class type is created.
• Any of the static members of the class type are referenced.
where as
Instance constructor can execute more than once.
Instance constructor executes when an instance of the class type is created.

What is use of private constructor in C# ?

If a class contains only static members and isn’t intended to be instantiated, adding an empty private instance constructor will prevent instantiation.

State whether the C# code : var z = null; is okay or not ?

Above code would give an error as null does not have type

Question : State whether below code would give compilation error or not ?

delegate string Delegate1(object o);
delegate object Delegate2(string s);
static string Dispaly(object o) {
return "Display";
}
static void Main(string[] args)
{
Delegate1 d1 = Display;
Delegate2 d2 = Display;
}

Answer :
It will give no compilation error .

Question : State whether below overload methods will give compilation erro or not ?

Class Test
{
void SetX(ref int x)
{
x = 1;
}
void SetX(out int x)
{
X = 2;
}
}

Answer :
Gives a compilation error.
SetX method cannot define overloaded methods that differ only on ref and out

Question : Whether b.x =1 statement in class A and class B is okay or not ?

public class A
{
int x;
static void SetX(B b)
{
b.x = 1; }
}
public class B : A
{
static void SetX(B b)
{
b.x = 1;
}
}

Answer :
b.x = 1 in Class A is fine.
b.x = 1 in Class B gives compilation error

Help :
As x is private field in Class A hence accessible within Class A
As x is private field in Class A and hence not accessible in Class B

Can namespace declaration have access modifiers ?

No . Namespace declaration cannot have access modifiers.

Help

Namespaces implicitly have public declared accessibility. No access modifiers and attributes are allowed on namespace declarations

Is static void Main(int a) {...} a valid entry point for C# program ?

Answer : No

Hint :

Entry point method is always named Main, and can have one of the following signatures:


static void Main() {...}

static void Main(string[] args) {...}

static int Main() {...}

static int Main(string[] args) {...}

The entry point may optionally have one formal parameter. The parameter may have any name, but the type of the parameter must be string[].

How to create read only property in C# ?

A property that has only a get accessor is a read-only property .

Example :
public int Count {


get { return count; }

}

public int Capacity {

get {

return items.Length;

}
}

What are parameter array ? How is parameter array declared ?

A parameter array allows variable number of arguments to be passed to a method. A parameter array is declared with the params modifier. Only the last parameter of a method can be a parameter array, and the type of a parameter array must be a single-dimensional array type.

Example :

public static void DisplayEmployeeDetails(string name, params object[] args) {...}

State different types of method parameters in C# ?

Different type of method parameters in C# are :

 
  1. Value Parameter
  2. Reference Parameter
  3. Output Parameter

How are indexer declared ?

Below is indexer declaration :
modifier return type this [argument list]
{
get
{
// Get codes goes here
}
set
{
// Set codes goes here
}
}
Where the modifier can be private, public, protected or internal. The return type can be any valid C# types. The 'this' is a special keyword in C# to indicate the object of the current class. The formal-argument-list specifies the parameters of the indexer. The formal parameter list of an indexer corresponds to that of a method, except that at least one parameter must be specified, and that the ref and out parameter modifiers are not permitted. Remember that indexers in C# must have at least one parameter. Other wise the compiler will generate a compilation error.

Difference between indexer and property ?

1. An indexer is identified by it's signature. But a property is identified it's name.

2. An indexer is always an instance member, but a property can be static also.

3. An indexer is accessed through an element access. But a property is through a member access.
Question :

State whether below class relationship is correct or not . If not why ?

class A: B {}

class B: C {}
class C: A {}

Answer :

Above releationship is incorrect as it has circular relationship. This would result in compile-time error.

State two importanrt properties of System.Exception Class ?

Two properties of System.Exception Class are :
  1. Message
  2. InnerException

How to instantiate and invoke static and instance method using delegates ?

Refer below code to understand :
using System;

delegate void DelMethod(int x); // This is the delegate declaration
// Example is Class containing static method and instance method to be instantiated
class Example
{
public static void StaticMethod(int i) {
Console.WriteLine("M1: " + i);
}
public void InstanceMethod(int i) {
Console.WriteLine("M3: " + i);
}
}
// Below class contains code to instantiate and invoke static and instance method using delegates
class Test
{
static void Main() {
DelMethod cd1 = new DelMethod (Example.StaticMethod); // Instantiate Static Method delegate
cd1(-1); // Invoke Static Method

Example c = new Example ();
DelMethod cd2 = new DelMethod (c.InstanceMethod); // Instantiate InstanceMethod delegate

cd2(5); // Invoke Instance Method
}
}

Which interfaces to use or implement when sorting custom arrays ?

Sorting in Custom Arrays can be acheived using one of following interfaces :
  1. IComparable
  2. IComparer

Popular Posts