为什么不允许在.net中创建日期时间常量? [英] Why it is not allowed to create datetime constant in .net?

查看:73
本文介绍了为什么不允许在.net中创建日期时间常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在.net中创建日期时间常量,但收到错误消息

I want to create a datetime constant in .net but getting the error message

const DateTime dt = Convert.ToDateTime("02/02/2014");

'System.DateTime'不能声明为const。

'System.DateTime' cannot be declared const.

推荐答案

DateTime不是C#语言的本机类型-它是.Net库中的结构。

DateTime is not a native type in the C# language - it's a struct from the .Net libraries.

C#,您只能使用本地语言类型创建const,并且只能使用可在编译时计算的值来初始化它们。 DateTime。现在无法在编译时计算。

In C#, you can only create consts from the native language types, and you can only initialise them with values that can be calculated at compile-time. DateTime.Now cannot be calculated at compile time.

(唯一可以与const一起使用的引用类型是字符串,这是因为字符串是经过特殊处理的。)

(The only reference type you can use with a const is a string, and that's because strings are handled specially.)

请参见 http://msdn.microsoft.com/en-gb/library/e6w8fe1b%28v=vs.71%29.aspx

这篇关于为什么不允许在.net中创建日期时间常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆