一点疑问:关于static 和static readonly

博客围绕程序中字段声明为static readonly的赋值问题展开。指出此类字段在构造函数里赋值无意义,应放在静态构造函数中。还通过程序示例展示不同代码下的输出结果,如将代码修改后输出结果改变,同时对静态字段不必放在静态构造函数中的情况提出疑问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先看一个程序:

None.gifusing System;
None.gif
None.gif
class Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public const int a=100;
InBlock.gif    
public readonly int b;
InBlock.gif    
public static readonly int c=300;
InBlock.gif
InBlock.gif    Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        b
=200;
InBlock.gif        
//c=300;  错误:不能在构造函数里被赋值
ExpandedSubBlockEnd.gif
    }

InBlock.gif
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Test e
=new Test ();
InBlock.gif    
InBlock.gif        Console.WriteLine (
"{0},{1},{2}",a,e.b ,c);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

一个字段被声明为static readonly,则不可以在构造函数里被赋值

我的理解是一个字段被声明为静态的(static),则它只能实类名而不是对象名来访问
构造函数是在实例化一个类的时候被调用,而对于静态的字段,放在构造函数里是没有意义的.
解决办法是把它放在静态构造函数中.

None.gifstatic Test()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    c
=200;
ExpandedBlockEnd.gif}

再看一个程序:

None.gifusing System;
None.gif
None.gif
class Test
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
InBlock.gif    
public static readonly int c=300;
InBlock.gif       
public static int d=0;
InBlock.gif    
InBlock.gif    Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif    
InBlock.gif        d
=400;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static Test()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        c
=300;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{

InBlock.gif        Console.WriteLine (
"{0},{1}",c,d);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif

此程序编译通过...
输出结果为 300,0

若改成

None.gifpublic static void Main()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        Test e
=new Test ();
InBlock.gif        Console.WriteLine (
"{0},{1}",c,d);
ExpandedBlockEnd.gif    }

则输出结果为 300,400

d被声明为静态,却不必放在静态构造函数中..?

转载于:https://www.cnblogs.com/kaisa/archive/2004/08/17/34125.html

在.NET框架中,`public static readonly DependencyProperty` 是在WPF(Windows Presentation Foundation)中定义依赖属性的一个常用方式。依赖属性允许WPF的属性系统提供额外的服务,如数据绑定、动画、样式设置继承等。 - `public` 表示这个属性是公开的,可以在类的外部访问修改。 - `static` 表示这是一个静态属性,它不属于类的任何单个实例,而是属于类本身。所有实例共享同一个属性值。 - `readonly` 表示这个静态属性是只读的,意味着它只能在声明时或在静态构造函数中被赋值,之后不能被修改。 `DependencyProperty` 是一个特殊的类,用于在WPF中存储依赖属性的信息。每个依赖属性都需要一个对应的 `DependencyProperty` 字段,这个字段通过调用 `DependencyProperty.Register` 静态方法来注册,并返回一个 `DependencyProperty` 实例。 举个简单的例子,如果你有一个 `MyUserControl` 控件类,你想创建一个名为 `UserName` 的依赖属性,它可能看起来像这样: ```csharp public class MyUserControl : UserControl { // 创建一个DependencyProperty字段 public static readonly DependencyProperty UserNameProperty = DependencyProperty.Register("UserName", typeof(string), typeof(MyUserControl)); // 公共的依赖属性获取设置方法 public string UserName { get { return (string)GetValue(UserNameProperty); } set { SetValue(UserNameProperty, value); } } } ``` 在这个例子中,`UserNameProperty` 是一个依赖属性字段,它通过 `DependencyProperty.Register` 方法注册。`UserName` 是一个公共属性,通过 `GetValue` `SetValue` 方法来获取设置 `UserNameProperty` 的值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值