小编Win*_*der的帖子

> 1客户端无法连接到c#中的服务器

我正在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)

.net c# sockets networking network-programming

1
推荐指数
1
解决办法
1142
查看次数

什么是这种类型的委托(C#)

我有这些代码行.

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(); };.

当然它不能被称为匿名方法,因为在这里我有一个命名方法.

.net c# delegates anonymous-delegates

1
推荐指数
2
解决办法
174
查看次数

通用接口引用如何接受非泛型对象?

我有代码

IEnumerable<card> any_object = new card[] { };
Run Code Online (Sandbox Code Playgroud)

哪个card是没有实现的类IEnumerable.我无法理解这段代码是如何工作的?

.net c# generics

1
推荐指数
1
解决办法
106
查看次数

EntityType"posts"没有定义键.定义此EntityType的键

我的模特课是

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)

我不明白为什么这么说.即使我遵循命名约定并仍然插入了安全措施的密钥注释,它仍然给我错误.

c# asp.net-mvc asp.net-mvc-4

1
推荐指数
1
解决办法
1792
查看次数

解决C#中的线程冲突

假设有一个由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 c# multithreading thread-safety

1
推荐指数
1
解决办法
331
查看次数

async和await不起作用

我正在尝试学习异步和等待.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(),一切似乎都正常.

谁能告诉我这里发生了什么?

.net c# asynchronous async-await

1
推荐指数
1
解决办法
2万
查看次数

更改不会永久保存到SQL Server Compact Edition中

我有一个奇怪的问题.我使用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关闭,之后我打开它来显示数据,数据就会消失,因为它从未保存过

当它清楚然后执行查询时,这怎么可能?

.net c# sql sql-server-ce

1
推荐指数
1
解决办法
2211
查看次数

String.Format不起作用

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#并且不知道是什么导致了这种奇怪的行为.

.net c# string.format

0
推荐指数
2
解决办法
138
查看次数

LINQ表达式而不是嵌套的foreach循环

所以我有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查询来实现相同的期望结果.

.net c# linq

0
推荐指数
1
解决办法
2116
查看次数

单击富文本框中的链接后如何打开新的浏览器窗口(C#)

我有一个Twitter应用程序,它检索用户时间线并将其附加到富文本框.但是当我点击链接时没有任何反应?

这里有什么不对,富文本框自动处理创建具有指定链接的新broswer窗口.如果没有那么我该如何实现这样的功能?

我正在使用winforms.

.net c# url richtextbox winforms

-1
推荐指数
1
解决办法
1691
查看次数