我需要const C#的帮助 [英] I need help with const C#
问题描述
大家好,我真的需要帮助解决这个错误。
到目前为止,我从未真正使用过consts。
所以我的计划是将所有偏移存储在我的API中,直到有案例一直顺利
case 0x8ffddfsdf
所以这里我的基本静态uint编码
public static uint HudEnt
{
get
{
#if(DEBUG)
return 0x913E0000;
#else
return Program.API.Offsets.Addr_Main.HudEnt.Get ();
#endif
}
}
,这是我尝试使用const
public < span class =code-keyword> const uint 测试
{
get
{
#if(DEBUG)
return 0x4156091D;
#else
return Program.API.Offsets.Addr_Main.Test.Get ();
#endif
}
}
但是这个代码我得到一个错误一个const字段需要一个值
我试过的:
目前我还没有尝试任何东西,因为我还是第一次使用它。因为它是我第一次使用它。
查看错误消息:
const字段需要值您无法定义
const
属性,因为属性不是字段 - 它是getter和setter方法对的语法糖:
public uint测试
{
get {return 123; }
}
实现为:
public uint getTest()
{
return 123;
}
你的内联用法也被翻译成方法调用。
因为C#中的方法不必返回相同的值时间,你不能有一个const属性,只有一个const字段。
即便如此,必须知道const
字段的值在编译时,而不是运行时间。
所以你不能这样做:
public const uint Test = Program.API.Offsets。 Addr_Main.Test.Get();
出于同样的原因:方法调用每次都不必返回相同的值,因此在编译时不知道表达式。
你可以做的是使用静态只读
:
public static readonly uint Test = Program .API.Offsets.Addr_Main.Test.Get();
它生成一个可以在运行时设置的值 - 一次,只有一次,除非它在构造函数内。
0x8ffddf s df
不是有效的十六进制数字表示。
十六进制数字只能用数字0到9和字母a到f指定(不区分大小写)。
此外,在C#中,您不能像使用C(++)那样使用属性或方法的const
关键字)。const
关键字是为类中的字段变量保留的。
所以你必须替换const
带有static
说明符的说明符:
public static uint 测试{
// ...
}
希望这个帮助。麻烦。
Hello all I really need help with this error I am getting.
I have never really used consts before till now.
So my plan is to store all offsets inside my API, it was all going well until there was a case
case 0x8ffddfsdf
So here my coding for basic static uint
public static uint HudEnt
{
get
{
#if (DEBUG)
return 0x913E0000;
#else
return Program.API.Offsets.Addr_Main.HudEnt.Get();
#endif
}
}
and here's my attempt with a const
public const uint Test
{
get
{
#if (DEBUG)
return 0x4156091D;
#else
return Program.API.Offsets.Addr_Main.Test.Get();
#endif
}
}
But with this code I am getting an error "A const field requires a value"
What I have tried:
Currently I have not tried nothing as Im still learn consts, since it the first time I needed to use one.
Look at the error message:
A const field requires a valueYou can't define a
const
property because a property is not a field - it's syntactic sugar for a getter and setter method pair:
public uint Test { get { return 123; } }Is implemented as:
public uint getTest() { return 123; }And your inline usage is also translated into the method calls.
And since methods in C# do not have to return the same value each time, you can't have a const property, just a const field.
And even then, the value of aconst
field must be known at compile time, not run time.
So you can't do this either:
public const uint Test = Program.API.Offsets.Addr_Main.Test.Get();For the same reason: the method call doesn't have to return the same value each time, so the expression is not known at compile time.
What you can do is usestatic readonly
:
public static readonly uint Test = Program.API.Offsets.Addr_Main.Test.Get();Which generates a value that can be set at runtime - once, and once only unless it is inside the constructor.
0x8ffddfsdf
is not a valid hexadecimal number representation.
Hexadecimal numbers can only be specified with digits 0 to 9 and letters a to f (case insensitive).
Moreover, in C# you cannot use theconst
keyword with a property or method as you can do in C(++).const
keyword is reserved for field variables inside a class.
So you have to replace theconst
specifier with thestatic
specifier:
public static uint Test { // ... }
Hope this helps. Kindly.
这篇关于我需要const C#的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!