问题描述
你能用C ++在C#中定义const函数吗?
例如(C ++ - code):
int Cube :: GetSide()const
{
返回m_side;
}
Thnx
Christian
Hi,
can you define const-functions in C# as in C++ ?
for example (C++-code) :
int Cube :: GetSide() const
{
return m_side;
}
Thnx
Christian
推荐答案
号码我不知道是否'正在考虑中。约束可以非常有用,但它确实需要相当多的纪律。
您可能对
-
Jon Skeet - < sk *** @ pobox.com>
如果回复小组,请不要给我发邮件
No. I don''t know whether or not it''s being considered. Const-ness can
be very useful, but it does require a fair amount of discipline.
You may well be interested in http://constcli.sscli.net/
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
不,我不知道是否正在考虑。 Const-ness可以非常有用,但它确实需要相当多的纪律。
您可能对
-
Jon Skeet - < sk *** @ pobox.com>
如果回复小组,请不要给我发邮件
No. I don''t know whether or not it''s being considered. Const-ness can
be very useful, but it does require a fair amount of discipline.
You may well be interested in http://constcli.sscli.net/
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
单独使用,不是很多 - 但是当它与想法相结合时一个const
对象或变量,这意味着你可以有更多的安全。例如,
假设您在类中维护一个字节数组,并且您希望在只读字段中使该数组可用。有点方式 - 你可以用const来公开它
属性,这意味着客户需要
做:
const byte [] foo = myObject.MyProperty;
而不是
byte [] foo = myObject.MyProperty;
然后他们就做不了
foo [0] = 5;
但他们可以做到
字节x = foo [0] ;
这意味着你基本上可以返回对可变对象的引用,但
带有const修饰符意味着客户端只能调用标记为const的
方法。同样。 (例如,在StringBuilder中
ToString可能被标记为const,但是Append不会。)
-
Jon Skeet - < sk *** @ pobox.com>
如果回复小组,请不要给我发邮件
On its own, not a lot - but when combined with the idea of a const
object or variable, it means you can have more safety. For instance,
suppose you maintain a byte array within a class, and you wish to make
that array available in a "read-only" kind of way - you could expose it
with a "const" property, which would mean that clients would have to
do:
const byte[] foo = myObject.MyProperty;
instead of
byte[] foo = myObject.MyProperty;
They then couldn''t do
foo[0] = 5;
but they could do
byte x = foo[0];
It means you can basically return references to mutable objects, but
with the "const" modifier meaning that the client could only call
methods marked as "const" as well. (So for instance, in StringBuilder
ToString might be marked as "const", but Append wouldn''t.)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于const函数在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!