Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么;“违约”;在C#TcpClient clientSocket=default(TcpClient);_C#_Sockets_Default - Fatal编程技术网

为什么;“违约”;在C#TcpClient clientSocket=default(TcpClient);

为什么;“违约”;在C#TcpClient clientSocket=default(TcpClient);,c#,sockets,default,C#,Sockets,Default,我找到了一些关于如何在C#中编写TCP/IP客户机-服务器应用程序的代码 主服务器以以下内容开始: TcpListener serverSocket = new TcpListener(8888); int requestCount = 0; TcpClient clientSocket = default(TcpClient); serverSocket.Start(); Console.WriteLine(" >> Server Started"); clientSocket =

我找到了一些关于如何在C#中编写TCP/IP客户机-服务器应用程序的代码 主服务器以以下内容开始:

TcpListener serverSocket = new TcpListener(8888);
int requestCount = 0;
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();

不幸的是,我不知道第三行中的关键字default在做什么。我本以为最后一行初始化了clientSocket,所以之前不需要初始化它。

default
关键字只是将对象
clientSocket
设置为
null
或引用类型的默认值,因为
TcpClient
是一个类(一个引用类型)。这与:

TcpClient clientSocket = null;
简而言之,它返回任何指定类型的默认值。例如,在
int
的情况下,它将返回
0
。比如:

int i = default(int); // i = 0
default
在类型未知的泛型代码中很有用


请参阅:

注意,此分配在此代码片段中没有任何作用。值(null)将被最后一行覆盖。