C#語法學習三(Method)

//簡單方法的應用,沒有帶參數
using System;
namespace Athrun
{
    
class Method
    {
        
public static void MyMethod()
        {
            Console.WriteLine(
"this is a method");
        }
        
static void Main()
        {
            MyMethod();
        }
    }
}
 
//有返回值的方法
using System;
namespace Athrun
{
    
class Method
    {
        
public static DateTime MyMethod()
        {
            
return DateTime.Now;
        }
        
static void Main()
        {
            Console.WriteLine(
"Now time is {0}",MyMethod());
        }
    }
}
 
//帶參數的方法
using System;
namespace Athrun
{
    
class Method
    {
        
public static void MyMethod(string aName)
        {
            Console.WriteLine(
"this Name is " + aName + "\n");
        }
        
static void Main()
        {
            
string s=" 帶參數的方法";
            MyMethod(s);
        }
    }
}
 
//帶參數的方法
using System;
namespace Athrun
{
    
class Method
    {
        
public static void MyMethod(string aName)
        {
            Console.WriteLine(
"this Name is " + aName + "\n");
        }
        
static void Main()
        {
            
string TempName="";
            
while (TempName!="end")
            {
                TempName
=Console.ReadLine();
                MyMethod(TempName);
            }
        }
    }
}
//帶參數的方法
using System;
namespace Athrun
{
    
class Method
    {
        
public static int add(int i,int j)
        {
            
return i+j;
        }
        
static void Main()
        {
            
int i=5;
            
int j=3;
            Console.WriteLine(
"i+j=" + add(i,j));
        }
    }
}
 
 

using System;
namespace Athrun
{
    
class Method
    {
        
class A
        {
            
//類中的靜態成員變量在各個實例中是共享的

            //如果去掉static變成public int i=0;則程序會出錯,"非靜態的字段,方法,屬性"也就是說非靜態的不能直接用類來訪問,只能用實例來訪問.
            public static int i=0;
            
public void addi()
            {
                i
=i+1;
            }
        }
        
static void Main()
        {
            A a
=new A();
            a.addi();
            A b
=new A();
            b.addi();
            Console.WriteLine(A.i);
        }
    }
}

 
 
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/26
 * Time: 下午 07:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
//方法的參數傳遞有三個不同的方式:值傳遞,引用傳遞,輸出參數傳遞
/*
引用傳參和輸出參數都是以引用類型來傳遞其地址的,這兩者唯一不同的是
 * 用輸出參數時必需在方法內對數數進行初始化,而用引用方式時必須在調用外
 * 對參數進行初始化.
 * 
 * 
*/
 
 
using System;
class Method
{
    
//值傳遞方式
    public static void ValueMethod(int i)
    {
        i
++;
    }
    
//引用傳參方式
    public static void RefMethod(ref int i)
    {
        i
++;
    }
    
//帶輸出參數的方法
    public static void OutMethod(out int i)
    {
        i
=0;
        i
++;
    }    
    
    
static void Main()
    {
        
int i=0;
        ValueMethod(i);
        Console.WriteLine(
"i={0}",i);
        
int j=0;
        RefMethod(
ref j);
        Console.WriteLine(
"j={0}",j);
        
int k;
        OutMethod(
out k);
        Console.WriteLine(
"k={0}",k);
    }
}
/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/26
 * Time: 下午 07:47
 * 可變數量參數的方法
 *                    值類型                                 引用類型
 * 變量中存放         真正的數據                             指向數據的指針
 * 內存空間分配       椎棧(stack)                            托管堆中(managed heap)
 * 內存需求           一般來說輸少                           較大
 * 執行效能           較快                                   較慢
 * 內存釋放時間點     執行超過定議變量的作用域               由回收站負責回收
 * 可以為null         不可                                   可以
 * 特定條件中值類型需要進行裝箱和拆箱的操作這樣會影響它的性能.
 * 字符串也是一個引用類型的,但字符串有所不同,字符串是一個新對像.
 * 新對像最大的一個特點就是它是不可變的

 * 
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
 
using System;
 
class Method
 {
     
static void setstr(string s)
     {
         s
="123456789";
     }
     
static int addi(params int[] values)
     {
         
int sum=0;
         
foreach (int i in values)
             sum
+=i;
         
return sum;
     }
     
//數組是引用類型
     static void printarr(int[] arr)
     {
         
for(int i=0;i<arr.Length;i++)
             arr[i]
=i;
     }
     
static void Main()
     {
//         int[] arr=new int[]{100,200,300,400,500,600,700,800,900,1000};
//         printarr(arr);
//         foreach (int i in arr)
//             Console.WriteLine(i+", ");
             
//Console.WriteLine(addi(a));
             string s="3333";
             setstr(s);
             Console.WriteLine(s);
     }
 }

posted @ 2008-08-27 09:21  Athrun  阅读(371)  评论(1编辑  收藏  举报