Adding two numbers without using the + Operator
This is pretty easy to accomplish as Minus Minus maketh plus.
int A = 10; int B = 5; Console.WriteLine(A - -B); Console.Read();
This is pretty easy to accomplish as Minus Minus maketh plus.
int A = 10; int B = 5; Console.WriteLine(A - -B); Console.Read();
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.
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());
Destructors being automatically invoked, cannot be Parameterized.
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.
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"); } |
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.
1 2 3 4 5 6 7 8 | for (int I = 0; I < 10; I++) { if (I % 2 == 0) { continue; } Console.WriteLine(I); } |
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); } |
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 != ""); |