DevPinoy.org
A Filipino Developers Community
   

Another Trivia

rated by 0 users
This post has 17 Replies | 1 Follower

Top 10 Contributor
Posts 2,038
Points 42,030
keithrull Posted: 08-02-2007 6:28 PM

Can you tell me whats wrong with the program below without running the program?

namespace KeithRull.StaticConstCompare
{
    class Program
    {
        static readonly int x = 5;
        const int y = 5;
 
        static void Main(string[] args)
        {
            x = y + 7;
 
            Console.WriteLine("x:{0}", x);
            Console.WriteLine("y:{0}", y);
 
            Console.ReadLine();
        }
    }
}

Why did it happen? Whats the diffrence between readonly and const in C#?

devpinoy sig

  • | Post Points: 20
Top 25 Contributor
Posts 193
Points 3,315
keithrull:
            x = y + 7;
Compilation error on the above line.
keithrull:
Why did it happen?
The compiler allows readonly fields to be reassigned only on constructors. The modifier on the field is also important: x is a static readonly field, so assignments to it are only allowed on a static constructor.
keithrull:
Whats the diffrence between readonly and const in C#?

Const is bound during compile time. Readonly is bound during runtime. :D

[jop]

  • | Post Points: 20
Top 10 Contributor
Posts 2,038
Points 42,030

hahah! bilis ni master jop ha :P

 Additional question: So when should i use const over readonly?

devpinoy sig

  • | Post Points: 35
Top 50 Contributor
Posts 27
Points 450

mga sir pasingit ha. hehe.

i think we should use readonly if that readonly field's value depends on other fields at runtime.

Ex.

private readonly int sum = a + b;

 

Pag constants, I believe you still have to recompile the assembly in case you want to change the value of the constant.

Tama po ba?

Success is 1% Inspiration and 99% Perspiration.. - T. Edison -
  • | Post Points: 5
Top 25 Contributor
Posts 193
Points 3,315
keithrull:

hahah! bilis ni master jop ha :P

Walang ginagawa dito sa trabaho eh... Wallpaper ko tong devpinoy.
keithrull:

Additional question: So when should i use const over readonly?

That's a tricky question. I don't like const. I find that I use readonly most of the time (if I ever find a need for it). :D Const is very limited. You can only assign literal (1.23, 42, "abc") values to it because the values are being resolved by the compiler. You can not assign regular objects to it. Readonly can be used as a const. It is also more descriptive of what it does. Const should be taken out to remove a little language bloat. :D

[jop]

  • | Post Points: 35
Top 10 Contributor
Posts 882
Points 13,550

there's a lot of YAGNI feature in the C# language which makes me hate it!!! 

  • | Post Points: 20
Top 25 Contributor
Posts 193
Points 3,315
lamia:

there's a lot of YAGNI feature in the C# language which makes me hate it!!! 

I also dislike for some languages - especially those that has lots of consonants and very few vowels. But I don't hate them. I think I just need to know them better to understand them. :D

Seriously, though. What about the C# language do you hate? Java and C# look very much the same to me.

--
Fear is the path to the dark side. Fear leads to anger. Anger leads to hate. Hate leads to suffering. - Yoda

[jop]

  • | Post Points: 20
Top 10 Contributor
Posts 2,038
Points 42,030

jop:
keithrull:

hahah! bilis ni master jop ha :P

Walang ginagawa dito sa trabaho eh... Wallpaper ko tong devpinoy.
keithrull:

Additional question: So when should i use const over readonly?

That's a tricky question. I don't like const. I find that I use readonly most of the time (if I ever find a need for it). :D Const is very limited. You can only assign literal (1.23, 42, "abc") values to it because the values are being resolved by the compiler. You can not assign regular objects to it. Readonly can be used as a const. It is also more descriptive of what it does. Const should be taken out to remove a little language bloat. :D

Nice. Very well explained ;) I think there are things that should be removed on any language.. but the again some features make others unique than others even if its a bloated function called Mid. ;) just my two cents.

devpinoy sig

  • | Post Points: 5
Top 10 Contributor
Posts 882
Points 13,550
jop:
lamia:

there's a lot of YAGNI feature in the C# language which makes me hate it!!! 

I also dislike for some languages - especially those that has lots of consonants and very few vowels. But I don't hate them. I think I just need to know them better to understand them. :D

Seriously, though. What about the C# language do you hate? Java and C# look very much the same to me.

--
Fear is the path to the dark side. Fear leads to anger. Anger leads to hate. Hate leads to suffering. - Yoda

 

My apologies(I hope you understand) It's just a prejudice over Microsoft for trying to be everything(very immature thinking, I know)!

  • | Post Points: 20
Top 25 Contributor
Posts 193
Points 3,315
lamia:

My apologies(I hope you understand) It's just a prejudice over Microsoft for trying to be everything(very immature thinking, I know)!

No problem. I asked what you don't like about C# not because I'm trying (or will try) to defend it. I'm honestly curious and wanted to know why?

Before I learned C#, I was a Java programmer (I would like to think that I still am). When I was teaching myself C#, I had some features that I disliked. Unfortunately, I had not listed them somewhere. After a few years, I guess I got used to the language and forgot what I didn't like about it. So I was wondering if you'll be able to refresh my memory. :D

Anyways, we should probably create another thread for that discussion...

[jop]

  • | Post Points: 20
Top 10 Contributor
Posts 882
Points 13,550

Ok then, my dislikes...
 

 1. It's another Java, a C++ wannabe!!!
 2. It's seems very trivial for me to include pointers in a so-called managed language.
 3. Namespaces starts with a capital letter
 4. Partial classes, I feel it's very unecessary
 5. Partial methods, just heard of the myth(I'm not even sure if there is) but why would you wanna create one?
 6. delegates, I used to like them but now after having observed that guys in our dev team don't use them... I got turned off...
 7. The out method parameter modifier, I'm sure I'll be able to live w/out it.
 8. They keep adding features that would make it look like a beautiful language. But in fact, they're just polluting it with more and more features that people/devs ain't gonna need.

I just finished through 10 topics and there's 9 more in the tutorial that I'm reading. So there could be more. :) 

Sorry, I'm too lazy to create another thread. :) 

  • | Post Points: 20
Top 25 Contributor
Posts 193
Points 3,315

1. It's another Java, a C++ wannabe!!! - C++ wasn't the final word on programming languages back then. Java and C# are both improvements. They are both C++ wannabes - C# more so. I don't know why Sun and Microsoft are so fixated on C++. It is a good language but there are certainly better ones around.

2. It's seems very trivial for me to include pointers in a so-called managed language. - That's one other feature that fattens the language. It is meant to attract C programmers. It think it is quite useful when you are interfacing with C, but I haven't had the chance to use it even once - not even on toy projects.

3. Namespaces starts with a capital letter - Very hard to argue there. Coding conventions are very religous topics :D

4. Partial classes, I feel it's very unecessary - I also feel that way too, but I guess they've painted themselves in a corner there. The Windows Forms model tried (very badly) to copy the Java Swing model where forms are constructed via code. Since the UI Designers also use code, what you end up with is a Form class that contains source that you shouldn't touch (JBuilder and NetBeans also used to do this). They've introduced partial classes to separate the autogenerated code with your code. In the Windows Forms world - partial classes are a welcome feature. But if you take a step back, copying Swing is a bad idea in the first place. The Win32 model and the WPF model (form design is just a resource) to me is a better model. Coding screens are a step back, I believe.

5. Partial methods, just heard of the myth(I'm not even sure if there is) but why would you wanna create one? - Partial methods are not even documented in the specs, so I'm not sure if it will even survive until release. I too don't find a use for partial methods. It is like a solution looking for a problem to solve.

6. delegates, I used to like them but now after having observed that guys in our dev team don't use them... I got turned off... - Delegates in C# are just like Java's anonymous inner classes. Everytime you wire up an event, you are using delegates. Your dev team uses them - unless they've created their own UI framework.

7. The out method parameter modifier, I'm sure I'll be able to live w/out it. - Ref parameters too. Have used them only on toy projects. I guess this is one of those "C compatability features". Makes it easy for C and C++ programmers to transfer to C#. Another one to take out.

8. They keep adding features that would make it look like a beautiful language. But in fact, they're just polluting it with more and more features that people/devs ain't gonna need. - Java and C# are not the final word of programming languages. I do agree that a lot of the new C# features are of the YAGNI type, (partial classes/methods), but some are very useful - like extension methods and lambdas. I guess we're back to my answer on (1) - C++ is a good language but there are better ones around.


Here's a trivia: according to Stroustrup, the sharp symbol in C# are two post increment operators (++) on top of one another. :D

--
Perfection is attained not when there is nothing left to add but when there is nothing left to take away - Antoine de Saint-Exupery

[jop]

  • | Post Points: 20
Top 10 Contributor
Posts 882
Points 13,550

Hehehe you're so patient, a true mark of a great leader. :) Ok, I won't argue anymore. Yep, netbeans has a crappy habit of generating bloated code that you can't edit. I hate it, which is why I like Eclipse in the first place. However, I've seen people who use Netbeans to code Swing apps and they've been very very very productive because of the re-usability it brings/provides. I will withdraw from this  C#( C== )  vs Java thing now before Keith gets angry with me for pirating this topic. :) Thanks a lot Jop! :)

  • | Post Points: 20
Top 10 Contributor
Posts 2,038
Points 42,030

lamia:

Hehehe you're so patient, a true mark of a great leader. :) Ok, I won't argue anymore. Yep, netbeans has a crappy habit of generating bloated code that you can't edit. I hate it, which is why I like Eclipse in the first place. However, I've seen people who use Netbeans to code Swing apps and they've been very very very productive because of the re-usability it brings/provides. I will withdraw from this  C#( C== )  vs Java thing now before Keith gets angry with me for pirating this topic. :) Thanks a lot Jop! :)

Hahah, lamia! why would i be angry? :P it's all good ;)

I love Java and .NET. They have been my passion for years and i still lead people to use Java or .NET in different scenarios and solutions. I know there's alot of language blot on both language but then again its those dirt that makes the language unique, pretty and habbit forming at the same time ;)

devpinoy sig

  • | Post Points: 20
Top 10 Contributor
Posts 1,036
Points 23,965
para patas, malaki pa ang mundo sa labas ng Java at .NET...learn stuff that the *master* here is doing like Ruby, Smalltalk and Lisp...he he...baka maliwanagan tayong lahat!
http://devpinoy.org/blogs/cruizer
Naglalayong buksan at palayain ang kamalayan ng Pinoy .NET developer
  • | Post Points: 20
Page 1 of 2 (18 items) 1 2 Next > | RSS
Copyright DevPinoy 2005-2008