Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 为什么我';如果变量为null,m不会得到null异常?_C#_Winforms - Fatal编程技术网

C# 为什么我';如果变量为null,m不会得到null异常?

C# 为什么我';如果变量为null,m不会得到null异常?,c#,winforms,C#,Winforms,代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using unfreez_wrapper;
using Shell32;


namespace DownloadImages
{
    public partial class Form1 : Form
    {
        string rainMapToRead;
        string UrlsPath;
        int counter;
        UnFreezWrapper uf;
        string localFilename;
        string stringForSatelliteMapUrls;
        string satelliteMapToRead;
        List<string> StartTags;
        List<string> LastTags;
        List<string> Maps;

        ExtractImages ei;

        public Form1()
        {
            InitializeComponent();




                using (WebClient client = new WebClient())
                {
                    client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#",localFilename + "rainMap.html");
                    client.DownloadFile("http://www.sat24.com/en/eu?ir=true", localFilename + "satelliteMap.html");
                }

                rainMapToRead = File.ReadAllText(localFilename + "rainMap.html");
                satelliteMapToRead = File.ReadAllText(localFilename + "satelliteMap.html");
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
Net系统;
使用System.Text.RegularExpressions;
使用System.IO;
使用unfreez_包装器;
使用Shell32;
名称空间下载图像
{
公共部分类Form1:Form
{
字符串映射器;
字符串UrlsPath;
整数计数器;
解冻;
字符串localFilename;
字符串stringForSatelliteMapUrls;
字符串卫星捕捉器;
列出StartTags;
列出最新标签;
列出地图;
提取图像;
公共表格1()
{
初始化组件();
使用(WebClient=newWebClient())
{
client.DownloadFile(“http://www.sat24.com/foreloop.aspx?type=1&continent=europa#,localFilename+“rainMap.html”);
client.DownloadFile(“http://www.sat24.com/en/eu?ir=true,localFilename+“satelliteMap.html”);
}
rainMapToRead=File.ReadAllText(localFilename+“rainMap.html”);
satelliteMapToRead=File.ReadAllText(localFilename+“satelliteMap.html”);
localFileName位于目录路径之前。 但是现在我没有定义它,所以它是空的。 但是即使是空的,rainMapToRead也不是空的,并且能够找到并读取“rainMap.html”

我的意思是,如果变量localFilename是null,那么文件被下放到?C:?D:?
如果为null,默认位置是什么?

连接null valid,您不会得到任何异常。这就是正在发生的情况

null + "satelliteMap.html" = "satelliteMap.html"
如果是相对位置,则文件将存储在exe的Physycal位置

相反,空字符串不引用System.string对象的实例,任何对空字符串调用方法的尝试都会导致NullReferenceException。但是,可以在与其他字符串的串联和比较操作中使用空字符串

concat(
+
)运算符只将
null
视为空字符串

使用的路径是工作目录。

因为这是
(null+“hello”)
C.
中完全合法的表达式

如果您仔细观察,您会发现以下语句:

在字符串连接操作中,C#编译器处理空值 字符串与空字符串相同,但不转换值 原始空字符串的


默认位置与可执行文件的位置相同

因此,如果您的可执行文件在
C:\MyProgram\
上运行,那么
WebClient
会将文件下载到
C:\MyProgram\rainMap.html
。而您的
rainMapToRead
将从
C:\MyProgram\rainMap.html
读取


这是因为
null+“String”
==“code>”String”

在字符串串联中使用null将导致空字符串。
Console.WriteLine(null+“foo”);
打印
foo