Programming in C#
Results 1 to 9 of 9

Thread: Programming in C#

  1. #1
    Senior Member
    Join Date
    Feb 2002
    Posts
    170

    Programming in C#

    Programming in C#
    The only tutorial you need.

    As the subtitle suggests it is my intention to write a rather large tutorial on C#. If you feel anything is missing or there is anything special you want added, don't hesitate to PM me and I'll add it. Planned TOC can be found below:

    Planned TOC so far:
    About the author.
    Getting started
    About the Microsoft.NET initiative
    What exactly is .NET
    C# and .NET - How do they relate
    Your first C#-program
    Variables. Value- and Reference types, .NET-aliases
    Reading text from the Console.
    Conditionals and Loops

    Actual TOC so far:
    About the author
    Getting started
    About the Microsoft.NET initiative
    What exactly is .NET

    Edit history:
    7/18 - Created
    7/18 - Added "C# and .NET - How do they relate"
    7/18 - Added "Getting started"
    7/18 - Added "Your first C#-program"
    8/18 - Added "Variables. Value- and Reference types, .NET-aliases"


    About the author
    I've been programming C# for a litte more than two years. That's longer than the .NET technology has been publicly available. I run the site cshrp.net which is one of the largest C# portals on the Internet. This tutorial first came to mind quite some time ago, when I first noticed the tutorials forum. Since then, I've had a hard time finding time to write it but it seems I've actually found some time now.
    "Why would I want to learn C#", or "Why a C# tutorial on a security site" one might ask. These are quite valid question and the best reason I can think of is that Microsoft is the biggest actor in this business. The .NET initiative is huge and very well thought. C# is the most competent language amongst the .NET-enabled languages. Thus it seems to be a good idea to learn some C#. It may actually be so that with .NET, Microsoft starts taking security seriously...


    Getting started
    To do any .NET development at all you need the .NET SDK or Visual Studio.NET. The latter costs lots of money so we'll just settle with the .NET SDK which can be found here. Development can be done in Notepad, Emacs or whatever you use. I do however recommend SharpDevelop wich is free and really cool. It is completly written in C# so be sure to install the SDK first or you wont get it to work. There's also Antechinus C# Programming Editor but it costs like $35 but it's well worth it.
    The absolute best development platform is Visual Studio.NET so if you happen to have an MSDN subscription you might want to use it instead. Be sure to only install what you need though or it will eat disc space like crazy.
    Oh well. Once you're happy with your choice of editor and you've installed the SDK you can start fiddle with .NET and C#. csc.exe is your C# compiler and ildasm.exe is your CIL dissasembler. I'll make heavy use of them both throughout this tutorial.


    About the Microsoft.NET initiative
    The .NET initiative was first presented at the Microsoft Professional Developers Conference in Orlando in June 2000. Bill Gates held a speach and showed some nifty stuff. Since then the .NET-framework have gone through several versions. First the PDC one, wich we weren't all that many to get our hand on. About three months later came Beta1 which was publicly available. Beta2 came sometime around last fall and the RTM (Release To Manufacturing) came in february this year. So the actual framework in its complete form has only been around for 5 months at the time of writing. One very important thing to note here is that Microsoft released beta software more than a year before they released the RTM. This is a very important fact because it shows how badly Microsoft actually wanted to incorporate the wishes of the developers. When you look at .NET from a business strategy standpoint it is clear that this is the biggest thing Microsoft has done since Win32 and with the amount of money they're putting in .NET they're sure to succeed. It seems Microsoft have learned from some of their mistakes made in the past and actually payed attention this time. Some of the bigger changes are the open source like nature of .NET, another is the ECMA-standardisation of parts of the framework and the C# language. The actual standardisation documents can be found here.
    The ECMA-standardisation also brings with it the possibility for other OS's to develop ports of the .NET framework. THere are currently a port for freeBSD developed by Corell and Microsoft. There's also the mono-project wich releases tar balls of a .NET environment for linux pretty much daily. They even have .deb packages for you apt-getters out there.


    What exactly is .NET
    .NET is all about loosly coupled, internet based applications that communicate via standard protocols. It sounds like buzzwords but is suprisingly the best way to actually describe .NET.
    .NET is a framework for programming and compiling .NET-enabled application. This is done via the CTS - the Common Type System, the CLI - the Common Language Infrastructure, wich all .NET-languages needs to adhere to.
    Basically a .NET enabled language must be object oriented, strongly typed, incorporate exception handling and support the data types in the CTS, The memory handling in .NET is done via a Garbage Collector so that must be supported too. (I can hear the sighs now, but the GC works suprisingly well).
    By adhering to these strict but few rules a class can be written in on language, instansiated in another and throw exceptions to a third. The whole application can be debugged seemlessly even though it is written with three different languages. This is the absolute primary strengt of .NET, the actual cost for competence is minimal because much of what the programmer knows can be applied in .NET if the language is .NET enabled. Microsoft releases four languages with it's runtime, JScript.NET (wich is now fully object oriented, has datatypes and behaves really cool), VB.NET (wich doesn't look much like VB anymore), C# (wich is new with, and espesially developed for the .NET framework) and C++ with managed extensions. (Basically what they've done here is taken all the .NET rules and incorporated them in some header files and stuff and hooked C++ up with a Garbage Collector.) There are twenty something third party languages as well, those I can remember are:
    Haskell, COBOL, Oberon, ML, Eiffel, Python, Perl, Mercury and Pascal.


    C# and .NET - How do they relate
    As I have mentioned earlier, C# was created for .NET. C# is a very competent language and it's fun to use (those of you who program perl will understand what I mean with the term 'fun'). C# can't however, exist or work without the .NET as it is executed within the .NET runtime, makes use of the .NET base classes and has its memory collected by the .NET garbage collector.
    This is also the reason for the texts above. One has to know a few things about .NET in general before he can start fiddle with C#.
    I also said that C# is the most competent .NET-language. I belive this to be more than a mere personal reflection as the language was designed with .NET in mind. It has no shortcomings due to backwards compatibility or such. There are also quite a few things in different situations that gets done implicitly for you in other languages. These have to be done explicitly in C# so the actual understanding for the .NET framework is greater with C#. Like many other .NET languages, C# have some nifty features wich can be used only in private members, ie they must never be exposed in the actual interface of the class or the cross compatibility between .NET languages will fail. If one knows however that the class will be used together with no other languages than C# he can make use of these features wherever he pleases. One of theses features is C++ like pointers for example. The same result can be achieved with the "ref" keyword but pointers are so much cooler to use, besides it will make C++ programmers feel right at home.


    Your first C#-program
    At this point you should have some basic understanding of what .NET is all about. You have installed the .NET SDK and you're happy with the editor you've chosen for yourself. It is time to write some code. C# in syntax, is very similar to Java. Those of you with a background in C++ or Java will immediatly recognize many keywords and access modifiers. C# is case sensitive and all members must be encapsulated in a class. Major difference from C++. Also, C# doesn't support header files because of the just stated fact. Since everything need to be put in a class, a struct or an enum there is no need for header file. Anyway, below is the mandatory "Hello world" in C#

    //hello.cs

    using System;

    class Hello
    {
    public static void Main()
    {
    Console.WriteLine("Hello World");
    }
    }
    Not very complicated but I belive it's alway better to start from the absolute basics. First line is a comment. C# support C++ style comments. ie "//" and "/**/", the latter for comments that span multiple lines. On the third line we find our "using" directive. This is not a mandatory part of the program but if we decide to use it, it goes at the top of the page. What we're saying here is just that we want to use classes in the System namespace. Console, with its WriteLine method is a class under the System namespace. The using directive in C# is the same thing as "using namespace" in C++. It just points to a logical hierarchy, a shortcut if you will. No actual including of anything takes place.
    The "Main()" method marks the entrypoint. This is where execution of out program starts. Main has to be declared "public" and "static" or compilation will fail. This makes perfect sence because "public" declare what can access said member and everyone need access to Main. "static" declares the member callable even though the class encapsulating it haven't been instansiated. Since Main marks the entrypoint nothing is instansiated yet. "void" is of course the return type of the method. It's preferable to declare members that return nothing as void although declaring them as int and returning 0 works too. It is, altough, mostly done in windows programming. At the console there is no need for it.
    Console is the class representnig standard input/output and WriteLine writes a string to said class, appending a linebreak at the end of the string. Another method is "Write" which skips the line break at the end. Thus Writeline("Hello") and Write("Hello\n") are equvivalent.
    Both Write and WriteLine support a nifty little way to include up to three variables in a string. It is done like so:
    Console.WriteLine("Hello {0}, you are {1} years old and have size {10} in shoes",
    name, age.ToString(), shoeno.ToString());
    Where name, age, and shoeno are arbitrary variables variables. ToString() converts a value type to a string, more on this in "Variables. Value- and Reference types, .NET-aliases"


    Variables. Value- and Reference types, .NET-aliases
    There are really two types of data in C# value types, which are allocated on the stack and popped when they go out of scope and reference types which store a reference on the stack to the actual data that is stored on the heap. The garbage collector traverses the stack from time to time to check if there is something on the heap that hasn't a corresponding reference on the stack. If so, the data on the heap is garbage collected.
    One of the most basic things in every programming language are variables. These can be thought of as an alias for a specific piece of data and like everything else in C# they come in two different flavours - value and reference. On top of this, most of them are .NET aliases as well. Remeber that the Common Type System specifies wich variable types are available to a .NET program? Well, C# has its own name for them, but the C# variable types are really only aliases for the actual .NET variable type.

    These are the value types of C#, with their .NET alias and a description:

    sbyte - System.SByte - 8-bit signed integer
    short - System.Int16 - 16-bit signed integer
    int - System.Int32 - 32-bit integer
    long - System.Int64 - 64-bit integer
    byte - System.Byte - 8-bit unsigned integer
    ushort - System.Int16 - 16-bit unsigned integer
    uint - System.Int32 - 32-bit uninteger
    ulong - System.Int64 - 64-bit uninteger
    float - System.Single - 32-bit single precision floating point
    double - System.Double - 64-bit double precision floating point
    decimal - System.Decimal - 128-bit high precission decimal notation
    bool - System.Boolean - Logical true/false, not compatible with 1/0
    char - System.Char - One single 16-bit character (Unicode)

    The reference types are only two:

    object - System.Object - The actual root type. All other types derive from it
    string - System.String - Unicode string

    Whether you write the .NET notation or the C# notation when declaring variables in C# is really a matter of taste though I suggest you use the latter for consistency.

    string myString = "Hello"; and System.String = "Hello"; are interchangeable.
    Mankan

    \"The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise.\"
    - Edsger Dijkstra

  2. #2
    Banned
    Join Date
    Jun 2002
    Posts
    458
    Why did you post this? Ever heard of negative's "what tutorial are you working on" sticky in the tutorials forum??

    EDIT:
    Ehrm... oops. Hehe. I sort of missed this whole thing and posted a tutorial without posting here first. Terrible sorry about that. Now what do I do?


    SO this was a tutorial?? Wel you don't have to post in negative's sticky, but I thought your c# was going to be the tutorial, and only that. Ah well, and one thing, FREE STUFF RULZ! Are c# development tools free? What OSs do they run on? Just wondering, because MS tends to charge lots o' money for their products, and that is one of the main reasons open source kicks so much ass, it is free, and so has a lot of users, and therefore a lot of support and help is available.

  3. #3
    Senior Member
    Join Date
    Feb 2002
    Posts
    170
    Originally posted here by khakisrule

    EDIT: SO this was a tutorial?? Wel you don't have to post in negative's sticky, but I thought your c# was going to be the tutorial, and only that. Ah well, and one thing, FREE STUFF RULZ! Are c# development tools free? What OSs do they run on? Just wondering, because MS tends to charge lots o' money for their products, and that is one of the main reasons open source kicks so much ass, it is free, and so has a lot of users, and therefore a lot of support and help is available.
    I'm sorry, I thought it was obvious this was a tutorial but maybe it isn't?
    To answer your questions, I've added a section in the tutorial above called "Getting started" where I list some editors and stuff. They all run under win2k and XP. The SDK run on pretty much any win version. There are nodevelopment tools that I know of for any other platforms.

    Cheers,
    Mankan

    \"The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise.\"
    - Edsger Dijkstra

  4. #4
    khakisrule, just because the tutorial is based on a proprietary development enviroment does not mean it's not useful. Some people have a license to run it and would like some help on it too. Open source is great but not the only thing...

    Anyway, the tutorial is really nice. Are you planning to post advanced topics too?

  5. #5
    Senior Member
    Join Date
    Nov 2001
    Posts
    4,786
    yes its a tutorial and from the way its shapping up its going to be a dam good one.
    you got my attention. i never really knew that much about .net or c#, got to many other things to worry about, but after reading this, tomorrow morning first thing SDK and sharpdevelop get downloaded and im following ever word...tut on dude!
    Bukhari:V3B48N826 “The Prophet said, ‘Isn’t the witness of a woman equal to half of that of a man?’ The women said, ‘Yes.’ He said, ‘This is because of the deficiency of a woman’s mind.’”

  6. #6
    Banned
    Join Date
    Jun 2002
    Posts
    458
    I know that numb, I said free stuff rules, not this isn't free. I was wondering and am still curious about that, I asked him because he was the one post the tutorial on the subject. Though I do really like open source, I enjoy the ease of use MS products offer, I asked because I use .net SDK to make active server pages on my sister's laptop, 'cause she has win98, which is required, and that SDK was free, so I was wondering if this was free as well.

  7. #7
    Senior Member
    Join Date
    Feb 2002
    Posts
    170
    Originally posted here by alittlebitnumb
    khakisrule, just because the tutorial is based on a proprietary development enviroment does not mean it's not useful. Some people have a license to run it and would like some help on it too. Open source is great but not the only thing...

    Anyway, the tutorial is really nice. Are you planning to post advanced topics too?
    There will be an "Advanced Topics"-tutorial as well, includeing reflection (.NETs ability to emit code on the fly), garbage collection and destructors and whatever you want written. PM me with your requests.


    Originally posted here by khakisrule
    I know that numb, I said free stuff rules, not this isn't free. I was wondering and am still curious about that, I asked him because he was the one post the tutorial on the subject. Though I do really like open source, I enjoy the ease of use MS products offer, I asked because I use .net SDK to make active server pages on my sister's laptop, 'cause she has win98, which is required, and that SDK was free, so I was wondering if this was free as well.
    Everything needed for C# development is free. If you have the .NET SDK on your sisters laptop you can use that for your C# programming.

    Cheers,
    Mankan

    \"The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise.\"
    - Edsger Dijkstra

  8. #8
    Banned
    Join Date
    Jun 2002
    Posts
    32
    Yes, unforunately for me and other win95 users, we are stuck out in the cold as MS has excluded us .net support. Only win98 and higher are able to install and run the .net framework and the .net SDK

  9. #9
    AntiOnline Senior Member souleman's Avatar
    Join Date
    Oct 2001
    Location
    Flint, MI
    Posts
    2,884
    8/18 - Added "Variables. Value- and Reference types, .NET-aliases"
    Ummm, where did you get the time machine, and can I borrow it

    Looks good. This is a great example of someone actually writing a tutorial, not just putting down everything they know on paper and posting it to the site. Good job...
    \"Ignorance is bliss....
    but only for your enemy\"
    -- souleman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •