From: Leon_Amirreza on 4 Dec 2009 00:36 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?
From: Leon_Amirreza on 12 Dec 2009 12:54 What About Environment.NewLine? it is there for more portable code! any literal exits in c# for that ? "Peter Duniho" <no.peted.spam(a)no.nwlink.spam.com> wrote in message news:eLKGxbKdKHA.6096(a)TK2MSFTNGP02.phx.gbl... > 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
From: Leon_Amirreza on 12 Dec 2009 13:03 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" <no.peted.spam(a)no.nwlink.spam.com> wrote in message news:eLKGxbKdKHA.6096(a)TK2MSFTNGP02.phx.gbl... > 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
|
Pages: 1 Prev: Button Array Next: What to do to make compiler consider STATIC Readonly propertiesas CONSTANT? |