Category: quiz

Some code

What you do not like about this code ? ( I have found 3 things – how many can you find?)

public static MyNewDocument  OpenDocument(string FileDocPath , out int codeError)
{

    if (string.IsNullOrEmpty(FileDocPath) || !File.Exists(FileDocPath))
    {
            codeError = 1;
            return null;
    }

    MyNewDocument doc = null;
    try
    {
              doc = new MyNewDocument(FileDocPath);
              if (doc == null)
                    throw new Exception();
              codeError = 0;
              return doc;
     }
     catch
    {
              codeError = 2;
              return null;
    }

}

path.combine idiosyncrasies

I have this small program :

var s = Path.Combine(@"E:\andrei", UnknownVar);
Console.WriteLine(Directory.GetFiles(s).FirstOrDefault());

In what conditions , if I have an “C:\andrei” folder with a single file , the output will be the name of the file ?
And where you want to be aware of this behaviour ?
Hint : The output is : \andrei\New Text Document.txt

Find the Error

This code was written by me, in a late night moment, when copy paste seems the best option
It started this way :

public string ImportFeed(string URL)
        {
            string ret = "";
            XmlDocument xd = new XmlDocument();
            xd.Load(URL);            
            for(int i=0;i<xd.ChildNodes.Count;i++)
            {
                ret += xd.ChildNodes[i].Name;
            }
            return ret;
            
        }

So far , nothing special. Just open the url, read into an XML , return first nodes. Then I wanted more – next nodes. Nothing easier – why bother with recursion or proper variable names ? Just copy and paste :

public string ImportFeed(string URL)
        {
            string ret = "";
            XmlDocument xd = new XmlDocument();
            xd.Load(URL);            
            for(int i=0;i<xd.ChildNodes.Count;i++)
            {
                ret += xd.ChildNodes[i].Name;
                for (int j = 0; i < xd.ChildNodes[i].ChildNodes.Count; j++)
                {
                    ret += xd.ChildNodes[i].ChildNodes[j].Name;
                }
            }
            return ret;
            
        }

Could you spot the error ?
Hint : I have discovered after 10 seconds …but not looking at the code…

C# quiz

Fast small quiz for sunny afternoon :
What is writing at the console those lines : ( please do not use a compiler) :


            decimal? v = 100;
            Console.WriteLine(v);
            decimal? x = null;
            x += v.Value;
            Console.WriteLine(x);

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.