克服C#限制 [英] Overcoming C# limitations

查看:57
本文介绍了克服C#限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




由于C#语言的限制,我最近遇到了一些设计问题。

以下是场景:


1.假设我有很多函数我想要b
添加一个可选参数。由于C#不支持可选的

参数,因此我必须复制所有这些函数,并使用其他参数覆盖

。这真的有必要吗?它只是我或者结果是丑陋的吗?


2.在C ++中我可以定义一个宏来实例化一个对象并且

然后进行方法调用。例如:

#define MY_FUNC(x)MyObj a; a.func(x);

这本来是有用的,因为我可以在任何范围内使用这个宏

知道MyObj(或实例a)的析构函数将是在

调用范围的末尾而没有明确地调用任何函数。对于

例如:

void Foo()

{

MY_FUNC(3);

....更多代码....

}< - MyObj的析构函数将在这里调用


现在回到C#,预处理器是非常有限的所以我无法定义

那个宏。

如果我将这两行代码封装在一个方法中,我显然会松散

这个优点,所以我留下复制并粘贴代码块

无论何时需要它。


我知道这可能听起来很愚蠢,它只是习惯了C ++,这些

问题看起来很奇怪。

如果我错过了一个更干净的方法,请告诉我。


谢谢,


Danny

解决方案

< blockquote> 2006年6月18日15:27:32 -0700,Danny Liberty < dl ****** @ gmail.com>

写道:



我有最近由于C#的语言限制而遇到了一些设计问题。
以下是场景:

1。假设我有许多功能,我想添加一个可选参数。由于C#不支持可选的
参数,我必须复制所有这些函数,并用另一个参数覆盖每个函数。这真的有必要吗?它只是我还是丑陋的结果?




你不必复制这些方法。将所有代码放在带有最多参数的

方法调用中,并让其他方法调用

那个,就像这样。


public int MyFunc(string s,int optional)

{

// Code to here ...

}


public int MyFunc(string s)

{

MyFunc(s,10); //假设可选的默认值是10

}


-

MarcusAndrén


你好Danny,

1.假设我有很多函数需要添加一个可选参数。由于C#不支持可选的
参数,我必须复制所有这些函数,并用另一个参数覆盖每个函数。这真的有必要吗?它只是我还是丑陋的结果?


这个话题已经提出了很多,而且即使在微软,也似乎有一些关于它的矛盾心理。但是,最终,他们不得不做出决定,并决定反对。我记得很久以前在微软的一个.Net开发人员那里读了一篇博客文章,但是不记得

到底在哪里。这里有一些讨论

http://weblogs.asp.net/jcogley/archi...08/239646.aspx )以及

链接到其中的视频一些.Net开发人员谈论它。

也许其他人记得我忘记的博客条目。

2.在C ++中我可以定义一个宏来实例化一个对象,然后进行方法调用。例如:
#define MY_FUNC(x)MyObj a; a.func(x);
这本来是有用的,因为我可以在任何范围内使用这个宏
知道将在末尾调用MyObj(或实例a)的析构函数范围没有明确调用任何函数。对于
示例:


C#规范中没有规定宏或内联

函数。我当然可以理解宏的问题(虽然我是

确定它不是不可克服的),这不是类型安全的,但我希望那里

是内联函数的规定,在C ++中是类型安全的,并且我相信不会在C#规范中造成任何问题。所以,我在你身边

那一个。至于第一个,好吧,我可以理解这种矛盾心理,并且在某些时候我们都必须钓鱼或切鱼饵,所以我不是在抱怨它。


-

HTH,


Kevin Spencer

Microsoft MVP

专业鸡肉沙拉炼金术士


我回收利用。

我将所有东西送回它来自的星球。

Danny Liberty ; < DL ****** @ gmail.com>在消息中写道

news:11 ********************* @ g10g2000cwb.googlegro ups.com ...
由于C#的语言限制,我最近遇到了一些设计问题。
以下是场景:

1.假设我有很多我想要添加可选参数的函数数量。由于C#不支持可选的
参数,我必须复制所有这些函数,并用另一个参数覆盖每个函数。这真的有必要吗?它只是我还是丑陋的结果?

2.在C ++中,我可以定义一个宏来实例化一个对象,然后进行方法调用。例如:
#define MY_FUNC(x)MyObj a; a.func(x);
这本来是有用的,因为我可以在任何范围内使用这个宏
知道将在末尾调用MyObj(或实例a)的析构函数范围没有明确调用任何函数。对于
示例:
void Foo()
{
MY_FUNC(3);
....更多代码....
}< - MyObj的析构函数将在这里调用

现在回到C#,预处理器非常有限所以我无法定义那个宏。
如果我封装了这两行代码在一种方法中,我显然已经放松了这个优势,所以我不得不复制并粘贴那段代码
无论何时需要它。

我知道这可能声音很愚蠢,它只是习惯了C ++,这些问题看起来很奇怪。
如果我错过了一个更干净的方法,请告诉我。

谢谢,

Danny



MarcusAndrén写道:

2006年6月18日15: 27:32 -0700,Danny Liberty < dl ****** @ gmail.com>
写道:


我最近遇到过一些设计由于C#语言的局限性问题。
以下是这些场景:

1.假设我有很多函数需要添加一个可选参数。由于C#不支持可选的
参数,我必须复制所有这些函数,并用另一个参数覆盖它们中的每一个。这真的很必要吗?它只是我还是丑陋的结果?



你不必复制这些方法。将所有代码放在带有最多参数的
方法调用中,并让其他方法调用那个,就像这样。

public int MyFunc(string s,int optional) )
{代码来到这里......

public int MyFunc(string s)
{
MyFunc(s ,10); //假设可选的默认值是10
}




....虽然这并不像可选参数那样优雅,但是您必须编写的源文本数量,在运行时它几乎是一个洗,

,也许实际上更好。使用可选参数,编译器必须在每个调用站点实例化可选参数,而转发

函数仅实例化可选参数一次。虽然没有

保证,但JIT完全可以内联转发

函数,从而在运行时产生与您相同的性能。使用C ++解决方案可以获得



-cd


Hi,

I''ve recently encountered some design problems due to the language
limitations of C#.
Here are the scenarios:

1. Suppose I have a numerous number of functions that I would like to
add an optional parameter to. Since C# does not support optional
parameters, I would have to duplicate all these functions and override
each of them with another parameter. Is this really necessary?? Is it
just me or is the result kind of ugly?

2. In C++ I could have defined a macro to instantiate an object and
then make a method call. For example:
#define MY_FUNC(x) MyObj a; a.func(x);
This would have been useful since I could use this macro in any scope
knowing that the destructor of MyObj (or instance a) will be called at
the end of the scope without explictly calling any function. For
example:
void Foo()
{
MY_FUNC(3);
.... more code....
} <-- destructor of MyObj would be called here

Now back to C#, the preprocessor is very limited so I cannot define
that macro.
If I encapsulate these two lines of code in a method I obviously loose
this advantage, and so I''m left copying and pasting that block of code
wherever it''s needed.

I know this may sound stupid, it''s just being used to C++, these
problems seem rather odd.
If I''m missing a cleaner way to do this please let me know.

Thanks,

Danny

解决方案

On 18 Jun 2006 15:27:32 -0700, "Danny Liberty" <dl******@gmail.com>
wrote:

Hi,

I''ve recently encountered some design problems due to the language
limitations of C#.
Here are the scenarios:

1. Suppose I have a numerous number of functions that I would like to
add an optional parameter to. Since C# does not support optional
parameters, I would have to duplicate all these functions and override
each of them with another parameter. Is this really necessary?? Is it
just me or is the result kind of ugly?



You don''t have to duplicate the methods. Place all the code in the
method call with the most parameters, and have the other methods call
that one, like this.

public int MyFunc(string s,int optional)
{
// Code goes here ...
}

public int MyFunc(string s)
{
MyFunc(s,10); //Assuming default value for optional is 10
}

--
Marcus Andrén


Hi Danny,

1. Suppose I have a numerous number of functions that I would like to
add an optional parameter to. Since C# does not support optional
parameters, I would have to duplicate all these functions and override
each of them with another parameter. Is this really necessary?? Is it
just me or is the result kind of ugly?
This subject has been brought up quite a bit, and there seems to be some
ambivalence about it even at Microsoft. However, in the end, they had to
make a decision, and decided against it. I remember reading a blog entry by
one of the .Net developers at Microsoft some time ago, but don''t remember
exactly where it is. There is some discussion of it here
(http://weblogs.asp.net/jcogley/archi...08/239646.aspx) as well as a
link to a video in which several of the .Net developers talk about it.
Perhaps someone else remembers the blog entry I have forgotten about.
2. In C++ I could have defined a macro to instantiate an object and
then make a method call. For example:
#define MY_FUNC(x) MyObj a; a.func(x);
This would have been useful since I could use this macro in any scope
knowing that the destructor of MyObj (or instance a) will be called at
the end of the scope without explictly calling any function. For
example:
There is no provision in the C# specification for either macros or inline
functions. I can certainly understand the problem with macros (although I''m
sure it is not insurmountable), which are not type-safe, but I do wish there
was a provision for inline functions, which are type-safe in C++, and would
not, I believe, pose any problems in the C# spec. So, I''m on your side on
that one. As for the first, well, I can understand the ambivalence, and at
some point we all have to fish or cut bait, so I''m not complaining about it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

I recycle.
I send everything back to the planet it came from.
"Danny Liberty" <dl******@gmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com... Hi,

I''ve recently encountered some design problems due to the language
limitations of C#.
Here are the scenarios:

1. Suppose I have a numerous number of functions that I would like to
add an optional parameter to. Since C# does not support optional
parameters, I would have to duplicate all these functions and override
each of them with another parameter. Is this really necessary?? Is it
just me or is the result kind of ugly?

2. In C++ I could have defined a macro to instantiate an object and
then make a method call. For example:
#define MY_FUNC(x) MyObj a; a.func(x);
This would have been useful since I could use this macro in any scope
knowing that the destructor of MyObj (or instance a) will be called at
the end of the scope without explictly calling any function. For
example:
void Foo()
{
MY_FUNC(3);
.... more code....
} <-- destructor of MyObj would be called here

Now back to C#, the preprocessor is very limited so I cannot define
that macro.
If I encapsulate these two lines of code in a method I obviously loose
this advantage, and so I''m left copying and pasting that block of code
wherever it''s needed.

I know this may sound stupid, it''s just being used to C++, these
problems seem rather odd.
If I''m missing a cleaner way to do this please let me know.

Thanks,

Danny



Marcus Andrén wrote:

On 18 Jun 2006 15:27:32 -0700, "Danny Liberty" <dl******@gmail.com>
wrote:

Hi,

I''ve recently encountered some design problems due to the language
limitations of C#.
Here are the scenarios:

1. Suppose I have a numerous number of functions that I would like to
add an optional parameter to. Since C# does not support optional
parameters, I would have to duplicate all these functions and
override each of them with another parameter. Is this really
necessary?? Is it just me or is the result kind of ugly?



You don''t have to duplicate the methods. Place all the code in the
method call with the most parameters, and have the other methods call
that one, like this.

public int MyFunc(string s,int optional)
{
// Code goes here ...
}

public int MyFunc(string s)
{
MyFunc(s,10); //Assuming default value for optional is 10
}



.... and while this isn''t as elegant as optional parameters, in terms of the
amount of source text you have to write, at runtime it''s pretty much a wash,
and perhaps actually better. With optional parameters, the compiler has to
instantiate the optional parameters at every call site, while the forwarding
function instantiates the optional parameters only once. While there''s no
guarantee, it''s entirely possible that the JIT will inline the forwarding
function, resulting in exactly the same performance at runtime as you''d have
with the C++ solution.

-cd


这篇关于克服C#限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆