接受 Object 作为参数可以接受吗?
假设我有一个文本框或任何其他形式的输入,要求提供社会安全号码。 我确实想指出,SSN 只是我现在想到的一个纯粹的例子。 该输入最初自然会存储为字符串。
string s = Console.ReadLine();
假设我想要一种验证 SSN 的方法,并且它可以在我的代码中的各种地方使用。 哎呀,我什至可以在尚未由用户输入确定的变量上调用该方法。
这是可以接受的吗?
public bool IsValidSSN(Object SSN)
{
int mySSN;
if(Int.Parse(SSN == false)
{
mySSN = Convert.toInt32(SSN);
}
...
}
或者你会坚持我要求特定的数据类型,例如,
public bool IsValidSSN(int SSN)
{
...
}
因此我需要在调用方法之前将输入转换为正确的数据类型。
顺便说一句:我不是在问如何执行正确的 IsValidSSN 代码:) 我只是想举一个例子来说明我所说的意思:我可以接受对象数据类型作为参数还是应该尽量避免它?
Let us say I have a textbox or any other form of input that asks for a social security number. I do want to note that the SSN is a pure example I simply thought of as of right now.
This input will naturally be stored as a string initially.
string s = Console.ReadLine();
Let us say I want to have a method that validates an SSN and it might be used throughout my code in all sorts of places. Heck, I might even call the method on a variable which has not been determined by user-input.
Is this acceptable?
public bool IsValidSSN(Object SSN)
{
int mySSN;
if(Int.Parse(SSN == false)
{
mySSN = Convert.toInt32(SSN);
}
...
}
Or would you guy insist that I ask for a specific datatype, e.g
public bool IsValidSSN(int SSN)
{
...
}
and therefor I am required to convert the input to the correct datatype BEFORE I call the method on it.
BTW: I am not asking how to do a proper IsValidSSN code :) I just wanted to give an example of what I meant when I said: Can I accept the Object datatype as a parameter or should I try to avoid it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您必须接受一个对象,我至少会重载采用强类型参数的方法。 然后将对象变体输入到这些方法中。
If you must accept an object I would at least have overloads of the method which take strongly typed parameters. Then have the object variants feed into these methods.
就我个人而言,我会创建一个 SSN 类,并能够询问该 SSN 是否有效。 拥有代表业务逻辑中主体的类非常重要。 例如,这就是您可能对需要更多验证的事物(例如信用卡类)所做的事情。 如果您可以避免它,则传递对象并不是最好的,并且将业务逻辑中的主体作为原语传递也很糟糕(您的架构使 SSN1 + SSN2 = SSN3 完全有效,即使在业务逻辑中,这是无意义的)。
Personally, I would make an SSN class and be able to ask that SSN if it was valid or not. It's important to have classes that represent principals in your business logic. This is very much, for example, what you might do with something that requires more validation like a credit card class. Handing around objects is not the best if you can avoid it and handing around something that is a principal in your business logic as a primitive is bad too (your architecture makes SSN1 + SSN2 = SSN3 perfectly valid, even though in business logic, it's nonsense).
在上面的示例中,创建类型化的 IsValidSSN 要容易得多。 一般来说,我发现打字可以减少错误并提高灵活性。
在灵活性至关重要的情况下,使用 Object 可能是最佳选择,但预计会在日志中遇到一些强制转换冲突异常。
对于所有其他情况,请严格输入,或者用 python 编写。
In your above example it is far easier to create a typed IsValidSSN. Generally I find that typing reduces bugs and flexibility.
Under circumstances in which flexibility if paramount then using Object is probably the best choice, but expect to face a few cast clash exceptions in the logs.
For all other cases be strict with your typing, or write it in python.
在这种情况下,我认为接受对象没有任何价值。 仔细考虑一下您期望该功能如何工作。 (显然你没有,因为你发布的代码不起作用)。 我认为您正在计划这样的事情:
这比:
重载方法更简单、更快,因为它们更多地决定从运行时到编译时要做什么。
I see no value in accepting an Object in this case. Think through how you expect that function to work. (Clearly you haven't, since the code you posted doesn't work). I think you're planning something like this:
How is that better than:
The overloaded methods are simpler, and faster, since they more the decision on what to do from runtime to compile time.
它完全取决于您的设计以及您希望验证发生的位置。 它实际上从根本上取决于您的整体架构和类层次结构。 无论哪种方式都没有错; 只需确保它适合您的架构设计即可。
It COMPLETELY depends on your design and where you want your validation to occur. It really fundamentally depends upon your overall architecture and your class hierarchy. It's not wrong to do it either way; just be sure that it's the way that fits with your architectural design.
在这种情况下,我想说这是不可接受的。 如果输入有破折号或其他分隔字符(例如:###-##-####)怎么办? 显然您无法将该值解析为整数,但该值仍然有效。 如何使用正则表达式来确保该值是您想要的值。
就使用类型“Object”作为参数而言,这在许多情况下是完全有效的。 事实上,它在整个 .NET Framework 中都有使用(查看事件委托):
这将是装箱/拆箱的简单情况,这实际上是 .NET 2.0 之前对变量执行“通用”操作的唯一方法。
您还可以使用泛型来解决此问题,而无需进行强制转换。 如果您创建一个实现 INumeric (我不知道这是否是实际接口)或 IComparable 之类的接口,您应该能够以更优雅的方式执行操作:
public bool IsValidSNN(INumeric SSN){}
In this instance, I would say it was unacceptable. What if the input has dashes, or some other separating character (eg: ###-##-####)? You obviously wouldn't be able to parse the value as an integer, but the value would still be valid. How about using a regular expression instead to ensure the value is what you've desired.
In terms of using the type "Object" as a parameter, this is completely valid in many instances. In fact, it is used throughout the .NET Framework (look at event delegates):
This would be a simple case of Boxing/Unboxing, which was really the only way of performing "Generic" operations on variables until .NET 2.0.
You can also use Generics to solve this problem without the need for casting. If you create an interface that implements something like INumeric (I don't know if that is the actual interface) or IComparable you should be able to perform the operation in a more elegant fashion:
public bool IsValidSNN(INumeric SSN){}