转换 x时出现问题到x

发布于 2024-09-02 17:28:34 字数 832 浏览 1 评论 0原文

嘿伙计们,我无法使用这段代码进行管理。这个想法是在创建方法执行错误的情况下返回默认的英文字母。谢谢。

覆盖显式运算符的想法很好,但我无法想象强制转换的实现。

namespace trie
    {
        class AlphabetFactory<T> where T: IConvertible
        {
            public Alphabet<char> SetDefaultAlphabet()
            {
                var list = new char[26];
                for (var i = Convert.ToInt32('a'); i <= Convert.ToInt32('z'); i++)
                    list[i] = Convert.ToChar(i);
                return new Alphabet<char>(list);
            }

            public Alphabet<T> Create(params T[] list)
            {
                if (list != null)
                    return new Alphabet<T>(list);
                else
                    return SetDefaultAlphabet();    // <- This is not correct   
            }
        }
    }

Hey guys I cant manage with this code. The idea is to return default English alphabet in case of erroneous create method execution. Thanks.

An idea to override explicit operator is good, but i cant imagine an implementation of casting.

namespace trie
    {
        class AlphabetFactory<T> where T: IConvertible
        {
            public Alphabet<char> SetDefaultAlphabet()
            {
                var list = new char[26];
                for (var i = Convert.ToInt32('a'); i <= Convert.ToInt32('z'); i++)
                    list[i] = Convert.ToChar(i);
                return new Alphabet<char>(list);
            }

            public Alphabet<T> Create(params T[] list)
            {
                if (list != null)
                    return new Alphabet<T>(list);
                else
                    return SetDefaultAlphabet();    // <- This is not correct   
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-09-09 17:28:34

我认为完成您想要的任务的唯一方法是使 Alphabet从非泛型抽象基继承,或者实现非泛型接口。那么您的代码可能如下所示:

interface IAlphabet {
    // ?
}

class Alphabet<T> : IAlphabet {
    // stuff
}

class AlphabetFactory {
    // ...

    IAlphabet Create<T>(params T[] list) where T : IConvertible {
        if (list != null)
            return new Alphabet<T>(list);
        else
            return SetDefaultAlphabet();
    }

    // ...
}

不过,这将使使用 AlphabetFactory 的客户端代码使用起来更加麻烦:

int[] integers = GetIntegers(); // let's say this might be null

var alphabet = myAlphabetFactory.Create(integers);

var integerAlphabet = alphabet as Alphabet<int>;
if (integerAlphabet != null) {
    // do stuff with integerAlphabet
} else {
    // alphabet is an IAlphabet, but not an Alphabet<int>
}

The only way I can think to accomplish what you want is to make Alphabet<T> inherit from a non-generic abstract base, or else implement a non-generic interface. Then your code might look something like this:

interface IAlphabet {
    // ?
}

class Alphabet<T> : IAlphabet {
    // stuff
}

class AlphabetFactory {
    // ...

    IAlphabet Create<T>(params T[] list) where T : IConvertible {
        if (list != null)
            return new Alphabet<T>(list);
        else
            return SetDefaultAlphabet();
    }

    // ...
}

This would make client code using your AlphabetFactory a bit more cumbersome to use, though:

int[] integers = GetIntegers(); // let's say this might be null

var alphabet = myAlphabetFactory.Create(integers);

var integerAlphabet = alphabet as Alphabet<int>;
if (integerAlphabet != null) {
    // do stuff with integerAlphabet
} else {
    // alphabet is an IAlphabet, but not an Alphabet<int>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文