我正在C#中创建一个聊天客户端,以便在localhost上进行演示.
这是相关的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static List<TcpListener> garabage_collection_preventor = new List<TcpListener>();
static Dictionary<IPEndPoint, bool> address_dictionary = new Dictionary<IPEndPoint, bool>();
static int port_increment = 9999;
static int client_id = 0;
void start_listening()
{
while (true)
{
TcpListener listen = new TcpListener(IPAddress.Any, port_increment);
garabage_collection_preventor.Add(listen);
listen.Start();
TcpClient client = listen.AcceptTcpClient();
IPEndPoint temp_end …Run Code Online (Sandbox Code Playgroud) 我有这些代码行.
class Program
{
public delegate void printer();
public static void Method()
{
Console.WriteLine("Hello");
}
static void Main(string[] args)
{
printer del = delegate { Method(); };
del();
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我怎么称呼这句话printer del = delegate { Method(); };.
当然它不能被称为匿名方法,因为在这里我有一个命名方法.
我有代码
IEnumerable<card> any_object = new card[] { };
Run Code Online (Sandbox Code Playgroud)
哪个card是没有实现的类IEnumerable.我无法理解这段代码是如何工作的?
我的模特课是
public class posts
{
[Key]
public int AuthorId;
public string author_first_name;
public string author_last_name;
public string post_text;
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这么说.即使我遵循命名约定并仍然插入了安全措施的密钥注释,它仍然给我错误.
假设有一个由2个线程访问的静态变量.
public static int val = 1;
Run Code Online (Sandbox Code Playgroud)
现在假设线程1执行这样的语句
if(val==1)
{
val +=1
}
Run Code Online (Sandbox Code Playgroud)
但是在检查之后和上面的语句中添加之前,线程2将val的值更改为其他值.
现在这会导致一些令人讨厌的错误.这发生在我的代码中.
是否有任何方式线程1被注意到val的值已被更改,而不是添加返回并再次执行检查.
我正在尝试学习异步和等待.NET 4.5的功能.首先,这是我的代码
static async void Method()
{
await Task.Run(new Action(DoSomeProcess));
Console.WriteLine("All Methods have been executed");
}
static void DoSomeProcess()
{
System.Threading.Thread.Sleep(3000);
}
static void Main(string[] args)
{
Method();
//Console.WriteLine("Method Started");
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
此代码不会在控制台上给我任何结果.我不明白为什么.我的意思是不是任务假设只是没有阻塞的线程.但是,如果我在main方法中取消注释Console.WriteLine(),一切似乎都正常.
谁能告诉我这里发生了什么?
我有一个奇怪的问题.我使用vs2012连接到SQL Server CE并执行一些插入查询.
public void EstablishConnection()
{
connection = new SqlCeConnection("Data Source=DataDump.sdf;Persist Security Info=False;");
try
{
connection.Open();
Console.WriteLine("Connection Successful");
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
public void AddRecord(string link,string content)
{
int num = 0;
var command = new SqlCeCommand("INSERT INTO Webpages(Link,Data) VALUES('"+link+"','"+content+"');",connection);
num = command.ExecuteNonQuery();
Console.WriteLine("Command Successful rows affected"+num);
var cmd2 = new SqlCeCommand("SELECT * FROM Webpages",connection);
SqlCeDataReader reader = cmd2.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader[0]);
Console.WriteLine(reader[1]);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我遇到的问题是,一旦VS关闭,之后我打开它来显示数据,数据就会消失,因为它从未保存过
当它清楚然后执行查询时,这怎么可能?
public override string ToString()
{
string token = "something";
foreach (something item in this)
{
something = something + "Some_Point({0},{1}),";
string.Format(something, item.X, item.Y);
}
return something+= "anything";
}
Run Code Online (Sandbox Code Playgroud)
这是ToString()自定义集合的重写方法.这里的问题String.Format是不替换X和Y的值.每当我添加return之前它都会替换String.Format.我刚刚开始学习C#并且不知道是什么导致了这种奇怪的行为.
所以我有List,其中每个元素都是一个字符串数组
List<string[]> TokenList = new List<string[]>();
Run Code Online (Sandbox Code Playgroud)
我想在列表中的每个数组中显示数组中的每个元素.为此,我使用嵌套的foreach循环.
foreach (var temp in pro.TokenList)
{
foreach (var s in temp)
{
Console.WriteLine(s);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在我的程序中使用LINQ,我想知道将使用什么样的LINQ查询来实现相同的期望结果.
我有一个Twitter应用程序,它检索用户时间线并将其附加到富文本框.但是当我点击链接时没有任何反应?
这里有什么不对,富文本框自动处理创建具有指定链接的新broswer窗口.如果没有那么我该如何实现这样的功能?
我正在使用winforms.
c# ×10
.net ×9
asp.net-mvc ×1
async-await ×1
asynchronous ×1
delegates ×1
generics ×1
linq ×1
networking ×1
richtextbox ×1
sockets ×1
sql ×1
url ×1
winforms ×1