Just some thoughts on the problems mentioned below:
String.Empty is a field because that can contain an object reference so
you will get the same object reference every time you use string.Empty.
An empty string constant on the other hand is read form the metadata of
the assembly using it.
Attribute constructor arguments are serialized (never executed) so you
have to use constant values and String.Empty is a field. Compile time
code execution would make no sense because the compilation environment
can differ from the execution environment.
Although readonly fields can actually vary from version to version and
implementation to implementation using a property (a special method)
call for Environment.NewLine makes sense since that is ment to be
platform dependent. If you find that calling an extra methods hurts that
much your programs performance, you can store its value in a field or a
local variable.
Marvin
Leon_Amirreza wrote:
> What About Environment.NewLine?
> it is there for more portable code! any literal exits in c# for that ?
>
> "Peter Duniho" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Leon_Amirreza wrote:
>>> Hi
>>> when i used the following attribute for a property:
>>> 1- [DefaultValue(string.Empty)]
>>>
>>> c# compiler nagged that it expects a constant and forced me to change
>>> it to this
>>> 2- [DefaultValue("")]
>>>
>>> Any1 knows of a better programming style than line No. 2-?
>>
>> No.
>>
>>> I thought compliers exist to encourage better programming practice
>>> (that is more readable, more protable, more writtabe, more
>>> maintainable code )?
>>
>> What's wrong with ""? Is there some difficulty understanding what it
>> means? What if the initialization value was a non-empty string? Why
>> is it that "My Non-Empty String" is okay, but "" is not?
>>
>>> Thank god for the reflection for that DotNet compilers can evaluate
>>> some of expressions to constant values at compile time then why
>>> doesnt c# do that?
>>
>> Actually, I hope there's no compiler out there evaluating String.Empty
>> at compile time. It would set a horrible precedent.
>>
>>> I can make many c# examples that compiler needs a constant, while it
>>> can evaluate the expression to a constant, but it doesnt!
>>
>> That's right. Expressions that are not already compile-time literals
>> cannot be proven by the compiler to not change at run-time, and thus
>> should NOT be evaluated for the purpose of declaring a constant.
>>
>>> there are alot of languages for windows and linux (like gnu C++ or
>>> any other variants of C/C++, Embarcadero Delphi - previously Borland
>>> Delphi -, ...) that can not or do not benefit from a platform like
>>> DotNet.
>>> As a DotNet Programmer I can benefit from the platform in many ways
>>> (while some drawbacks) but why the compiler itself doesnt benefit
>>> from some of features that already there?
>>
>> The compiler does benefit from features that are already there, at
>> times when it's reasonable to do so. But allowing String.Empty as a
>> compile-time literal isn't one of those times.
>>
>> Pete
>
|