user2760148
user2760148

Reputation: 427

Why i'm not getting null exception if the variable is null?

This is the code:

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");

localFileName was before a path to a directory. But now i didn't define it so it's null. But even when it's null the rainMapToRead is not null and was able to find and read the "rainMap.html"

I mean if the variable localFilename is null where the file was downloded to ? C: ? D: ? In case it's null what is the default location ?

Upvotes: 2

Views: 121

Answers (4)

Simon Whitehead
Simon Whitehead

Reputation: 65079

The default location is the same location as the executable.

So, if your executable is running at C:\MyProgram\, the WebClient will download the file to C:\MyProgram\rainMap.html.. and your rainMapToRead will read from C:\MyProgram\rainMap.html.

This is because null + "String" == "String"

Upvotes: 0

Tigran
Tigran

Reputation: 62246

bacause this (null + " hello") is perfectly legitimate expression in C#.

If you look on How to: Concatenate Multiple Strings (C# Programming Guide), you can find following statement:

In string concatenation operations, the C# compiler treats a null string the same as an empty string, but it does not convert the value of the original null string.

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190944

The concat (+) operator just treats null as an empty string.

The path used is the working directory.

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Concatenating null valid, you won't get any exception. This is what is happening

null + "satelliteMap.html" = "satelliteMap.html"

Provided that's a relative location, the file will be stored on the phisycal location of the exe.

From MSDN

By contrast, a null string does not refer to an instance of a System.String object and any attempt to call a method on a null string causes a NullReferenceException. However, you can use null strings in concatenation and comparison operations with other strings.

Upvotes: 3

Related Questions