IP-WARS.NET - a forward command post of the IP Wars
create account| Front Page|Mission|Standard Operating Procedures|Operating Instructions(aka FAQ's)|Privacy Policy|Site Stats/Info|Admin Actions|Search
Sections:General|IP|SCO v World |Microsoft|grok*/OSRM|IPW Site Meta|Logbooks|Diaries|Legal Documents|View All Articles

Learning C#


Microsoft

By ColonelZen, Section Diary
Posted on Sun Sep 23rd, 2007 at 16:08:56 EST

At the company where I work the decision has been made to move our primary business system to the dot net platform.  Now this was against my recommendation and most others going up the tech chain.  It was purely a business decision and though I dislike it from a technical perspective, I can understand why it was made given the intents and purposes of those making the decision.

In the past when such a decision was made, I was usually told to start looking for another job ... or would already have been handed my hat.  But my current employer claims they want me to stay on.  Maybe this time it will be different (pause, looking out the window to see which direction the asteroid strike will come from ... my inner cynic says "keep looking, it's faaaaaaaar more likely.").  Well says I to my boss, then I'm going to need Visual Studio Pro and some training classes.   To my surprise VS and MSDN were installed on my work box a week later, and they paid for the training classes with no friction at all.

That being the case, I'm first and foremost a grunt programmer/analyst (highly and widely skilled and experienced, if you don't mind my saying so, but in the end -), just another cog in the machine:  I'z goes to work, I'z does whats they sez, and I'z gets my paycheck.  C# is just another language I can pick up without breaking a sweat.  As for a new platform, I've changed my underwear several times throughout my career; once more won't kill me.  And in the end the experience will make me more valuable, whatever happens next in my career.  And lastly and most important at all, I love learning new things and playing with new toys no matter what my misgivings about the materials of which they're constructed.   So off to class I went...

Glorious locale!  I swear that if Rt 10 were ever free of traffic there must be a place where you could stand in the middle of it and look in one direction, then the other and each way see the back of the "Entering Parsippany" signs - those being the only way to distinguish it from the bordering "towns" in the North Jersey sprawl.  Otherwise it was just what you'd expect if you've ever done a professional training class: a typical suburban office park in concrete, glass and asphalt, and a hand full of rooms, some set up as classrooms in a building in said office park.

The C# class was what I expected. I got out of it what I wanted - a week of uninterrupted focus on the language where I could learn where C# differed from java (more later on that) and the background habits, practices and conventions of C# programmers and enough examples and practice Labs that I could begin to absorb all of that in a way that will stick if I use it for a while.  

If there is a negative, it's that my classmates were relatively new to programming or at least high level OO programming, so they were there to learn programming where I was there to learn a language.  This of course forced the instructor to focus more on what I would consider elementary material than the details which were of deeper interest to me.  In a way that may have been a good thing: MS terminology is often more distinctive and specific about particular terms than their general use in discussions about programming. The focus on elementary constructs may have helped set those distinctions in my mind. I'm satisfied - I'm confident that my grounding is now sufficient that I can find the answer to deeper questions from other sources.  Kudos to my instructor, Darsham Mehta, who obviously knew the material and language very well, as well as programming in general, and was an enthusiastic teacher and patient with my sometimes arcane questions.

Should anyone else be interested in C# who already has a grounding in programming, my teacher recommended the book "Professional C# 2005".  I've barely started through it but it's 1500 pages of dense material and looks very much like the perfect rounding out of what I need.

C# and Visual Studio

C# is very, very similar to java.  So much so that when it first came out, there were claims that one could translate between java and C# with a Perl program.   Now having a little deeper look at C# I don't believe that true for any but the simplest cases. Java (at least earlier java) to C# yes, but not the other way.   Remember java was there first and fairly mature; MS's engineers had the advantage of seeing what was missing in it, adding what might be useful, and deciding what may have been mistakes and correcting them.  Of course they copied some of the mistakes too...

There are innumerable comparisons between java and C# already out there on the web (just google "java C# comparison"), so I'm not going to give a complete rundown of them, only those which struck me as most significant.  It should be noted that there are a number of innovations in java 1.5 and 1.6 and many of those comparisons do not account for.   For example, enums and varargs are now supported in java where C# evidently had them from the beginning.  And generics are (relatively) new to both java and C#. I'd add that learning C# has made me look much harder at the new features in java than in the past; in some cases (the enhanced "for", for example) I didn't know they were there - so learning C# is going to make me a better java programmer.

One of the early and I suspect more problematic differences is that C# allows passing of primitive types by reference.   This means that you can change a primitive value in the called method and its value will be changed in the calling method.   Now any experience programmer can see the danger in that - one expects local primitives to stay local. But the danger is somewhat ameliorated by the requisite of using the "ref" (or "out") keyword in invocation.  In other words doing this is required to be quite explicit.  Similarly the "out" keyword indicates a pass by reference but the compiler will not then allow the called program to access data from that parameter, only to write to it's value.   But object references too can be passed by reference, and it is quite possible that the called method can change the instance object referred to by the caller.  There are grave dangers here ... but also interesting possibilities.  Only by time and practice will I decide whether one outweighs the other.

I suspect that the driving force behind the above was the inclusion of a "struct" in C#.  Struct's are simply data structures, or from the other direction, classes with no methods.   But they also differ from classes in that they can be implemented locally, on the stack, by declaring a struct without a "new" keyword.  The pass by reference allows "functions" to manipulate these without otherwise encapsulating them.  This is a performance enhancement, and potentially valuable but (as with many of my complaints) ugly from an object oriented standpoint.

C# uses namespaces rather than packages.  Most of what this means is that the name of the file and directory structure in which the source code resides does not necessarily have any connection to the class(es) it contains.  This has both positive and negative aspects.  Negative in that the enforced structure in java always tells you where the corresponding class can be found while developing before you've created your jar.  Positive because as the programmer/packager you now have more freedom to build and use the directory structures as you wish.   And one also means one can define many classes in a single file.  Likewise C# supports "partial" classes allowing a class to be defined over several files.  Having written some classes with a large number of methods, the ability to group these methods into separate files separately relating to different aspects of the objects' behaviors could be a big plus.  Of course the purist (maybe me, sometimes) would say that probably means the class should be refactored and broken into smaller classes.

But there is a more practical motivation for classes spread across several files as well.  As part of my (self imposed) learning experience I've started writing my own variant of Conway's Life, and thus using Visual Studio.  Now Visual Studio's form designer produces its code in one file for initialization, but the event handlers you do have to write code for go into another file .  You don't want to change the source in the file (or at least that section of it) created and marked by forms designer - it will be overwritten if you change the form!  But abstracted this makes even more sense.  If you have tools that generate code, by allowing the class to be composed of separate files agglomerated later, you can have those portions generated by tools be separate objects to the file system from those which the developer must maintain manually.  I can easily imagine several possibilities where the specifications for something could be stored in an XML file (or database, among other possibilities) which subsequently drives generation of new code.  The developer would then never touch the code generated by the XML processing tool, but could still change as needed those other portions of the class which needed to be manually maintained without any risk of corrupting the generated code (or having the manual code corrupted or removed by the tool).

Let's take a trivial example.  You have a lot of customers and your sales force is way out of control in negotiating billing options but your customer management system only has a single numeric for billing code.  No problem.  You write an XML:

<?xml verson='1.0' encoding='ISO-8859-1' ?>
<billingOptions>
  <option id='1' cycle='monthly' net='30' discount='0' >
  <option id='2' cycle='weekly'  net='30' discount='0' />
  <option id='3' cycle='monthly' net='60' discount='3' />
  ...
</billingOptions>

Now all your code generation tool (which in this case could be a simple xsl piped to a file) does is take this xml and generate a method which contains a case statment which calls a method the same name as the cycle attribute with the net and discount parameters.  Well that's trivial enough ... you don't need tools for that... except that in real life there could be a hundred or more different codes and it could be many different codes mapping to complex behaviors to be mapped to different methods with different meanings and lots of different case statements.  But the XML is self documenting where lots of complex case statements are touchy to read and understand at best.  And then along comes salesman Joe who really can land a large account with group of rich California Wiccans at a very high profit margin except that they insist on being billed on the full moon with a two lunar net.  Still no problem.  You add:

<option id='666' cycle='lunar' net='"2lunar"' discount='0' />

 ... to the xml and write the code for method lunar(string net, int discount){ ...} in the manually maintained portion, run the XML tool, compile and you're done.

I really like the idea of partial classes.  There are workarounds and other ways of doing it that I've used for java.  I've written Perl scripts which sometimes write complete class files then referenced as static or as a singleton and others which look for a / ==BEGIN= to find where to insert code into a source file.   But the partial class is far the more elegant solution.

Another difference between java and C# is that in java you need to declare the exceptions which can be thrown in the method declaration.  C# doesn't.   I recognize and understand the motivation for removing this - I've written many a method with a boilerplate "throws Exception" but on the other hand I've often had the compiler tell me that the class I've written might throw an exception I hadn't anticipated but needed to do something about enough to be wary.  It basically means the programmer needs to give a little more thought to how exceptions can "boil up" than otherwise.

One of the features I really like about C#, borrowed from Visual Basic, is the "property".   These are commonly used as accessors as in getters and setters but aren't required to be; they can invoke more sophisticated processing.  It essentially means that assignment to or from a named element triggers the processing, similar to a tie in Perl.  Of course in java you can always write more complex code in your getXXXX and setXXX methods as well, but I've always been bothered by the java tool sets' dependency upon a convention which isn't part of the language.

Another useful feature is syntactic encapsulation of handling delegates.  We didn't get to this in class - not enough time.  I found out about it studying the code that forms designer created then looking at help and finally the Pro C# 2005 book.  In java you deal with delegates by writing an interface specifying the methods which must be there, implementing those methods in the classes and adding the objects instantiated to a list of delegate objects to do the appropriate processing then calling the prescribed method on those objects when the delegated process is required (most commonly, an asynchronous event).   All very straightforward and logical but somewhat tedious to do in practice (of course the graphics, xml parser and similar libraries already have the interfaces, event registrar and actual delegation methods, and usually some base classes with null handlers already defined, but if you're writing something new you have to do it all yourself).  C# has syntax to handle delegate assignment trivially, and adding or removing listeners by += and -= syntactic expressions.  Additionally there is no requirement to use the same method name for different objects which may wind up being listeners; their parameter lists must match, obviously, but the method names can reflect their local behavior rather than their relation to the delegating behavior.

I said above that C# had copied some of java's mistakes.  Both java and C# purport to be "object oriented".  But this is not strictly true.  In both cases static ("class") variables and methods are "early bound", meaning bound to the scope in which they are referenced rather than the object being referenced.  So if you have class A with static variable x set to "a" and class B which subclasses A but which also has an independent static variable of x, set to "b", then in executable code you have a declaration of an A which in the instance contains a B, the dereference of x will give an "a" rather than a "b" even though the object is supposed to be a B.   I've actually written code that broke because of this before I understood what was going on.  And I keep writing code where the object based behavior rather than the lexically (or more precisely "scope") or "early" bound behavior would be more useful ... I wind up having to write a (-an instance) method and invoking that rather than simply referencing the variable.  This is also true of static methods, but I haven't yet come across a case where that in particular would break expectations.

Now C# goes a little farther in this breakage than java - in java instance methods are always "late bound", i. e. the method invoked will always reflect the object instance.   In C# a method by default will be lexically ("early") bound, that is the actual method invoked will be the one associated with the local reference class rather than the referenced object's class.  BUT C# gives you the option on how to handle it.  You can add the "virtual" keyword to the method declaration in the base class, and "override" on the method in subclasses, to specify that a method invocation should follow the object.  And there are ways to change and limit how the "chain" of subclasses are followed when a method is invoked (the "new" keyword on the method declaration).  What this means to a java programmer is that you almost always want to use virtual and override in defining methods which might be overridden.  Otherwise it means that C# is more flexible but potentially quite vexing in deciding on what and how a level of subclassed method should be invoked from a referenced object.

This understanding all by itself more than justified the cost of the C# class.   Without the labs that drove this home, the lack of understanding this difference from java would have left me puzzled and it might well have been a long time with storied misadventures in coding and debugging before I understood it.

One of the more visible but more trivial differences are style conventions.  It seems that MS has written a large style guide.  Methods should be pascal cased.  Instance variables should begin with an underscore, and many others.  In my younger, more arrogant, days I used to be contemptuous of style recommendations, rather feeling fit to roll my own as I went along.  Now, with too many years and too many languages behind me I find that conformity has a real value.  Not only does it make your code more readable to others, if you follow the language conventions yourself you will better and more quickly read and understand code that others have written following those conventions.   Besides which, the IDE's and tools often force compliance with at least the larger elements of the language conventions and contrariness is an uphill and almost always losing battle.

There are numerous other features in C# that differ from java.  Some are trivial, as "sealed" means essentially the same thing as "final" in a class declaration.  Others are more significant, vis the "using" block to force immediate deallocation of resources in a class which implements IDisposable.   Not only do I not have the time and inclination to delineate them all, but at my present stage I'm probably ignorant of many as well.

It is possible to write C# with a text editor and use the command line "csc" command to compile and build executables and libraries (dll's), but the standard and natural way to write C# is with Visual Studio.   I would generally say that unless you're into masochism in a big way you must use Visual Studio.

At one time Visual Studio was "hot stuff", considered one of, if not the best programming environments available.  It wasn't the first of course - I remember having tools on green screens connected to the mainframe to write CICS screen definitions.  But now being used to Eclipse (3.2 at home) I'm less than impressed with it.  Of course I'm new to VS as well and certainly don't know all the tricks available to use all its features, but it seems considerably less able than Eclipse in a lot of areas.  There are more options in code refactoring in Eclipse.  Your ability to specify code generation options is much greater in eclipse.  You seem in VS to be limited to working on one "project" at a time.  Hey, where are the emacs key bindings?!!

Now Eclipse had humble beginnings and evidently my teacher only ever saw a much older version of it.  He was continually surprised when he would proclaim some feature of VS as wonderful and I would "ho hum" it. He seemed to think that the ability to find the definition of a reference in a project and globally change it's name was significant. Quite frankly VS is now a toy, at least not counting what I don't know of its capabilities (admittedly MS, in many instances, has a perverse way of hiding some very powerful features in ways that make them difficult to find, or even guess that they exist), compared to Eclipse.

But of course, C# and any other language, for any thing complex enough to be interesting and useful it is not so simple as write a file, compile and go.  There are complexities in putting together "assemblies" - collections of compiled objects with mutually bound references - into an executable and or library which will take me a long time to learn.   VS, naturally, does all this without getting in the way.  Ergo, it makes it possible to work on large projects without and before having to learn those complexities.

The most beloved feature (at least for the Visual Basic crowd who are its keenest admirers) of VS is the forms designer.  Here I have to admit that  in the brief time I've been using it is something worthwhile that Eclipse (by itself, anyway) doesn't have.  Now for C# forms designer really only generates C# code that gets compiled into your project just like any other source code file.  You can forego forms designer and write all the form object initialization yourself if you are inclined to tedium.   But from where I sit not only does form designer remove a lot of that drudgery, but as a neophyte to the language and platform the code that it generates is very useful in gaining an understanding of C# and the .NET environment.

Conclusion

So, in the end, do I like C# so far?  Yep.  And I'll be happy to use and play with this toy.  It will be an interesting ride.  Will I use C# in preference to java in my casual play programming?  Nope.  Despite the interesting and in some cases superior features, it is still far too tightly bound to the Microsoft platform.  When I want a tool of general use, I'll use java.  But C# is powerful enough, clean enough, and interesting enough to use without qualm and with considerable pleasure when professionally required.

-- TWZ

< SCO v Novell Looks to Be a Bench Trial (0 comments) | An IPOWER ful experience (6 comments) >
Display: Sort:
Learning C# | 1 comment (1 topical, 0 editorial, 0 hidden)
Re: Learning C# (none / 0) (#1)
by ColonelZen (tzellers lieth within pobox of thy kingdom com) on Sun Sep 23rd, 2007 at 21:00:09 EST
(User Info)
Sigh.  My self editing sucks.   At the lead break there's a missing paragraph:


Evidently all the professional training classes for Microsoft products follow the same script prescribed by MS.  Almost every certified training company has nearly identical offerings.  But none of the local ones had the courses I thought I would need (C# and ASP.NET) less than several months out.  One of our sister companies recommended SetFocus in Parsippany and they had them scheduled soonest, so that's where I went.

Also the italicised text following the xml quoted blocks appears to be an artifact of Scoop.  There's nothing wrong with the escapes I posted for the angle brackets/greater-and-less than signs or the xml constructs that I can see.

-- TWZ

Learning C# | 1 comment (1 topical, 0 editorial, 0 hidden)
Display: Sort:

Links

Firefox 2

Use OpenOffice.org

Add to Technorati Favorites

Join EFF Today

ToTehMoon web site button

~ Merkey v The Internet et al Docs
~ Yahoeuvre
~ tuxrocks.com (SCO cases legal docs)
~ scofacts.org
~ eagle.petrofsky.org
~ Zen's Den
~ Yahoo SCOX Message Board
~ Lamlaw
~ Microsoft Watch
~ Groklaw
~ Korgwal - a Groklaw mirror
~ nosoftwarepatents.com
~ Flame Warriors
~ SCOXE Wars
~ Get your Merkey Number here!
~ Digital Law Online

Recent Comments

Breaking News and External Article Comments
General News – General Articles
by ColonelZen, January 5
60 comments
» SCO Lifeboat List from Stats_for_all – AncientBrit, May 6
» Not a single comment on the Novell... – sphealey, Jul 22
» Re: Not a single comment on the Novell... – AncientBrit, Aug 8

Eagle Loses Appeals
General News – General Articles
by JCausey, December 15
1 comment
» Re: Eagle Loses Appeals – br3n, Jan 7

The Chinese Room Revisited, Thoughts on...
General News – Diary
by ColonelZen, November 24
1 comment
» Re: The Chinese Room Revisited,... – ColonelZen, Nov 24

How to Transition a Windows Shop to Linux
General News – General Articles
by JCausey, November 21
3 comments
» Re: How to Transition a Windows Shop to... – ColonelZen, Nov 22
» Re: How to Transition a Windows Shop to... – JCausey, Nov 23
» Re: How to Transition a Windows Shop to... – ColonelZen, Nov 23

Advocacy
General News – Diary
by br3n, October 29
3 comments
» Re: Advocacy – br3n, Nov 2
» Re: Advocacy – ColonelZen, Nov 2
» Re: Advocacy – br3n, Nov 4

Very Bad News for Darl and Ralph
SCO v The World – Diary
by ColonelZen, October 13
7 comments
» Re: OT advocacy – br3n, Oct 26
» Re: OT advocacy – JCausey, Oct 28
» Re: OT advocacy – br3n, Oct 29

Some SCOX Financial Analysis
SCO v The World – SCO Related Articles
by JCausey, September 21
13 comments
» Re: Some SCOX Financial Analysis – br3n, Oct 3
» Re: Some SCOX Financial Analysis – ColonelZen, Oct 3
» Re: Some SCOX Financial Analysis – br3n, Oct 6

Open Source in Education - Opening Doors
General News – General Articles
by JCausey, September 28
1 comment
» Re: Open Source in Education - Opening... – br3n, Sep 29

An IPOWER ful experience
General News – Diary
by ColonelZen, September 25
6 comments
» IPOWER SysAdmin Doesn't Do Weekends!! – ColonelZen, Sep 29
» Re: An IPOWER ful experience – ColonelZen, Sep 29
» Re: An IPOWER ful experience – ColonelZen, Sep 29

Learning C#
Microsoft – Diary
by ColonelZen, September 23
1 comment
» Re: Learning C# – ColonelZen, Sep 23

Comment search...

Recent Diaries

SCO has a Potential and Credible BILLION Dollar Liability
by ColonelZen - March 15

The Chinese Room Revisited, Thoughts on Consciousness
by ColonelZen - November 24
1 comment


Advocacy
by br3n - October 29
3 comments


An IPOWER ful experience
by ColonelZen - September 25
6 comments


Learning C#
by ColonelZen - September 23
1 comment


Getting ruby DBI for Mysql and Postgresql working on FC 6
by ColonelZen - March 7

Declaration of Linus Torvalds
by nedu - February 13
1 comment


Declaration of M. Douglas McIlroy
by nedu - February 12
6 comments


Declaration of Ulrich Drepper
by nedu - February 11
1 comment


Declaration of K. Y. Srinivasan
by nedu - February 11


More Diaries...

Login

Make a new account

Username:
Password:

Older Stories

Monday May 28th
Why SCO Does Not Own the Unix Copyrights
   (0 comments)

Thursday April 5th
It Can Really Happen - Eagle Broadband Delisting from AMEX
   (5 comments)

Monday March 12th
OpenOffice.org Sends Open Letter to Dell
   (0 comments)

Tuesday March 6th
Preliminary Order in Prohibition
   (2 comments)

Monday January 15th
[Linux-ia64] optimizing __copy_user
   (12 comments)

Older Stories...

Related Links

~ ColonelZen's Diary

SourceForge Logo Powered by Scoop

All trademarks and copyrights on this page are owned by their respective companies or owners.
Comments, articles and logbooks are owned by the Poster. By posting on the ip-wars.net web site, all posters grant a license to ip-wars.net to publish the content and release it pursuant to the Creative Commons License that covers the rest of the site. For more details, please check out the Standard Operating Procedures. Also, please read the Privacy Policy for the site. Finally, DO NOT send e-mail to the site owner (Jeff Causey) unless you have read and agree to the terms regarding e-mail included in the Standard Operating Procedures.
Everything else © 2004, 2005, 2006, 2007 ip-wars.net and Jeffrey G. Causey and is licensed under a
Creative Commons License
This work is licensed under a Creative Commons License.