开放的编程资料库

当前位置:我爱分享网 > C#教程 > 正文

C#变量

C#变量教程展示了如何在C#中使用变量。C#教程是C#语言的综合教程。

变量是一个存储位置。变量具有名称和数据类型。数据类型决定了哪些值可以分配给变量,例如整数、字符串或布尔值。随着时间的推移,程序变量可以获得相同数据类型的各种值。

在可以对变量进行任何引用之前,变量总是被初始化为其类型的默认值。

变量名称必须遵守有效C#标识符的规则。变量名必须以字母或_开头。它们可能包含Unicode字母字符、十进制数字字符、Unicode连接字符、Unicode组合字符或Unicode格式化字符。变量名称区分大小写。

C#变量示例

在第一个例子中,我们定义了几个变量。

string city = "Berlin";
string name = "Peter"; int age = 36;
string nationality = "German";

Console.WriteLine(city);
Console.WriteLine(name);
Console.WriteLine(age);
Console.WriteLine(nationality);

city = "London";
Console.WriteLine(city);

在示例中我们有四个变量。

string city = "Berlin";

我们声明了一个字符串类型的城市变量,并将其初始化为“纽约”值。

string name = "Peter"; int age = 36;

我们声明并初始化另外两个变量。我们可以在一行中放置两个语句。但出于可读性原因,每条语句应单独一行。

Console.WriteLine(city);
Console.WriteLine(name);
Console.WriteLine(age);
Console.WriteLine(nationality);

我们将变量的值打印到终端。

city = "London";

我们为city变量分配一个新值。

$ dotnet run
Berlin
Peter
36
German
London

C#变量-var关键字

可以使用var关键字隐式键入变量。变量始终是强类型的,但是对于var,C#编译器会从赋值的右侧推断出类型。

var name = "Lucia";
var age = 27;

Console.WriteLine($"{name} is {age} years old");

name = "Robert";
age = 42;

Console.WriteLine($"{name} is {age} years old");

Console.WriteLine(name.GetType());
Console.WriteLine(age.GetType());

在示例中,我们有两个隐式类型变量。

var name = "Peter";
var age = 23;

在赋值的左边我们使用var关键字。name变量是string类型,ageint。类型是从赋值的右侧推断出来的。

Console.WriteLine(name.GetType());
Console.WriteLine(age.GetType());

我们使用GetType确定变量的类型。

$ dotnet run
Lucia is 27 years old
Robert is 42 years old
System.String
System.Int32

C#局部变量

局部变量是作用域在声明它的块内的变量。局部变量可以在函数、try语句、switch语句或for和foreach语句中定义。

Console.WriteLine(Repeat(0, "hey"));
Console.WriteLine(Repeat());
Console.WriteLine(Repeat(4));
Console.WriteLine(Repeat(5, "cau"));

string Repeat(int times = 0, string message = "")
{
    int _times = 3;
    string _message = "hello";

    if (times == 0 && string.IsNullOrEmpty(message)) {

        return string.Join(" ", Enumerable.Repeat(_message, _times));
    } else if (times == 0 && !string.IsNullOrEmpty(message)) {

        return string.Join(" ", Enumerable.Repeat(message, _times));
    } else if (times != 0 && string.IsNullOrEmpty(message)) {

        return string.Join(" ", Enumerable.Repeat(_message, times));
    } else if (times != 0 && !string.IsNullOrEmpty(message)) {

        return string.Join(" ", Enumerable.Repeat(message, times));
    } else {

        return String.Empty;
    }
}

在示例中,我们定义了两个局部变量:_times_message。它们的有效性受Repeat函数的大括号限制。

$ dotnet run
hey hey hey
hello hello hello
hello hello hello hello
cau cau cau cau cau

C#实例变量

实例变量存在于所创建类的实例中。当不再有对该实例的引用时,它就不再存在。

每个实例变量在每个创建的对象中都是唯一的。

var u1 = new User { Name = "John Doe", Occupation = "gardener" };
Console.WriteLine(u1);

var u2 = new User { Name = "Roger Roe", Occupation = "accountant" };
Console.WriteLine(u2);

class User
{
    public User() {}

    public string Name { set; get; }
    public string Occupation { set; get; }

    public override string ToString()
    {
        return $"{Name} is a(n) {Occupation}";
    }
}

我们有User类,它有两个实例变量:NameOccupation。这两个变量在创建的两个类实例中是不同的。

$ dotnet run
John Doe is a(n) gardener
Roger Roe is a(n) accountant

C#静态变量

类的所有实例共享一个静态变量值。无论创建了多少个类实例,静态成员都只存在一个副本。

var counter1 = new Counter();
counter1.f();
counter1.f();
counter1.f();

var counter2 = new Counter();
counter2.f();
counter2.f();
counter2.f();

var ret1 = counter1.show();
var ret2 = counter2.show();

Console.WriteLine(ret1);
Console.WriteLine(ret2);

class Counter
{
    static int count = 0;

    public void f()
    {
        count++;
    }

    public string show() {

        return $"# of calls: {count}";
    }
}

我们有一个count静态成员变量,定义在Counter类中。计数器通过f方法递增。

var counter1 = new Counter();
counter1.f();
counter1.f();
counter1.f();

我们创建一个计数器对象并调用f方法三次。

var counter2 = new Counter();
counter2.f();
counter2.f();
counter2.f();

我们创建另一个计数器对象并调用它的f方法三次。

$ dotner run
$ of calls: 6
$ of calls: 6

从输出中我们可以看到两个对象共享静态count变量。

C#变量-函数参数

值参数在调用参数所属的函数、成员函数或匿名函数时出现。它使用调用中给出的参数值进行初始化。

int r = Add(5, 6);
Console.WriteLine(r);

int r2 = Add(11, 12);
Console.WriteLine(r2);

int Add(int x, int y) {

    return x + y;
}

在示例中,我们有两个函数参数:xy。它们在Add函数的范围内有效。

$ dotnet run
11
23

C#变量-函数引用参数

ref修饰符声明的参数是引用参数。引用参数不会创建新的存储位置;它表示与作为参数给出的变量相同的存储位置。

int x = 5;
int y = 6;

Console.WriteLine(x);
Console.WriteLine(y);

Change(ref x, ref y);

Console.WriteLine("--------------------");

Console.WriteLine(x);
Console.WriteLine(y);


void Change(ref int x, ref int y) {

    x = 15;
    y = 16;
}

Change函数中,现在创建了新变量。而是传递对现有xy变量的引用。

$ dotnet run
5
6
--------------------
15
16

在本文中,我们使用了C#中的变量。

列出所有C#教程。

未经允许不得转载:我爱分享网 » C#变量

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏