2013年9月22日 星期日

[C#基礎]-C# class static variable

[C#基礎]-C# class static variable測試範例



今天在爬文時,赫然發現有人在討論class static variable,馬上發揮(C/P)達人本能備份一下,有興趣的同好歡迎來(C/P)一下。









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

namespace
class_static_variable
{

class
static_variable
{


public static
int count;
public
int number;
public
static_variable()
{

//count++;
number = 0;
number++;
count++;
}

public
void showData()
{

Console.WriteLine("count:\t"+count);
Console.WriteLine("number:\t"+number);

int
count_d = 0;//static int count_d=0;
count_d--;
Console.WriteLine("count_d:\t"+count_d);
}

//private static int pricount;
}
class
Program
{

static
void Main(string[] args)
{

static_variable.count = 0;

static_variable a= new static_variable();
a.showData();

static_variable b= new static_variable();
//a.showData();
b.showData();

static_variable c = new static_variable();
c.showData();

Console.ReadKey(true);
}
}
}


 


 


1 則留言:

  1. 剛才找到的相關資料,和大家分享

    Does C# support the use of static local variables?
    資料來源:http://stackoverflow.com/questions/2393156/does-c-sharp-support-the-use-of-static-local-variables
    ////////////////////////////////////////////////////

    No, C# does not support this. You can come close with:

    static System.Text.RegularExpressions.Regex re =
    new System.Text.RegularExpressions.Regex("\\(copy (\\d+)\\)$");
    private static string AppendCopyToFileName(string f)
    {

    }


    The only difference here is the visibility of 're'. I don't think C or Java will behave any different with regards to when this is initialized.

    回覆刪除