Archive

Archive for the ‘Google’ Category

Adding two numbers without using the + Operator

March 27th, 2008

This is pretty easy to accomplish as Minus Minus maketh plus.

int A = 10;
int B = 5;
Console.WriteLine(A - -B);
Console.Read();

Google

How to copy Array values by value and not by reference.

October 28th, 2007

When you try to copy the value of one Array into another, you actually end up having two references to the same array. This is because an Array is a reference type.

int[] A = { 1, 2, 3 };
int[] B = A;
B[1] = 69;
Console.WriteLine(A[1]);

The above code would give an output of 69. As you can see, changes made to the elements of array B are applied to those of array A.

To do a copy by value you need to iterate through each of the Array elements in the source array and assign the value to the corresponding index in the destination array.

1
2
3
4
5
6
7
8
int[] A = { 1, 2, 3 };
 
// Set the Size of Array B to be the same as that of Array A.
int[] B = new int[A.Length];
for (int I = 0; I < A.Length; I++)
{
    B[I] = A[I];
}

There is a much simpler way to do this using the static method Copy of the Abstract class Array.

int[] A = { 1, 2, 3 };
 
// Set the Size of Array B to be the same as that of Array A.
int[] B = new int[Arr.Length];
Array.Copy(A, 0, B, 0, Arr.Length);

The Copy method is overloaded into 4 forms with two variations:-

Array.Copy(int[] SourceArray, int[] DestinationArray, int NumberOfElementsToCopy);
Array.Copy(int[] SourceArray, int SourceIndexToStartCopyFrom,
int[] DestinationArray, int DestinationIndextToStartCopyFrom, int NumberOfElementsToCopy);

The remaining two forms are the long counterparts of these two.

Google

What is Convert.ToInt32(Console.ReadLine()) Method In C#

October 28th, 2007

The method Console.ReadLine always reads the input from the user in the form of a string. To get an integer value from the user, this string needs to be converted to Integer. The Convert.ToInt32 does this.

Example Code:-

Console.WriteLine("Please enter a number");
int Num = Convert.ToInt32(Console.ReadLine());

Google

Can Destructors Be Parameterized?

October 28th, 2007

Destructors being automatically invoked, cannot be Parameterized.

Google

Get the name of the currently executing Function/Method

August 8th, 2007

A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

Source: MSDN

1
2
3
System.Diagnostics.StackFrame sf = new System.Diagnostics.StackFrame();
System.Reflection.MethodBase mb = sf.GetMethod();
Console.WriteLine(mb.Name);

This code can be used to get the name of the current class.

string ClassName = this.GetType().Name;

Note: This code can only be called inside a non-static method/function. This is because a static method cannot put into use the instance retriever – this keyword.

Google

Default Return type of functions in C#

August 8th, 2007

There is NO default return type of functions in C#. If you don’t want to return any value, the function must be declared as void.

1
2
3
4
public void MyFunc()
{
    Console.Write("My Function");
}

Google

What statements can come with “continue” statement in C#

August 8th, 2007

The continue statement is used to pass the control to the next iteration of the loop it is in. So, it can only be used with loops.

for

1
2
3
4
5
6
7
8
for (int I = 0; I < 10; I++)
{
    if (I % 2 == 0)
    {
        continue;
    }
    Console.WriteLine(I);
}

foreach

1
2
3
4
5
6
7
8
9
int[] Numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
foreach (int Number in Numbers)
{
    if (Number % 2 == 0)
    {
        continue;
    }
    Console.WriteLine(Number);
}

while/do while

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string Name = "";
do
{
    Console.Write("Enter a Name:");
    Name = Console.ReadLine();
    if (Name == "Parastratiosphecomyia stratiosphecomyioides")
    {
        // The animal with the longest Scientific Name???
        continue;
    }
    if(Name != "")
        Console.WriteLine("{0} is a good name", Name);
 
} while (Name != "");

Google