Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# return 2 values

// This is C# 7 and above
public static (int, int) Add_Multiply(int a, int b){
  return (a+b,a*b);
}
static void Main(string[] args)
{
  int a=10, b=20;
  int add, multiply;
  (add,multiply) = Add_Multiply(a, b);
  Console.WriteLine(add);
  Console.WriteLine(multiply);
}
Comment

returning multiple values in C#

//Using Ref
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    int add = 0;
    int multiply = 0;
    Add_Multiply(a, b, ref add, ref multiply);
    Console.WriteLine(add);
    Console.WriteLine(multiply);
}

private static void Add_Multiply(int a, int b, ref int add, ref int multiply)
{
    add = a + b;
    multiply = a * b;
}

//Using Out
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    int add;
    int multiply;
    Add_Multiply(a, b, out add, out multiply);
    Console.WriteLine(add);
    Console.WriteLine(multiply);
}

private static void Add_Multiply(int a, int b, out int add, out int multiply)
{
    add = a + b;
    multiply = a * b;
}

//Using Struct
struct Result
{
    public int add;
    public int multiply;
}
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.add);
    Console.WriteLine(result.multiply);
}

private static Result Add_Multiply(int a, int b)
{
    var result = new Result
    {
        add = a * b,
        multiply = a + b
    };
    return result;
}

//Using Class
class Result
{
    public int add;
    public int multiply;
}
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.add);
    Console.WriteLine(result.multiply);
}

private static Result Add_Multiply(int a, int b)
{
    var result = new Result
    {
        add = a * b,
        multiply = a + b
    };
    return result;
}

//Using TupleClass
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    var result = Add_Multiply(a, b);
    Console.WriteLine(result.Item1);
    Console.WriteLine(result.Item2);
}

private static Tuple<int, int> Add_Multiply(int a, int b)
{
    var tuple = new Tuple<int, int>(a + b, a * b);
    return tuple;
}

//Using C# 7 Tuples
static void Main(string[] args)
{
    int a = 10;
    int b = 20;
    (int a_plus_b, int a_mult_b) = Add_Multiply(a, b);
    Console.WriteLine(a_plus_b);
    Console.WriteLine(a_mult_b);
}

private static (int a_plus_b, int a_mult_b) Add_Multiply(int a, int b)
{
    return(a + b, a * b);
}
Comment

c# method returns multiple values

public Tuple<int, int> GetMultipleValue()
{
     return Tuple.Create(1,2);
}
Comment

c# return two values

public void getValues( out int i, out int j)
{
  i = // output 1;
  j = // output 2;
}
Comment

c# return 2 values

public Tuple<int, int> GetMultipleValue()
{
     return Tuple.Create(1,2);
}
Comment

C# return multiple values

public async Task DeleteSchoolTask(int schoolNumber, int taskDetailId)
        {  
            var result = await GetTaskTypeAndId(taskDetailId);
            int taskId = result.Item1;
            string taskType = result.Item2;

            // step 1: delete attachment physically from server
            var fileService = new FileService(Logger, CurrentUser);
            var relativeFilePath = $"{schoolNumber}{Consts.RM_SCHOOL}{taskDetailId}";
            fileService.DeleteAttachmentFolderFromServer(Consts.CONFIG_SMP_UPLOADFILE_ROOTPATH, relativeFilePath);

            // step 2: delete records from database
            await _routineMaintenanceRepo.Value.DeleteSchoolTask(taskDetailId);
        }

        public async Task<(int, string)> GetTaskTypeAndId(int taskDetailId)
        {
            var detailRecord = await _routineMaintenanceRepo.Value.GetDetailRecord(taskDetailId);

            int taskId = 0;
            string taskType = "";

            switch (detailRecord.TaskType)
            {
                case 1:
                    taskId = (int)detailRecord.RoutineMaintenanceTaskId;
                    taskType = Consts.RM_DEFAULT;
                    break;
                case 2:
                    taskId = (int)detailRecord.RoutineMaintenanceTaskDuplicateId;
                    taskType = Consts.RM_DUPLICATE;
                    break;
                case 3:
                    taskId = (int)detailRecord.RoutineMaintenanceTaskSchoolId;
                    taskType = Consts.RM_SCHOOL;
                    break;
                default:
                    break;
            }
            return (taskId, taskType);
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make a cast in c# 
Csharp :: convert bitmap to imagesource 
Csharp :: c# if else 
Csharp :: implicit vs explicit cast c# 
Csharp :: c# square symbol 
Csharp :: c# distinct comparer multiple properties 
Csharp :: c# reflection create generic type 
Csharp :: exception is null c# 
Csharp :: c# sort llist 
Csharp :: c# collection of generic classes 
Csharp :: c# webclient vs httpclient 
Csharp :: round image unity 
Csharp :: string vs string c# 
Csharp :: c# generate insert statement from object 
Csharp :: json serialize object capitalization config 
Csharp :: system.componentmodel.dataannotations hide field 
Csharp :: Uninstall-SPSolution: This solution contains resources scoped for a Web application and must be retracted from one or more Web applications. 
Csharp :: populate array from an XML file 
Csharp :: C# HttpUtility not found / missing C# 
Csharp :: c# accept any enum 
Csharp :: ASP.NET C# Catch all exceptions in a class 
Csharp :: c# Unit Test IDbContextFactory 
Csharp :: in c# show error when user choose old datetime 
Csharp :: get datacontext of itemscontrol item c# 
Csharp :: redsymbol.net exit traps 
Csharp :: GridViewColumn url wpf 
Csharp :: get path c# application 
Csharp :: c# ile ürün çekme - htmlagilitypack 
Csharp :: c# short 
Csharp :: my context class is in different project and i want migration in different project in asp.net mvc 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =