C#中的值引數,引用引數及輸出引數

iamzxf發表於2015-03-20


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace paraDemo
{
    class samplePar {

        public void swap1(int x, int y)
        {
            int z = x;
            x = y;
            y = z;
        }

        public void swap2(ref int x, ref int y)
        {
            int z = x;
            x = y;
            y = z;
        }

        public void add(int x, int y, out int z)
        {
            z = x + y;
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            int a = 1, b = 2;
            samplePar smp = new samplePar();
            smp.swap1(a, b);
            Console.WriteLine("{0},{1}",a,b);

            smp.swap2(ref a, ref b);
            Console.WriteLine("{0},{1}", a, b);

            int z;
            smp.add(a, b, out z);
            Console.WriteLine(z);
            Console.ReadLine();

        }
    }
}



相關文章