C # is The New MODERN LANGUAGE FOR Development Applications for Microsoft '
s .NET platform. This document will go through the basics of this language and explain how you can use it to write efficient platform neutral applications. The .NET platform is designed to be language neutral, indeed, all .NET code is run under the Common Language Runtime (CLR). C # isjust one of the languages that can be used to write classes for the CLR, but so far it is the only language that was written from the outset with .NET in mind. This makes C # as the language Of choice for .net development. ---------------------------------- What is C #?
----------------------------------- The Most Simple C # Program is shown here: // in Simple.csclass App {public static void Main () {}} This code does nothing, but will compile fine using the C # compiler csc.exe: csc Simple.csBefore making this application do more let me explain the code that you already seen The code defines. a .NET class which has a single public method. By public I mean that the method can be called from code outside of the class, in general a method marked as private is only accessible from code in the current class. However Main () is a special case, you can declare it as private and the system will still be able to call it, but in practice you should always declare it as public.The method is also marked as static Classes are usually used to create objects -. instances of The Class - and Methods Are Called Through Objects Using Data That Is Specific To An Object. However, a Static Method Can Be Called WITHOUT An Object Instance. .Net Does Not Allow You To Write Glo bal methods, every method has to be part of a class, so static is the only way to call a method without first creating an object.Because static methods are not associated with objects, they can not call non-static methods or use other members of THE CLASS THAT IS NON-static. for example: class app {public static void main () {f (); // this will not compile!} public void f ()}} one Typical REASON for a static method is to To Create An Instance of the Class, this Is Done in C # with the new operator: class app {public static void main () {app app = new app (); app.f (); public void f ()}}} THE THETIC Method f () THROUGH THAT OFSTANCE
app is known as a reference. The syntax used here is different to what you would expect in C . Firstly, the code must explicitly specify the constructor that will be used (in this case the default constructor that is provided by C #). The second difference is that although new is used to create a reference there is no equivalent of the C delete operator. The reason is that .NET has a garbage collector that monitors object usage and when all references to an object have been released (or if there is a circular dependency) the garbage collector can release the object.In C new has the specific meaning of "create a new instance of this class in the C free store". The meaning of new in C # is more wide ranging, it merely says " create a new instance of this class ", usually this class will be created on the heap and managed by the .NET garbage collector, but if used to create an instance of a value type (a term that will be explained later) the object will Be created on the stack. ------- ------------------------------------- Note: if you have a class thing holds resources that shouth NOT be held for a long time (for example an exclusive lock on a file), then you should implement a method on the class to free this resource and explicitly call this method when you are sure that the object will not longer be used. Typically, Such a Method Is Called Dispose (). ----------------------------------------- - the main () Function Has A Special Meaning, It is The Entry Point for An Application, And Every C # Application Must Have A Class with a public static main () Method - And Only One Such Class. AS IN C
, The Main () method may return void or an int and it can take parameters Main () that accepts command line parameters looks like this:. Public static int Main (string [] args) {return 0;} The args parameter is an array of strings. Arrays in .NET are instances of the System.Array class, and items are accessed through square brackets, as in C . The size of an array is determined by accessing the Length property. A property gives access to data in the class.For your application to do anything it must use a class library. A class library contains classes that perform various system actions like creating and manipulating windows, managing security, or accessing databases. C # code uses the .NET library that is common to all . code that runs on the .NET platform This means that the same library is available to VB.NET code and through the Managed Extensions for C A class library is accessed through the using keyword: // in Simple.csusing System; class App. {public static int main (String [] args) {for INT x = 0; x ll create your own namespaces and putting classes in a namespace allows other applications to use your classes. This code uses a class called Console. As the name suggests this gives access to the command line console. This simple example prints all the command line arguments to the console. Namespaces scope class names, using System in System.String means that you indicate that you want to use String from the System namespace as opposed to String defined in another namespace. you use the / reference switch on csc to indicate the namespaces that your code will use The using keyword allows you to use classes without using the fully qualified name Note:.. like C and C, each statement must end in a semicolon However, unlike C the class declaration does not have to have a terminating semicolon. .................................................................................................................................................................................................................................................................................................. Blic static void main () {INT i = 1; double f = 2.0; console.writeline ("integer"); console.writeline (i); console.writeline ("floating point"); console.writeline (f); } Other than literal strings, this code passes an integer and a double precision floating point number to WriteConsole () The output of this new program is:. Integer1Floating point2Notice that there is a newline after each call to WriteLine () there are two ways. To get the string and value on the Same Line, Both Are Shown in this next code: public static void main () {int i = 1; double f = 2.0; console.write ("integer"); console.writeline (i Console.WriteLine ("Floating Point {0}", F);