INSTRUCTOR: DR MARCO VALTORTA CSCE 330 C# GROUP MEMBERS: KONSTANTINOS MALEGOS YASSER AL-MUTAIRY CHRIS HESTER UNIVERSITY OF SOUTH CAROLINA THE BIRTH OF C# • C# parents are C++ and JAVA. So, C# contains many C++ features but also adds the object-oriented features from JAVA. • C# is the a hybrid language developed by Microsoft. In order to understand the basic features of the language, we must compare them with its predecessors. INTERESTING CONCEPTS • To produce the first component oriented language in the C/C++ family. You can construct components that have properties, methods and events. • To create a language which everything is an object. C# bridges the gap between primitive types and classes by allowing any piece of data to be treated as object. MICROSOFT.NET • MICROSOFT.NET is the new programming environment introduced by Microsoft. • It is the new way of building and deploying software that leverages standards such as HTTP and XML to make interoperability a reality. • It is not necessary for C# to use this environment but C# was especially design for this environment. Common Language Runtime • The CLR serves as an execution engine for applications. • Every byte of code that you write for the framework runs in the CLR. • CLR consists of instructions written in CIL(common intermediate language-pseudomachine language). CIL instructions are just-intime and compiled into native machine code at run time. • A given method is compiled only once-the first time is called-and thereafter cached in memory so it can be executed again without delay. COMMON CIL INSTUCTIONS • LDC-Loads a numeric constant onto the stack • LDELEM-Loads an array element onto the stack • NEWARR-Creates a new array • NEWOBJ-Creates a new object • THROW-Throws an exception COMPILERS • Microsoft provides CIL compilers for Java, C++, Visual Basic, Cobol, Python, Eiffel and Jscript.You can even use the assembler ILASM to write code in CIL. • All of these languages use the same API so you can port an application from Visual Basic and C++ to C#. • Because high level code ultimately compiles to CIL, the framework lets you write a class in one language and use it or derive it from another. That is language independence!!! C#API • The new .NET framework class library is a library with 7000 types,classes, structs, interfaces, enumerations. The bad news is that is like learning a new operating system. The good news is that that every languages uses the same API so you can easily switch to VB or C++. SAMPLE NAMESPACES • System: core data types and auxiliary classes. • System.Net: classes that wrap protocols such as HTTP. • System.Web.Services: classes for writing Web Services. • System.Web.UI: core ASP.NET classes • System.Xml: classes for reading and writing XML data. CONTINUE… • To enable construction of robust and durable software. C# includes garbage collection,structured exception handling and type safety. • It allows the use of pointers through the unsafe mode where performance is the key issue. • Supports web programming through ASP and XML. CONTINUE… • C# code can be written in any text editor. The only required task is to download the compiler from the Microsoft’s Website. • It is recommended to use the graphical environment Visual Studio.NET. The advantages are that you can incorporate code from other languages like C++ and C and change it into C# code. EXAMPLES OF USAGE • • • • • • • • using System ; //Import the WinForms Namespace using System.Windows.Forms ; //Class Form1 extends class Form from the //System.Windows.Forms namespace public class Form1 : Form { //Main Method public static void Main() { //Run the Application Application.Run(new Form1()); } } RESULT THE HELLO PROGRAM • class HelloWorld { public static void Main() { System.Console.WriteLine("Hello World") ; } } • Output will be Hello World WRITE TO A FILE • public class Writetextfile { } public static void Main(string[] args) { FileInfo t = new FileInfo(“example.txt"); StreamWriter Tex =t.CreateText(); Tex.WriteLine(“I am writing to the file"); Tex.Write(Tex.NewLine); Tex.close(); } SIMILARITIES AND DIFFERNCES • • • • DECLARING VARIABLES : C#: int x; int x=3; C++: int x; int x=3; Java: int x; int x=3; • • • • ASSIGNMENTS C#: xvalue=7; C++: xvalue=7; Java: xvalue=7; CONTINUE… • • • • • • • FOR LOOPS C#: for(int i =1; i<=10; i++) Console.WriteLine(“ The number is {0}”, i); C++: for(int i =1; i<=10; i++) Printf(“%d\n”,i); JAVA: for(int i =1; i<=10; i++) System.out.println(“The number is” +i); C# VERSUS JAVA • C# and Java are both new generation languages descended from a line including C and C++. Each includes advanced features, like garbage collection, which remove some of the low level maintenance tasks from the programmer. In a lot of areas they are syntactically similar. • C# contains more primitive data types than Java , and also allows more extension to the value types. For example, C# supports 'enumerators', types which are limited to a defined set of constant variables and structs, which are userdefined value types. CONTINUE… • Unlike Java, C# has the useful feature that we can overload various operators. • Like Java, C# gives up on multiple class inheritance in favor of a single inheritance model extended by the multiple inheritance of interfaces. However, polymorphism is handled in a more complicated fashion, with base class methods either 'overriding' or 'hiding' super class methods. According to Microsoft the top 6 Reasons to Switch to Visual C# from Java are: • Familiarity Visual C# provides Java developers with a language that is immediately familiar and comfortable to them, regardless of skill level. • Object-based Type System Visual C# provides developers with a modern and intuitive object-based type system that negates the need for complex and verbose data marshalling code commonly found in Java applications. Continue… • Powerful Component-oriented Development Visual C# provides developers with a powerful component-oriented development language that includes support for properties, indexers, delegates, inheritance, versioning, and attributes, without the need for esoteric or rigid naming patterns and companion classes. • C# allows developers to use Extensible Markup Language (XML) comments to provide useful and customizable source code documentation. CONTINUE… • Target Any Device Visual C# lets developers target powerful desktop computers and a wide variety of handheld and wireless devices today using identical tools and skills. • Leverage the .NET Framework Visual C# provides developers with access to the Microsoft .NET Framework, a robust, thread-safe library of collection classes, networking functionality, data access classes, and more. ANY QUESTIONS?