|
The last one is my coding style.
veni bibi saltavi
|
|
|
|
|
Guess the output:
namespace Quiz
{
public class Base
{
public static string Name { get ; protected set ; }
static Base() { Name = "I am Base" ; }
}
public class Derived : Base
{
static Derived() { Name = "I am Derived" ; }
}
public static class Quiz
{
public static void Main()
{
System.Console.WriteLine ( Base.Name ) ;
System.Console.WriteLine ( Derived.Name ) ;
Base b = new Derived() ;
System.Console.WriteLine ( Derived.Name ) ;
}
}
}
This issue bit me this week when I refactored some code.
Originally I had a base class (with some static members) and some derived classes that set the static values as needed. (This has worked just fine for more than a year.)
This week, while refactoring, I decided that the static and instance parts should be separated (they really should), so I split them. A few days later I realized that the static parts weren't behaving as expected.
The problem was that after splitting the instance parts away, I no longer instantiate the class that contains the static parts and the static constructors in the derived classes never get called!
My solution was to eliminate the use of static and to have a normal class that I instantiate with the appropriate values. This is a much better technique for other reasons as well.
The following two pages were no help:
https://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.120).aspx[^]
http://csharpindepth.com/Articles/General/BeforeFieldInit.aspx[^]
|
|
|
|
|
Interesting and I am glad I guessed it correctly.
As I know them, static members are associated with the type and not the instance. So, although the Derived class has the static property, it's still associated with the Base class. And hence the static ctor of the Derived class doesn't get called until you instantiate it. So, unless you explicitly have that property in the Derived class (since it can't be overridden), the output would be:
I am Base
I am Base
I am Derived
Here's an SO question[^] the accepted answer of which you might find interesting (of course, I am sure you already know this ).
Or it's due to "Calling static methods of a type does not force the system to initialize the type."
-- EDIT
Correct me if I am wrong.
You have just been Sharapova'd.
modified 17-Apr-15 3:02am.
|
|
|
|
|
All your base are belong to us.
-Derived
|
|
|
|
|
It's All about the Base[^]
Anything that is unrelated to elephants is irrelephant Anonymous
- The problem with quotes on the internet is that you can never tell if they're genuine Winston Churchill, 1944
- I'd just like a chance to prove that money can't make me happy. Me, all the time
|
|
|
|
|
|
Ooooh, I like that video!
Anything that is unrelated to elephants is irrelephant Anonymous
- The problem with quotes on the internet is that you can never tell if they're genuine Winston Churchill, 1944
- I'd just like a chance to prove that money can't make me happy. Me, all the time
|
|
|
|
|
this sound like a job for: basehunter!
Life's like a nose, you've got to get out of it whats in it!
|
|
|
|
|
It's Friday! What are you doing this weekend?
I will be going to Pondicherry for the weekend. Been there several times, but this time I plan to eat out (never tried their French style cafes and restaurants), anything (vegetarian) in particular to recommend?
Cheers,
विक्रम
"We have already been through this, I am not going to repeat myself." - fat_boy, in a global warming thread
|
|
|
|
|
I expect to spend Saturday working in the yard and playing with the new milling machine I recently bought. I made some T-nut stock by milling bar stock to shape, drilling and tapping the holes for 6 separate nuts, but I can't cut the individual parts until my band saw gets here next week. Since I'll need clamping bars, threaded studs and locking nuts to go with the T-nuts, I may manufacture a few while I'm waiting for the saw. Sunday I expect to spend barbecuing and repairing the a/c systems on a couple of cars for friends, since I have the tools and freon handy. After that, I think I'll take a nap.
Will Rogers never met me.
|
|
|
|
|
I will be doing a crash course in Angular. I met with a potential client yesterday, and told them I didn't know Angular, but could have a basic working knowledge by Monday. Ouch. So they are going to give me a practical assessment, for completion by, say, Wednesday.
No object is so beautiful that, under certain conditions, it will not look ugly. - Oscar Wilde
|
|
|
|
|
I don't know squat about Angular, but committing to learn any new thing in a few days is gutsy. Best of luck to you!
Will Rogers never met me.
|
|
|
|
|
Balls to the wall Brady that's what they call him... the very best of luck to you on that, I hope you get the contract.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Pondicherry ?
I love it
Im going to spend full time with COC Game . we have a battle this weekend , so i have to upgrade my defence . lol
நெஞ்சு பொறுக்கு திலையே-இந்த
நிலைகெட்ட மனிதரை நினைந்துவிட்டால்
|
|
|
|
|
King Fisher wrote: we have a battle this weekend
Wooz we have one every 48 hours!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Quote: we have one every 48 hours!
In your real life. lol
நெஞ்சு பொறுக்கு திலையே-இந்த
நிலைகெட்ட மனிதரை நினைந்துவிட்டால்
|
|
|
|
|
Got a lot to do, tewday some wood cleaving and reenactment club.
Saturday renovate new building for the club and a session of GTA V.
Finally a long sleepy on sunday and a late breakfast
if(this.signature != "")
{
MessageBox.Show("This is my signature: " + Environment.NewLine + signature);
}
else
{
MessageBox.Show("404-Signature not found");
}
|
|
|
|
|
having a party at a friends... it is however my turn to drive... fml
Life's like a nose, you've got to get out of it whats in it!
|
|
|
|
|
Off to Madurai to night. Planning to be back to Chennai on Sunday.
|
|
|
|
|
Are you from Madurai?
நெஞ்சு பொறுக்கு திலையே-இந்த
நிலைகெட்ட மனிதரை நினைந்துவிட்டால்
|
|
|
|
|
Nope..am actually from Coimbatore. But now in Chennai for nearly 10 yrs.
|
|
|
|
|
As usual: Playing guitar/piano, reading(just got new books yesterday) and going somewhere with my girlfriend, maybe watch a few movies.
|
|
|
|
|
Friday! Friday!
Gotta get drunk on Friday!
veni bibi saltavi
|
|
|
|
|
Nagy Vilmos wrote: Gotta get drunk on Friday! Indeed! On Fridays, one must do something completely different than everyday's rut.
You have just been Sharapova'd.
|
|
|
|
|
Well that was obvious...
if(this.signature != "")
{
MessageBox.Show("This is my signature: " + Environment.NewLine + signature);
}
else
{
MessageBox.Show("404-Signature not found");
}
|
|
|
|