Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.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# 如何在C中说“不是对象”?_C# - Fatal编程技术网

C# 如何在C中说“不是对象”?

C# 如何在C中说“不是对象”?,c#,C#,我有一个类Issue,它应该只在文件名为特定格式时执行其逻辑: namespace Site.Models { public class Issue { String FilePath { get; set; } String FileName { get; set; } String HTML {get; set;} public Issue(String path) {

我有一个类Issue,它应该只在文件名为特定格式时执行其逻辑:

namespace Site.Models
{
    public class Issue
    {
        String FilePath { get; set; }
        String FileName { get; set; }
        String HTML {get; set;}

        public Issue(String path)
        {
            FilePath = path;
            FileName = Path.GetFileName(FilePath);

            if(FileName.StartsWith(".")){
                // How do I say "Not an Issue; don't continue logic"
            }

            HTML = "<li>" + FileName + "</li>;
        }
    }
}

我可以把所有的逻辑放在一个else中,但如果可能的话,我真的希望避免不必要的条件。另外,我很好奇是否有办法做到这一点。

在C中,一旦输入构造函数,就无法避免构造对象

当然,您可以从该方法返回,而不执行剩余的代码,但这将为您提供一个很可能不处于有用状态的对象。另一种选择是抛出异常,但这应该保留用于异常情况,否则您的程序将很快陷入基于try-catch的流控制的混乱中


通常,您不希望在不进行基本验证(确保构造首先成功)的情况下调用构造函数。如果这是一个常见的操作,您可以为它创建一个工厂方法。

在If语句中添加缺少的括号,并用感叹号检查false

if (!FileName.StartsWith(".")) {
    HTML = "<li>" + FileName + "</li>;
}

非常快速的解决方案,使用异常停止逻辑:

public class Issue
    {
        String FilePath { get; set; }
        String FileName { get; set; }
        String HTML {get; set;}

        Exception NotIssueException;

        public Issue(String path)
        {
            NotIssueException = new Exception("Not an Issue Exception");
            FilePath = path;
            FileName = Path.GetFileName(FilePath);

            if(!FileName.StartsWith("."))
            {
                throw NotIssueException;
            }
            else
            {
                //Do Your Logic
            }

            HTML = "<li>" + FileName + "</li>;
        }
    }

然后可以使用Try\Catch块来管理此异常

您不需要在构造函数中执行验证逻辑,而是希望将其移动到某种形式的工厂方法,并在那里执行检查

namespace Site.Models
{
    public class Issue
    {
        String FilePath { get; set; }
        String FileName { get; set; }
        String HTML { get; set; }

        private Issue(String path)
        {
            FilePath = path;
            FileName = Path.GetFileName(path);
            HTML = "<li>" + FileName + "</li>;
        }

        public static Issue CreateIssue(String path)
        {

            var fileName = Path.GetFileName(path);

            if (fileName.StartsWith("."){
                 throw new Exception("Not an issue");
                // or you could return null and handle that in the calling code
            }

            return new Issue(path);

        }
    }
}
然后,不再调用new Issuepath,而是调用Issue.CreateIssuepath并捕获异常或检查null返回


编辑:添加了缺少的返回类型

如何在Issue中创建一个静态方法来创建Issue的实例

namespace Site.Models
{
public class Issue
{
    String FilePath { get; set; }
    String FileName { get; set; }
    String HTML {get; set;}

    public Issue(String filename)
    {
        HTML = "<li>" + FileName + "</li>;
    }

    public static Issue getIssue(String path)
    {
        FilePath = path;
        FileName = Path.GetFileName(FilePath);

        if(!FileName.StartsWith("."){
            return new Issue(FileName);
        }
        else
        {
            //throw exception or whatever you want to do to handle it
        }
     }
}
}

如果FileName.StartsWith。?使用正则表达式检查正确的模式只需使用return语句return;别忘了把ifFileName.StartsWith括起来。@TravisHeeter,我想是缺乏研究吧。这个问题不大可能对其他人有用。否决投票不是仇恨,它是一种统计工具,用来标记问题不太好。我不想把所有的逻辑都放在假设中。这个例子是一个简化的例子,实际的逻辑有更多的运动部件。我也很好奇是否有办法做到这一点。@TravisHeeter我真的不明白你想做什么。。。你想用另一种方式做这个答案中的事情?我对C不太了解,但也许你可以用?:?我正在发送一个文件路径。它应该执行对pdf的逻辑,但文件夹中有很多垃圾,与真正的pdf同名,但以“.”开头,我想忽略这些文件。@TravisHeeter那么这是你的最佳选择,除非你愿意在这个类之外做事情;编辑:我认为接受的答案是一个很好的选择,实际上+1对于一个工厂来说,你忘记添加返回类型Woops,现在修复了。。谢谢你让我知道。工厂模式在这里有什么帮助?