有没有办法指定我的变量是一个短整数?我正在寻找类似于M后缀的小数字.对于小数,我不必说
var d = (decimal)1.23;
Run Code Online (Sandbox Code Playgroud)
我可以写如下:
var d = 1.23M;
Run Code Online (Sandbox Code Playgroud)
有没有办法写这个
var s = SomeLiteralWithoutCast
Run Code Online (Sandbox Code Playgroud)
所以s暗示是短的int?
有谁知道C#编译器编号文字修饰符的完整列表?
默认情况下,声明'0'使其成为Int32,'0.0'使其成为'Double'.我可以在末尾使用文字修饰符"f"来确保将某些内容视为"单个".比如这样......
var x = 0; // x is Int32
var y = 0f; // y is Single
Run Code Online (Sandbox Code Playgroud)
我可以使用的其他修饰符是什么?是否有一个强制Double,Decimal,UInt32?我试着谷歌搜索但找不到任何东西.也许我的术语是错误的,这就解释了为什么我的空白.任何帮助非常感谢.
您可以在C#中以各种方式定义一个数字,
1F // a float with the value 1
1L // a long with the value 1
1D // a double with the value 1
Run Code Online (Sandbox Code Playgroud)
我个人正在寻找哪种方式short,但是为了让问题更好地为人们提供参考,你可以应用的数字文字的所有其他后期修复是什么?
我有以下代码:
Int16 myShortInt;
myShortInt = Condition ? 1 :2;
Run Code Online (Sandbox Code Playgroud)
此代码导致编译器错误:
不能将'int'类型转换为'short'
如果我以扩展格式编写条件,则没有编译器错误:
if(Condition)
{
myShortInt = 1;
}
else
{
myShortInt = 2;
}
Run Code Online (Sandbox Code Playgroud)
为什么我会收到编译器错误?
我正在尝试制作一个简单的英尺到仪表转换器,但发生了这种情况:
using System;
using System.Windows;
using System.Windows.Controls;
namespace CoolConversion
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
decimal feet;
decimal meter;
public MainWindow()
{
InitializeComponent();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
feet = Convert.ToDecimal(Feet.Text);
meter = feet / 3.281;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我目前拥有的代码。起初,feet&meter是整数,但我不能将整数除以 3.281。我将它们更改为小数,现在出现此错误:
错误 CS0019 运算符“/”不能应用于“decimal”和“double”类型的操作数
如果我不能用整数除以小数,如果我不能/在小数上使用符号,我应该如何除以小数?
当我读到关于隐式类型变量时,这个问题出现在我的脑海里.我无法在互联网上找到答案所以决定把它放在野兔身上.
假设我使用'var'关键字声明一个变量.
var i = 10;
Run Code Online (Sandbox Code Playgroud)
编译后,我编译/处理为'整数'我.
现在,我的问题是为什么'i'没有被编译为'short',因为'i'的值非常小以适应'Short'数据类型; 为什么它总是编译成'整数'?
我试图在我的应用程序中乘以两个数字,但在某些情况下,它会导致错误的值
var result = 0;
var firstNumber = 654165;
var secondNumber = 6541;
result = firstNumber * secondNumber;
Run Code Online (Sandbox Code Playgroud)
结果-16074031是错的
你能帮我找到错误的地方吗?