static void Main(string[] args)
{
countValues();
}
static void countValues()
{
float value1;
float value2;
float result;
Console.WriteLine("Give a number");
value1 = Convert.ToSingle(Console.Read());
Console.WriteLine("Give another number");
value2 = Convert.ToSingle(Console.Read());
result = value1 + value2;
Console.WriteLine("You gave numbers " + value1 + " and " + value2);
Console.WriteLine("Together these values are " + result);
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这段代码有什么问题吗?我上次使用C#已经很久了,我已经忘记了它的一切.当我运行这段代码时,它会询问一个数字.当我输入一些数字时,它会打印所有内容,而不会要求第二个.
我正在编写一个程序,将数据从微控制器获取到 PC。数据为浮点格式。我尝试使用将字符串转换为浮点型Convert.ToSingle(string),但转换结果错误:
正如您所看到的,它丢失了前导 0. ,这是出乎意料的。怎么会发生这种事?
MySpecialView是一个复杂的图像控件,我想从不同的角度重用它,并ViewModel像本例一样传递它。
MainWindow.xaml
<Window x:Class="YouBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:YouBug"
mc:Ignorable="d"
DataContext="{Binding MainViewModel}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:MySpecialView ViewModel="{Binding MySpecialViewModel}"></local:MySpecialView>
</Grid>
Run Code Online (Sandbox Code Playgroud)
主视图模型
public class MainViewModel
{
public MySpecialViewModel MySpecialViewModel { get; set; }
public MainViewModel()
{
MySpecialViewModel = new MySpecialViewModel();
//gets not displayed!
Task.Run(() => MySpecialViewModel.changeImage(5000, "C:\\Users\\user\\Pictures\\Capture.PNG"));
}
}
Run Code Online (Sandbox Code Playgroud)
MySpecialView.xaml
<UserControl x:Class="YouBug.MySpecialView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:YouBug"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Image Source="{Binding ImageSource}" />
</Grid>
Run Code Online (Sandbox Code Playgroud)
MySpecialView.xaml.cs
public partial class MySpecialView : UserControl
{
public static readonly DependencyProperty …Run Code Online (Sandbox Code Playgroud) 根据 Pashov 的博客文章,我有以下静态方法用于在过滤器选项之间进行选择。
public static class ExpressionRetriever
{
private static MethodInfo containsMethod = typeof(string).GetMethod("Contains");
private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });
public static Expression GetExpression<T>(ParameterExpression param, ExpressionFilter filter)
{
MemberExpression member = Expression.Property(param, filter.PropertyName);
ConstantExpression constant = Expression.Constant(filter.Value);
switch (filter.Comparison)
{
case Comparison.Equal:
return Expression.Equal(member, constant);
case Comparison.GreaterThan:
return Expression.GreaterThan(member, constant);
case Comparison.GreaterThanOrEqual:
return Expression.GreaterThanOrEqual(member, constant);
case Comparison.LessThan:
return Expression.LessThan(member, constant);
case Comparison.LessThanOrEqual:
return Expression.LessThanOrEqual(member, …Run Code Online (Sandbox Code Playgroud) 我有一个类对象的列表
可以说这个类的结构是这样的
class DataEntity
{
public string col1 { get; set; }
public string col2 { get; set; }
public string col3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
所以名单将是
List<DataEntity> list = new List<DataEntity>
Run Code Online (Sandbox Code Playgroud)
所以这是我的问题,我需要遍历列表并修改特定属性的值..但我不知道在运行时需要修改什么属性。
所以可以说有一个方法可以转换列表,并且要修改的属性名称作为字符串值传入
public List<DataEntity> Convert (List<DataEntity> list, string propertyName, string appendedValue)
{
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何遍历该列表,并为输入的 propertyName 将 appendedValue 附加到该属性值
我知道我可以使用这样的反射来获取属性值
Type type = dataEntity.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
}
Run Code Online (Sandbox Code Playgroud)
但我不确定如何利用它来定位特定属性以在运行时追加。
我有一个任务来计算它从某个来源接收到的数据包数量。
每 250 毫秒一个计时器启动读取并将计数输出给用户。在我需要将计数设置回 0 之后。
我担心的是,在读取和显示计数之间,但在我设置计数 = 0 之前,计数在另一个线程中增加了,所以我最终通过将其归零来丢失计数。
我是线程的新手,所以我有多种选择。
我研究过使用 Interlocked 但据我所知它只给我算术运算,我没有选择将变量实际设置为值。
我也在研究 ReaderWriterLockSlim,我需要的是最有效/更少开销的方法来完成,因为有很多数据来了。
我最近问过这个问题:什么时候==会以不同的方式覆盖.equals?.我被引用到这篇文章:https://ericlippert.com/2013/10/07/math-from-scratch-part-six-comparisons/
我不完全理解对静态方法调用(==和!=)和动态方法调用(.Equals())的引用.请参阅以下代码:
public class A
{
private string Field1;
private string Field2;
public A(string field1, string field2)
{
Field1 = field1;
Field2 = field2;
}
public static bool operator ==(A a1, A a2)
{
throw new NotImplementedException();
}
public static bool operator !=(A a1, A a2b)
{
throw new NotImplementedException();
}
public override int GetHashCode()
{
throw new NotImplementedException();
}
public override bool Equals(object obj)
{
throw new NotImplementedException(); …Run Code Online (Sandbox Code Playgroud) 我有一个文本框,我希望人们输入一个不是0且小于5的整数(不是双精度).变量numofitems存储文本框的值,当你按下一个按钮时,它会将它转换为int和测试它是否符合我想要的要求.这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
string numofitems = textBox1.Text;
int x = 0;
if (Int32.TryParse(numofitems, out x) && numofitems !=0 && numofitems <=5)
{
//it is valid
}
}
Run Code Online (Sandbox Code Playgroud)
虽然,它说:
operator!=不能应用于'string'和'int'的操作数.
我怎样才能解决这个问题?我不想先测试它的价值,因为那时人们可以进入00文本框,它会起作用.谢谢!
我们如何从元素本身知道列表元素的索引?
例如,下面的代码将不起作用,因为“s”没有名为 的属性index。但是有没有一种简单的方法来获取索引呢?
我知道IndexOf(),Find()但这不是我要找的东西。因为我们已经有了元素,所以它应该是类似属性的东西来显示索引,对吧?
private List<string> myStr = ......
foreach (string s in myStr)
{
if (....)
{
return s.index;
}
}
Run Code Online (Sandbox Code Playgroud)