我今天正在研究一个项目,发现自己在几个地方使用Math.Max,并在其他地方使用内联if语句.所以,我想知道是否有人知道哪个更"好"......或者更确切地说,真正的差异是什么.
例如,在下面,c1 = c2
:
Random rand = new Random();
int a = rand.next(0,10000);
int b = rand.next(0,10000);
int c1 = Math.Max(a, b);
int c2 = a>b ? a : b;
Run Code Online (Sandbox Code Playgroud)
我是专门询问C#,但我想不同语言的答案可能会有所不同,但我不确定哪些有类似的概念.
在清理我继承的项目的文件夹/文件结构的过程中,我遇到了组织所需外部库的问题.我想将它们保存在自己的.\dll\
文件夹中,但它们不会被正确复制到构建目录中.它们应该位于根构建目录中,但它们将被移动到子文件夹中.
我的.csproj
文件包含以下xml:
<Project>
<ItemGroup>
<None Include="dlls\libraryA.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
然后,在构建时,libraryA.dll
文件被复制到bin\Debug\dll\
文件夹,但我想在bin\Debug\
文件夹中.
我有一个异步UDP服务器类,其中一个套接字绑定在IPAddress.Any上,我想知道收到的数据包被发送到哪个IP地址(...或接收到).看来我不能只使用Socket.LocalEndPoint属性,因为它总是返回0.0.0.0(这是有意义的,因为它绑定到那个......).
以下是我目前使用的代码的有趣部分:
private Socket udpSock;
private byte[] buffer;
public void Starter(){
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, 12345));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
private void DoReceiveFrom(IAsyncResult iar){
//Get the received message.
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
int msgLen = recvSock.EndReceiveFrom(iar, ref clientEP);
byte[] localMsg = new byte[msgLen]; …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在构建过程中将所有javascript文件组合在一个项目中,但它对我来说不起作用.这就是我所拥有的:
<Target Name="CombineJS">
<CreateItem Include=".\**\*.js">
<Output TaskParameter="Include" ItemName="jsFilesToCombine" />
</CreateItem>
<ReadLinesFromFile File="@(jsFilesToCombine)">
<Output TaskParameter="Lines" ItemName="jsLines" />
</ReadLinesFromFile>
<WriteLinesToFile File="all.js" Lines="@(jsLines)" Overwrite="true" />
</Target>
Run Code Online (Sandbox Code Playgroud)
MSBuild ReadLinesFromFile
在行上抛出一个错误,指出"File"参数的值无效.(当只有一个文件要合并时没有错误)
那么,有两个问题:
我有一个非常简单的异步UDP侦听器,设置为服务,它现在已经运行了一段时间,但它最近在SocketException上崩溃了An existing connection was forcibly closed by the remote host
.我有三个问题:
我的代码如下所示:
private Socket udpSock;
private byte[] buffer;
public void Starter(){
//Setup the socket and message buffer
udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpSock.Bind(new IPEndPoint(IPAddress.Any, 12345));
buffer = new byte[1024];
//Start listening for a new message.
EndPoint newClientEP = new IPEndPoint(IPAddress.Any, 0);
udpSock.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref newClientEP, DoReceiveFrom, udpSock);
}
private void DoReceiveFrom(IAsyncResult iar){
try{
//Get the received message.
Socket recvSock = (Socket)iar.AsyncState;
EndPoint clientEP …
Run Code Online (Sandbox Code Playgroud) 我有一个winform程序,可以在a上执行一些异步IO SerialPort
.但是,我经常遇到程序冻结SerialPort.Close()调用的问题,看似随意.
我认为这是一个线程安全问题,但我不确定如果它是如何解决它.我尝试使用端口打开/关闭功能添加/删除异步DataReceived处理程序并丢弃端口上的输入和输出缓冲区,但它似乎没有做任何事情.我认为重要的SerialPort
代码如下:
using System;
using System.Collections.Generic;
using System.IO.Ports;
public class SerialComm
{
private object locker = new object();
private SerialPort port;
private List<byte> receivedBytes;
public SerialComm(string portName)
{
port = new SerialPort(portName);
port.BaudRate = 57600;
port.Parity = Parity.None;
port.DataBits = 8;
port.StopBits = StopBits.One;
receivedBytes = new List<byte>();
}
public void OpenPort()
{
if(port!=null && !port.IsOpen){
lock(locker){
receivedBytes.Clear();
}
port.DataReceived += port_DataReceived;
port.Open();
}
}
public void ClosePort()
{
if(port!=null && port.IsOpen){
port.DataReceived -= port_DataReceived;
while(!(port.BytesToRead==0 …
Run Code Online (Sandbox Code Playgroud) 我编写了一个C#windows服务,可以将消息写入自定义EventLog或任意数量的文件.这些消息都标记有一些优先级(因此,例如,只有ERROR和WARNING存储在EventLog中,但如果需要,可以将更多内容存储到文件中).
我现在要做的是创建一个GUI,可以监听这些消息并实时显示它们.允许用户观看当前的消息(在任何他们想要的优先级),而不需要的一切存储到文件中.我认为这是某种形式的挂钩到服务的一个单独的程序,但我不能确定从哪里开始.
这是我第一次真正的Windows服务,所以我似乎缺少一些关键字找出如何做到这一点?是否有任何代码样本,教程,参考文献等,为如何做这样的事情?
更新
很多有用的答案,我喜欢它,有很多方法可以解决问题!我想我将实现一个基于WCF的自托管解决方案.我仍然非常关注细节,因为我正在努力了解WCF(我相信它对我来说在其他项目中非常有用)...但到目前为止,我发现这里的视频是最多的有用的介绍方法.
我正在学习JQuery,因为我正在对网站进行重大的重新设计.我意识到这将是多么好的做一些造型中的JavaScript,而不是记住要正确的CSS类添加到每一个DIV(显然,我想忘记这样的事情).
无论如何,我想将样式添加cursor: pointer;
到已注册click事件的所有元素.我认为它需要看起来像这样:
$("div").Find(/*Has Click Event*/)
.css("cursor", "pointer");
Run Code Online (Sandbox Code Playgroud) 目前我有一个SQL Server 2005表,看起来有点像:
ID | name | desc ---------------------- 1 | ONE | Value One 3 | THREE | Value Three 5 | FIVE | Value Five
此表对应于C#中的枚举,如下所示:
enum MsgTypes{
<summary>Value One</summary>
ONE = 1,
<summary>Value Three</summary>
THREE = 3,
<summary>Value Five</summary>
FIVE = 5
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:有没有一种很好的方法将枚举与SQL表关联起来,以便不需要在c#代码中手动对表中的值进行任何更改/添加?
我的 Windows 窗体中有多个控件,我希望右键单击每个控件时都弹出相同的菜单。但是,根据单击的控件的不同,操作应略有不同。
我遇到的问题是 ToolStripMenuItem 没有任何有关最初单击哪个控件以使工具条可见的信息。我真的不想每个控件都需要一个单独的上下文菜单!
到目前为止,我的代码看起来像这样:
private void InitializeComponent()
{
this.openMenuItem = new ToolStripMenuItem();
this.openMenuItem.Text = "Open";
this.openMenuItem.Click += new EventHandler(this.openMenuItemClick);
this.runMenuItem = new ToolStripMenuItem();
this.runMenuItem.Text = "Run";
this.runMenuItem.Click += new EventHandler(this.runMenuItemClick);
this.contextMenuStrip = new ContextMenuStrip(this.components);
this.contextMenuStrip.Items.AddRange(new ToolStripMenuItem[]{
this.openMenuItem,
this.runMenuItem});
this.option1 = new Label();
this.option1.Click += new EventHandler(this.optionClick);
this.option2 = new Label();
this.option2.Click += new EventHandler(this.optionClick);
}
void optionClick(object sender, EventArgs e)
{
MouseEventArgs mea = e as MouseEventArgs;
Control clicked = sender as Control;
if(mea==null || clicked==null) return; …
Run Code Online (Sandbox Code Playgroud) 我有一个 Windows.Forms.TextBox,我希望允许用户输入转义序列,然后将其解释为它们所代表的字符。
例如,如果输入以下内容:
Hello\r\nWorld
Run Code Online (Sandbox Code Playgroud)
我希望将其存储为字符数组:
H e l l o \r \n W o r l d
0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x0D, 0x0A, 0x57, 0x6F, 0x72, 0x6C, 0x64
Run Code Online (Sandbox Code Playgroud)
到目前为止,我使用以下内容来处理 '\r' 和 '\n',但我也希望能够处理任何其他转义序列,例如\u001A
(ctrl+z) 或\u001B
(esc)
string str = txtBox.Text.Replace("\\r","\r").Replace("\\n","\n");
Run Code Online (Sandbox Code Playgroud)
...必须有一种更通用的方法来处理这个问题...
我正在寻找一个C#SQL Server包装器来调用一些存储过程.如果我正在写一个函数,我会做类似下面的事情(我认为是正确的/正确的):
void RunStoredProc1(object arg1)
{
using(SqlConnection conn = new SqlConnection(connStr)){
try{
SqlCommand cmd = new SqlCommand("storedProc1", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@input1", arg1);
conn.Open();
cmd.ExecuteNonQuery();
} catch (Exception ex){
//handle the exception appropriately.
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是它似乎有很多重复的代码......每个函数都有相同的using/try(打开/执行)/ catch代码,而且只需要一个就可以了地点.这样做有干净的方法吗?对于我想要使用数据读取器的查询怎么样?
c# ×9
winforms ×3
.net ×2
msbuild ×2
udp ×2
asynchronous ×1
asyncsocket ×1
csproj ×1
escaping ×1
event-log ×1
inline-if ×1
jquery ×1
serial-port ×1
sockets ×1
textbox ×1