PC Review


Reply
Thread Tools Rate Thread

What to do to make compiler consider STATIC Readonly properties as CONSTANT?

 
 
Leon_Amirreza
Guest
Posts: n/a
 
      4th Dec 2009
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-?

I thought compliers exist to encourage better programming practice (that is
more readable, more protable, more writtabe, more maintainable code )?
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?

I can make many c# examples that compiler needs a constant, while it can
evaluate the expression to a constant, but it doesnt!
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?

 
Reply With Quote
 
 
 
 
Peter Duniho
Guest
Posts: n/a
 
      4th Dec 2009
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
 
Reply With Quote
 
Leon_Amirreza
Guest
Posts: n/a
 
      12th Dec 2009
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


 
Reply With Quote
 
Leon_Amirreza
Guest
Posts: n/a
 
      12th Dec 2009
these are const fields and are queryable from .net platform at compile or
runtime which ever more appropriate.
these types are not going to change at runtime and at least compiler can
consider them as literal constants.
I guess in IL language it is possible to have somthing that acts like line
NO 1- but only impossible in C#!

"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


 
Reply With Quote
 
Marvin Landman
Guest
Posts: n/a
 
      12th Dec 2009
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

>

 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      12th Dec 2009
Leon_Amirreza wrote:
> What About Environment.NewLine?


What about it?

> it is there for more portable code!


Specifically, because what constitutes a "new line" varies from
operating system to operating system (among other things...even on
Windows, some programs use only '\n' for a new line, so to some extent
it's even more variable than per-platform).

> any literal exits in c# for that ?


No environment-dependent literal, no. Obviously if you know the context
(e.g. HTML), you can always hard-code "\r\n" or "\n" as appropriate.

What this has to do with String.Empty, I don't know. String.Empty isn't
platform dependent, and always is the same as "".

Pete
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      12th Dec 2009
Leon_Amirreza wrote:
> [...]
> I guess in IL language it is possible to have somthing that acts like
> line NO 1- but only impossible in C#!


There's a lot that is supported in IL or the CLR, but which is
unavailable from the various .NET languages, including C#.

You are free to code in IL if you like.

Pete
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What to do to make compiler consider STATIC Readonly properties as CONSTANT? Leon_Amirreza Microsoft C# .NET 1 4th Dec 2009 06:00 AM
readonly properties when displaying an object's properties in propertygrid movieknight@gmail.com Microsoft VB .NET 0 14th May 2006 06:48 AM
Visual Inheritance - How to make public inherited properties private or readonly? Michael.Suarez@gmail.com Microsoft C# .NET 2 23rd Mar 2006 09:49 PM
Static Readonly Properties and Thread Safety The Crow Microsoft C# .NET 4 15th Jul 2005 10:58 AM
Can you make a static constant? Phill Microsoft C# .NET 2 16th Nov 2004 01:36 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:44 AM.