A program can return more than one value from a method from C#7.0.
Example 1:
class Program
{
public (int Sum, int Product) Calc(int val1, int val2)
{
int val3 = val1 + val2;
int val4 = val1 * val2;
return (val3, val4);
}
static void Main()
{
Program P = new Program();
var (Sum_Result, Product_Result) = P.Calc(100, 50);
Console.WriteLine("Sum: " + Sum_Result);
Console.WriteLine("Product: " + Product_Result);
Console.Read();
}
}
Output:
Sum: 150
Product: 5000
If you notice val3 and val4 of type integer has been returned from Calc method
In the Main method we've captured val3 and val4 values into Sum_Result and Product_Result and printed them as well. .
Example 2:
class Program
{
public (int Sum, int Product) Calc(int val1, int val2)
{
int val3 = val1 + val2;
int val4 = val1 * val2;
return (val3, val4);
}
static void Main()
{
Program P = new Program();
var var_obj = P.Calc(100, 50);
Console.WriteLine("Sum: " + var_obj.Sum);
Console.WriteLine("Product: " + var_obj.Product);
Console.Read();
}
}
Output:
Sum: 150
Product: 5000
In the above example we have created a var obj by name var_obj which calls Calc method using object(P) of the class Program and passed two integer values as well.
Then printed two values using var_obj.
Example 1:
class Program
{
public (int Sum, int Product) Calc(int val1, int val2)
{
int val3 = val1 + val2;
int val4 = val1 * val2;
return (val3, val4);
}
static void Main()
{
Program P = new Program();
var (Sum_Result, Product_Result) = P.Calc(100, 50);
Console.WriteLine("Sum: " + Sum_Result);
Console.WriteLine("Product: " + Product_Result);
Console.Read();
}
}
Output:
Sum: 150
Product: 5000
If you notice val3 and val4 of type integer has been returned from Calc method
In the Main method we've captured val3 and val4 values into Sum_Result and Product_Result and printed them as well. .
Example 2:
class Program
{
public (int Sum, int Product) Calc(int val1, int val2)
{
int val3 = val1 + val2;
int val4 = val1 * val2;
return (val3, val4);
}
static void Main()
{
Program P = new Program();
var var_obj = P.Calc(100, 50);
Console.WriteLine("Sum: " + var_obj.Sum);
Console.WriteLine("Product: " + var_obj.Product);
Console.Read();
}
}
Output:
Sum: 150
Product: 5000
In the above example we have created a var obj by name var_obj which calls Calc method using object(P) of the class Program and passed two integer values as well.
Then printed two values using var_obj.
No comments:
Post a Comment