What is the difference between “int” and “Int32” in C#?
for more .net and c# interview questions videos click on .NET and c# interview questions
C# and .NET step by step with interview questions
class Program
{
static void Main(string[] args)
{
int y = Add(10, 20);
Console.WriteLine("The Addition of two Numbers is : " + y);
Console.ReadLine();
}
static int Add(int Num1, int Num2)
{
return Num1 + Num2;
}
}
In the above code snippet you can see that I have created a function “Add”
which takes two parameters as “Num1” and “Num2” and return the addition of two numbers. The value for the
parameters are been passed from the Main and it simply print the output to the screen of the “y” variable. class Program
{
static void Main(string[] args)
{
//Passed 5 different values.
int y = Add(10,20,30,40,50);
Console.WriteLine("The Addition of Numbers are : " + y);
Console.ReadLine();
}
//Created a listNumbers variable as declared as Params.
static int Add(params int[] listNumbers)
{
int Total = 0;
foreach (int i in listNumbers)
{
Total = i + Total;
}
return Total;
}
}
for more .net and c# interview questions videos click on .NET and c# interview questions
class Program
{
//created a delegate.
delegate void PointToCallMe(string str);
static void Main(string[] args)
{
//Delegate,which points to the CallMe Function.
PointToCallMe objDelegate = new PointToCallMe(CallMe);
objDelegate.Invoke("Called from Delegate");
Console.ReadLine();
}
public static void CallMe(string str)
{
Console.WriteLine(str);
}
}
In the above code snippet you can see that I have created a function CallMe with a string parameter “str” and I have created a delegate which points toward the CallMe function. class Program
{
//created a delegate.
delegate void PointToCallMe(string str);
static void Main(string[] args)
{
//Lambda Expression.
PointToCallMe objLambda = str => Console.WriteLine(str);
objLambda.Invoke("Called from Lambda");
Console.ReadLine();
}
}
Now, in the above code snippet you can see that I have just eliminated the extra created CallMe function and just written a few line of Lambda Expression, which makes your code more readable and understandable with a few line of code.