我想将包含不可打印字符的字节数组转换为我的应用程序的字符串.当我转换回字节数组时,数组的内容应该保持相同,我发现ASCII/Unicode/UTF8总是给我正确的解决方案?
例如
byte[] bytearray ={ 147, 35, 44, 18, 255, 104, 206, 72 ,69};
string str = System.Text.Encoding.ASCII.GetString(bytearray);
bytearray = System.Text.Encoding.ASCII.GetBytes(str);
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我发现字节数组包含
{ 63, 35, 44, 18, 63, 104, 63, 72 ,69}.
Run Code Online (Sandbox Code Playgroud)
请帮助我.
我正在使用Visual Studio .NET 2010中的Windows窗体创建GUI应用程序C#.我有多个线程,例如:
Control.Invoke().我的目标:当用户在主窗体上按(x)时,我想确保正确关闭窗体.这意味着我希望我的连接关闭,所有侧线程终止等.我正在尝试使用FormClosing事件.在那里我将KillThreads标志设置为true并等待侧线程终止.每个都有一张支票,比如if(KillThreads)
return;
Run Code Online (Sandbox Code Playgroud)
但是有一个问题.如果我试图等待所有线程终止
for(;;)
if(ActiveSideThreads == 0)
break;
closeConnection();
Run Code Online (Sandbox Code Playgroud)
线程N3冻结被阻塞Invoke(); 当然是,因为主gui线程处于无限循环中.所以表格结束会永远延迟.
如果我将on-close操作实现为线程过程4,并在FromClosing事件处理程序中创建一个新线程,则表单在线程4终止之前立即关闭,并Invoke()抛出错误-t.4无法及时关闭线程3.如果我Join(), Invoke()再次阻止添加呼叫,并且线程3永远不会终止,那么线程4将永远等待.
我该怎么办?有什么方法可以解决这个问题吗?
int stxval = 0x02; //found this value on wikipedia
string mystring = "Hello";
StringBuilder builder = new StringBuilder(mystring);
builder.Append(stxval);
Run Code Online (Sandbox Code Playgroud)
此处在字符串构建器"2Hello"中附加.如何以这种形式"Hello"获取字符串?
有没有办法在图表上标记轴?
<charting:Chart Name="EventAlertsChart" BorderThickness="0" Margin="0,10,0,0">
<charting:Chart.Axes>
<charting:LinearAxis Orientation="Y" Minimum="0" Title="Number of Alerts" Margin="0,0,10,0" />
</charting:Chart.Axes>
<charting:Chart.LegendStyle>
<Style TargetType="Control">
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
</Style>
</charting:Chart.LegendStyle>
<charting:Chart.Series>
<charting:ColumnSeries Name="LineSeriesBWSrc" ItemsSource="{Binding AlertPoints,UpdateSourceTrigger=PropertyChanged}"
IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" Title="Alerts" Background="Maroon" >
<charting:ColumnSeries.DataPointStyle>
<Style TargetType="charting:ColumnDataPoint">
<Setter Property="Background" Value="Crimson" />
</Style>
</charting:ColumnSeries.DataPointStyle>
</charting:ColumnSeries>
</charting:Chart.Series>
</charting:Chart>
Run Code Online (Sandbox Code Playgroud)
我已设法使用标记Y轴
<charting:Chart.Axes>
<charting:LinearAxis Orientation="Y" Minimum="0" Title="Number of Alerts" Margin="0,0,10,0" />
</charting:Chart.Axes>
Run Code Online (Sandbox Code Playgroud)
但是,如果我想标记X轴,它会出现在图表的顶部.我只是希望能够像"时间"和"事件"那样在轴上键入一些图例,但我找不到合适的方法来做到这一点.
如果我在X轴上执行相同操作,则图例和值将显示在图表的顶部.

当X轴的代码被引入时:
<charting:Chart.Axes>
<charting:LinearAxis Orientation="Y" Minimum="0" Title="Number of Alerts"
Run Code Online (Sandbox Code Playgroud)

我编写了一个代码来计算给定文件夹中所有文件的行数.它工作正常,但我试图包含所有可能的C#功能,以将其重构为更紧凑和高效的代码.请帮帮我.
这是代码.
class LineNumberCounter
{
public static string Calculate(string folderPath, string pattern = "*.txt")
{
DirectoryInfo dirInfo = new DirectoryInfo(folderPath.Trim());
if (!dirInfo.Exists)
throw new ArgumentException("No such directory exists");
StringBuilder returnValue = new StringBuilder();
long totalLines = 0;
pattern.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).All(filter =>
{
int count = 0;
dirInfo.GetFiles(filter.Trim(),
SearchOption.AllDirectories).All(file =>
{
using (StreamReader reader = file.OpenText())
{
for (; reader.Peek() > -1; count++)
reader.ReadLine();
}
returnValue.AppendLine(string.Format("Number of lines with {0} pattern is {1}",
filter, count));
totalLines += count; …Run Code Online (Sandbox Code Playgroud) 我有一个结构:
struct Student
{
string Name;
int ClassesMissedToday;
}
Run Code Online (Sandbox Code Playgroud)
我现在有一个结构列表:
List<Student> Students = new List<Student>();
Run Code Online (Sandbox Code Playgroud)
如何在LINQ中说出以下内容:
Students.Add(new Student { Name = "Bob", ClassesMissedToday = 2 })
Students.Add(new Student { Name = "Bob", ClassesMissedToday = 0 })
Students.Add(new Student { Name = "Joe", ClassesMissedToday = 0 })
Students.Add(new Student { Name = "Bob", ClassesMissedToday = 1 })
Run Code Online (Sandbox Code Playgroud)
(伪代码)
foreach Student.Name in Students:
Console.WriteLine("Total Classes Missed Total: {0}", ClassedMissedToday(count all of them)
Run Code Online (Sandbox Code Playgroud)
我知道这对某些人来说相当微不足道,但由于某种原因,我似乎无法找到任何简化的有效示例.
想法?
我的代码做的很简单
列表已经有元素.我在列表中有大约25000个元素(我希望有更多元素),每个元素都很小(DateTime).
List<DateTime> newList = new List<DateTime>();
Parallel.ForEach(list, l => newlist.Add(new DateTime(l.Ticks + 5000)));
Run Code Online (Sandbox Code Playgroud)
也就是说,基于每个元素,我正在创建新元素并将它们添加到不同的列表中.但是,这似乎不是一个好的编程方法.我打这个例外一些时间,但不是每次.
IndexOutOfRangeException : {"Index was outside the bounds of the array."}
Run Code Online (Sandbox Code Playgroud)
我们可以使用Parallel.ForEach()将元素添加到列表中吗?如果是,为什么我会遇到错误?如果不是,为什么?
我的申请由于某种原因我有5位数的两个数字.
以下代码简要介绍了您的想法.
string s = "00001"; // Initially stored somewhere.
//Operation start
string id = DateTime.Now.ToString("yy") + DateTime.Now.AddYears(-1).ToString("yy") + s;
//Operation end
//Increment the value of s by 1. i.e 00001 to 00002
Run Code Online (Sandbox Code Playgroud)
这可以通过将s的值转换为int并将其递增1 来轻松完成,但毕竟我还必须将增加的s值存储为5位数,因此它将为"00002".
这个想法给我一个痛苦......
我有一个ListBoxItem的数据模板,它包含几个按钮,以及很少的自定义控件,如网格或图表.每个按钮都绑定到一个合适的命令处理程序,ListView控件的SelectedIndex属性也绑定到ViewModel的属性.
问题:在命令处理程序(绑定到按钮)中我无法解析当前选定的项目/索引,因为它在单击ListBox项目中的按钮或其他控件时没有改变,但是当我单击ListBoxItem区域本身时 - SelectedIndex正在改变.
问题是如何在单击ListBoxItem中的任何控件时触发SelectedIndex被更改?
Microsoft在哪些方面利用了适配器模式?我正在寻找使用适配器的.NET组件的具体示例.