传地址和传值

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace StaticTest
{
    public class MyClass
    {
        public int Value;
    }

    public static class TestClass
    {
        public static void Test1(MyClass myClass)
        {
            myClass.Value = 1;
            HttpContext.Current.Response.Write(myClass.Value.ToString() + ",");
        }

        public static void Test2(ref MyClass myClass)
        {
            myClass = new MyClass();
            myClass.Value = 2;
            HttpContext.Current.Response.Write(myClass.Value.ToString() + ",");
        }

        public static void Test3(MyClass myClass)
        {
            myClass = new MyClass();
            myClass.Value = 3;
            HttpContext.Current.Response.Write(myClass.Value.ToString() + ",");
        }

        public static void Test4(string str)
        {
            str = "4";
            HttpContext.Current.Response.Write(str + ",");
        }

        public static void Test5(ref string str)
        {
            str = "5";
            HttpContext.Current.Response.Write(str + ",");
        }

        public static void Test6(out string str, string str2)
        {
            str = str2;
            HttpContext.Current.Response.Write(str + ",");
        }

        public static void Test7(int integer)
        {
            integer = 6;
            HttpContext.Current.Response.Write(integer.ToString() + ",");
        }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MyClass myClass = new MyClass();
            myClass.Value = 0;
            string str = "0";
            int integer = 0;

            TestClass.Test1(myClass);
            Response.Write(myClass.Value.ToString() + ",");
            TestClass.Test2(ref myClass);
            Response.Write(myClass.Value.ToString() + ",");
            TestClass.Test3(myClass);
            Response.Write(myClass.Value.ToString() + ",");

            TestClass.Test4(str);
            Response.Write(str + ",");
            TestClass.Test5(ref str);
            Response.Write(str + ",");
            TestClass.Test6(out str, "8");
            Response.Write(str.ToString() + ",");
            TestClass.Test7(integer);
            Response.Write(integer.ToString());
        }
    }
}

你可能感兴趣的:(传地址和传值)