开发者

What is wrong with this c# method?

I use this method to get file extension,

public string ReturnExtension(string fileExtension)
    {
        switch (fileExtension)
        {
            case ".doc":
            case ".docx":
                return "application/ms-word";
        }
    }
开发者_运维百科

When i compile it i got the error BaseClass.ReturnExtension(string)': not all code paths return a value.. Any suggestion...


You need to add a default condition if you are returning from inside the switch statement.

// As SLaks mentioned, you should be case in-sensitive.
// Therefore, I'm comparing only the Lower Case version of the extensio

switch(fileExtension.ToLowerInvariant())
{
    case ".doc":
    case ".docx":
        return "application/ms-word";
    default:
        // You could also throw an Exception here if unknown extensions
        // are truly invalid (which seems to be the more popular choice)
        //
        // Since it looks like you're returning a content-type for a
        // download, I would default to octet-stream so the user would
        // just get a download window.
        return "application/octet-stream";
}


You haven't specified what the method should return if fileExtension is not ".doc" or ".docx". You can do this by adding a default case to the switch statement. Assuming that other fileExtension values are invalid, you should throw an exception:

public string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".doc":
        case ".docx":
            return "application/ms-word";
        default:
            throw new ArgumentException(string.Format("Invalid fileExtension '{0}'.", fileExtension));
    }
}


You need a default or you need to return something outside the switch.

default:
        Console.WriteLine("Default case");
        return "";


This is one of the better error messages a compiler can issue. It means exactly that: Not all code paths return a value.

A code path is created by branching statements like while, if and switch.

The compiler is trying to guarantee, that whenever the runtime executes this function, a value will be returned of the specified type (here: string).

In your example, the return value for a fileextension not in the set (doc, docx) is not defined. You can either

  • specify a default clause in your switch statement
  • or add a catch-all return statement at the end of your method (return "text/plain"?)


The compiler derives a control flow graph from our source and sees that not all paths are covered, for example if you pass in a fileExtension ".rtf" this function can not provide a return value.

So either provide a return "something", at the end of the function or simply throw an exception for the switch's "default:" case. You have to decide about what the function needs to do in the cases when it neither sees a ".doc" or ".docx".


Ask yourself what happens when fileExtension is .xls. You have to return something in the default-case or simply after the switch statement:

public string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".doc":
        case ".docx":
            return "application/ms-word";
    }
    return "unknown";  // this path wasn't returning anything
}


You need a default and break, or a break on your last case statement.


Just my $0.10, it's pretty clear that the content header is being returned here, so if we don't recognize the file extension, rather than throw exceptions or invalid MIME header information (e.g., "unknown"), we should return the file to the browser as an octect stream, like this:

public string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".doc":
        case ".docx":
            return "application/ms-word";
        default:
            return "application/octet-stream";
    }
}

This way we can accommodate scenarios where 5 years down the road, M$ releases MS-Word v30.1, and the extension becomes ".docz", the system will not throw exceptions, but invoke MS-Word (more) gracefully, albeit not using the IE-enhanced behavior that "application/ms-word" will.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜