CIS 24: Project 2. Introduction to C# Authors: Steven Hamilton Adam Brown. CIS 24 Mondays 6:30 9:10 Dr. Kopec



Similar documents
Objectif. Participant. Prérequis. Remarque. Programme. C# 3.0 Programming in the.net Framework. 1. Introduction to the.

Fundamentals of Java Programming

Course MS10975A Introduction to Programming. Length: 5 Days

Evolution of the Major Programming Languages

INTRODUCTION TO C# 0 C# is a multi-paradigm programming language which is based on objectoriented and component-oriented programming disciplines.

Java Interview Questions and Answers

Praktikum im Bereich Praktische Informatik Entwicklung eines Ray-Tracing Systems. computer graphics & visualization

C# and Other Languages

.NET Overview. David Smith. Today s s Topics. Why am I here? A tool. Microsoft s s Vision for.net

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

1. Overview of the Java Language

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Konzepte objektorientierter Programmierung

Android Application Development Course Program

What Perl Programmers Should Know About Java

Dynamic Web Programming BUILDING WEB APPLICATIONS USING ASP.NET, AJAX AND JAVASCRIPT

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Characteristics of Java (Optional) Y. Daniel Liang Supplement for Introduction to Java Programming

Course Title: Software Development

MA-WA1920: Enterprise iphone and ipad Programming

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Cross-platform IL code manipulation library for runtime instrumentation of.net applications

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Java technology trends offer renewed promise for portable embedded applications

An Easier Way for Cross-Platform Data Acquisition Application Development

On the (un)suitability of Java to be the first programming language

Impact of Source Code Availability on the Economics of Using Third Party Components A White Paper

An Overview of Java. overview-1

An Introduction to.net for the J2EE Programmer

C#5.0 IN A NUTSHELL. Joseph O'REILLY. Albahari and Ben Albahari. Fifth Edition. Tokyo. Sebastopol. Beijing. Cambridge. Koln.

The C Programming Language course syllabus associate level

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Programming Language Inter-conversion

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

ASP &.NET. Microsoft's Solution for Dynamic Web Development. Mohammad Ali Choudhry Milad Armeen Husain Zeerapurwala Campbell Ma Seul Kee Yoon

Semantic Analysis: Types and Type Checking

Glossary of Object Oriented Terms

Description of Class Mutation Mutation Operators for Java

Crash Course in Java

CrossPlatform ASP.NET with Mono. Daniel López Ridruejo

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Java SE 8 Programming

Java EE Web Development Course Program

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Object Oriented Software Design II

Programming with the Microsoft.NET Framework Using Microsoft Visual Studio 2005 (VB)

Chapter 5 Names, Bindings, Type Checking, and Scopes

#820 Computer Programming 1A

Input/Output Subsystem in Singularity Operating System

Java Application Developer Certificate Program Competencies

Lecture 1 Introduction to Android

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

Take Your Team Mobile with Xamarin

Managing Variability in Software Architectures 1 Felix Bachmann*

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Chapter 1 Fundamentals of Java Programming

1 The Java Virtual Machine

C++ INTERVIEW QUESTIONS

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Java (12 Weeks) Introduction to Java Programming Language

INTRODUCTION TO JAVA PROGRAMMING LANGUAGE

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Java SE 7 Programming

.NET Overview. Andreas Schabus Academic Relations Microsoft Österreich GmbH

.NET and J2EE Intro to Software Engineering

COMPARISON OF OBJECT-ORIENTED AND PROCEDURE-BASED COMPUTER LANGUAGES: CASE STUDY OF C++ PROGRAMMING

C Compiler Targeting the Java Virtual Machine

Syllabus for CS 134 Java Programming

Programming Language Concepts for Software Developers

TypeScript for C# developers. Making JavaScript manageable

CSC230 Getting Starting in C. Tyler Bletsch

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Programming. Languages & Frameworks. Hans- Pe(er Halvorsen, M.Sc. h(p://home.hit.no/~hansha/?page=sodware_development

Web Development in Java

Programming and Software Development CTAG Alignments

Chapter 6: Programming Languages

Programming in C# with Microsoft Visual Studio 2010

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Semantic Object Language Whitepaper Jason Wells Semantic Research Inc.

Dart a modern web language

1/20/2016 INTRODUCTION

Java SE 7 Programming

UML for C# Modeling Basics

CSC 551: Web Programming. Spring 2004

Developing Embedded Software in Java Part 1: Technology and Architecture

Checking Access to Protected Members in the Java Virtual Machine

Specialized Programme on Web Application Development using Open Source Tools

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

The Rules 1. One level of indentation per method 2. Don t use the ELSE keyword 3. Wrap all primitives and Strings

WHITE PAPER. TimeScape.NET. Increasing development productivity with TimeScape, Microsoft.NET and web services TIMESCAPE ENTERPRISE SOLUTIONS

SQL and Programming Languages. SQL in Programming Languages. Applications. Approaches

CSC Software II: Principles of Programming Languages

To Java SE 8, and Beyond (Plan B)

UNIX, C, C++ History, Philosophy, Patterns & Influences on modern Software Development. Alexander Schatten November

Java SE 7 Programming

Computing Concepts with Java Essentials

Transcription:

CIS 24: Project 2 Introduction to C# Authors: Steven Hamilton Adam Brown CIS 24 Mondays 6:30 9:10 Dr. Kopec

Introduction: C# (pronounced Cee sharp), coauthored by Anders Hejsberg (designer of the Turbo Pascal Compiler, and Delphi, another object-oriented language that allows graphical user interfaces to be constructed in much the same way as Visual Basic) and Scott Wiltamuth, is the latest and greatest object-oriented programming language from Bill Gates Microsoft. It is a strongly-typed language designed to give the optimum blend of simplicity, expressiveness, and performance. 1 It draws heavily from C++ and Java, which can be seen in the syntax (or code), although it is said to keep other languages in hindsight. 1 Runtime: One of the major features of the C# language is how closely associated it is with the.net framework. The.NET platform has, at its center, the Common Language Runtime (CLR) and a set of libraries which can be exploited by a wide variety of languages which are able to work together by all compiling to an intermediate language (IL). 1 Here is one of the main items that have given people the impression of C# s similarity to Java. The CLR is a lot like the Java Virtual Machine, a software "execution engine" that safely and compatibly executes the byte codes in Java class files on a microprocessor 2, which is what makes the Java language work. The.NET platform s main purpose is to provide connectivity between languages. Since languages within the.net framework all compile to the IL, they can interact with each other rather well. C# is a little symbiotic: some features of C# are there to work

well with.net, and some features of.net are there to work well with C# (though.net aims to work well with many languages) 1. Type System: The C# type system is based on two base types: Value Types, and Reference Types. Value types are like primitive types in Java and C++ (i.e.-int, boolean, char). Reference types are like Objects in C++ Java Type conversions are handled in the expected ways. Implicit conversions are automatically done when widening occurs and explicit conversions can be forced through casting. Casting and implicit conversions follow the same conventions and rules as C++ and Java. Code Documentation Facilities The only thing more important than the source code, is the documentation of the source code. It is even argued by some, that the documentation is equally, if not more, important than the source. C# provides a very clearly structured mechanism for documenting code using XML. These specially formed comments may be parsed out later to produce robust, complete documentation. Ex: standard comment // this is a standard comment Ex: XML comment /// <summary> xml comment </summary> Technically an XML solution is far superior to any other documentation facility every implemented to date. The real question is whether developers will actually use the

verbose XML format for code documentation or fall back on easier, less verbose mechanisms. Classes and Objects Class declarations are syntactically and semantically equivalent to C++ and Java. Ex: class MyClass{... code... The declaration scoping modifiers are similar to C++ and Java Fields and Properties: C# has implemented properties for fields, a concept that VB developers will be familiar with, but which will seem awkward to C, C++, and Java developers. Properties allow limiting variable access. In Java, and C++, in order to protect a member variable, or field, it must be declared private or protected, and then accessor methods must be written to allow getting and setting of the value. C# allows properties to be set, which mechanically is similar to VB. For example, the following code demonstrates: Ex: private int i = 0; public int i { get{ return i; set{ this.i = value;

*note: anyone who is devoted to pure OO should find this feature of C# to be rediculous and unnecessary. It in fact lends itself to break OO methodology by giving the appearance that a member field is publically accessible, when actually it may not be based on the properties set. OO Classes may be declared abstract, and may inherit from a super class. Multiple inheritance in C# is not allowed. C# follows all standard OO conventions for overloading, over-riding, inheritance and scoping. Nested classes are supported, that is the definition of one class within another, but Anonymous nested classes are not supported. C++ has function pointers, and Java has full reflection of classes. C# has provided its own similar functionality through Delegates. Delegates provide a way for C# code to establish a relationship between a variable, and one or more concrete functions. Both C++ and Java could also precisely duplicate the functionality of C# delegates, but C# has simply made it a formalized part of the language. The measure of any true OO implementation is the presence or lack of reflection. Reflection is the ability for the application to inspect its own object environment. Java and C++ provide reflection, and so does C# through System.Reflection namespace. Namespaces

Namespace declaration in C# is exactly the same as C++, and similar to Java, although all are functionally equivalent. There is a slight difference in syntax, but other than that, the meaning is the same. Ex: c++ AND c# namespace declaration namespace root.sub.mynamespace{... your declarations... Ex: java namespace declaration package root.sub.mynamespace; To use a specific namespace, you use the using keyword followed by the namespace you wish to reference. This is identical to C++, and functionally equivalent to the Java import statement. Exception Handling C# has exception handling virtually identical to Java and C++. Here is a small example to demonstrate: try { a = 10 / b; catch ( Exception e ) { Console.WriteLine ( e ) ; IO IO is made available through standard library objects. Anyone familiar with Java or C++ will immediately recognize how to handle standard IO. C# also supports the notions of streams, also through standard interfaces and existing library objects.

Ex: Console.WriteLine("Hello World"); Concurrent Programming Threading is supported intrinsically through a thread library. The model is similar to Java s in that a thread of execution is created where in C and C++ a new process is spawned. The implementation of the thread is not similar to Java and more like that of C++. A thread is created, and a function delegate (analogous to a pointer) is passed to the thread to be called back by the concrete thread of execution. Garbage Collection: Garbage collection is necessary to help provide memory protection. With C, C++ and other low level languages that do not have a mandatory garbage collection mechanism, the developer may (purposely or not) abuse memory utilization. More often than not, memory utilization, and improper freeing of unused memory, is a common source of bugs made by experienced and novice programmers alike. While operating systems can try to minimize the damaging effects of improper memory utilization, there is no way (at least no reasonably efficient way) for an OS to examine all memory usage and correctly and efficiently determine when to release resources. A Garbage collection scheme uses a set of rules to determine when to automatically release system resources, and when properly integrated into the program,

can be very effective at reclaiming unused resources. In C# the garbage collector is part of the runtime that runs all code. Common Misconceptions and Misunderstandings C# was primarily based on C, C++, and Java, with some influence from VB. Because of this, some things may not work as expected, or simply be missing from C# that the developer would expect or take for granted. C# no longer allows header files, global functions or constants, and pointers. The arrow operator (->) which was used in C++ to access methods in certain situations has been eliminated and replaced with the period. Also, another nice feature is the inherent get and set features within the language, as opposed to other object-oriented languages where it was necessary to write a getter and setter for each method. The finalizer in C# ~ can be very confusing to C++ developers. It looks like the deconstructor in C++, but in fact operates differently in C#. In C++ the tilde is used to define a function that is explicitly called by the developer to release resources. In C# the tilde defines a finalizer function, and can only be called by the runtime when the class is about to be cleaned up by the garbage collector. C# still allows definition of structs. This can be confusing down the road to those reading code that are not aware the oject they are dealing with was declared as a struct. Structs and object behave slightly differently in certain situations, which will not lead to runtime errors, but may lead to unexpected and erroneous results. Review: Benefits and Disadvantages

With some scrutiny, it is hard to argue the fact that C# s CLR is a superior conceptual design to the JVM. The JVM places developers in the position of ALL or NOTHING. In other words you take ALL of Java s virtual machine constraints, or you can rewrite it in C or C++. The CLR provides the ability to run unmanaged code that is able to bypass some of the limitations inherent to ALL virtual machine implementations. C# does suffer the same Achilles heel faced by windows C and C++ developers. In fact it may be worse in C#. In C or C++, when accessing non-portable functionality of the OS, it is a very clear and conscious choice by the developer. In C#, due to the high integration with the.net platform, the developer may be utilizing non-portable functionality without even realizing it. In windows, accessing non-portable os features is more common than one would think, but not surprising if you accept that Microsoft has designed this effect into their API s intentionally over the years. The CLI is an ECMA standard. While this seems like a good point, it is deceptive and misleading. The CLI depends on many NON-PORTABLE libraries and OS features that are NOT part of the ECMA standard. The only way for Microsoft to have made the.net platform truly cross-platform, would have been to not support legacy code, and technologies, which Microsoft s Visual C++ and Visual Basic both rely on. In other words, impossible for Microsoft to do. C# does consider all types as first class objects, something that Java does not, and for which the Java spec has long been criticized by hard OO advocates. In this respect, C# is more true to OO where types are concerned. C# s Graphics and Windowing libararies blow the socks off (in terms of speed of compiled code at runtime) Java s Swing (JFC). Of course the obvious tradeoff is that

nobody has implemented a full set of.net compliant graphics libraries except Microsoft, and of course Microsoft s libraries only run on windows. The XML Documentation features intrinsic to the C# compiler spec is far superior to any other language documentation facility to date, hands down, no argument. It is also, undeniably more verbose, and complicated to learn to use. Authoring comments is complicated, and actual publication of comments in large projects is so complex, a special publication tool is necessary. A quick survey of how to publish comments in large projects, looked more complicated than the language itself. Conclusions: If you are a proponent of Microsoft technologies, there is no doubt that.net platform is a compelling and attractive choice for future development. Within the context of.net, C# is a very attractive choice, as it melds the quickness and ease of VB and Java, with the power and extensibility of C++. For Windows development, C# out powers VB, and out-eases C++. If you are a proponent of true cross platform, OO development, then C# will just not cut the mustard. Only a small sliver of the.net platform is standardized, the overwhelming majority is still proprietary Microsoft legacy code. The cross platform claims for.net by Microsoft just are not a reality. Bottom line, if you are developing solely for Windows, there is no reason the majority of your code should not be in C#. If you are targeting a cross platform audience, look more towards GNU based C++, or Java solutions.

Language Score Sheet: Feature Description Score Encapsulation Provides complete encapsulation through class and 10 field scope modifiers. Inheritence Provides single inheritance 9 Polymorphism Provides all features required to implement 10 polymorphism, including overloading, over-riding, etc. Abstraction Provides abstract classes 10 Classes are objects Allows representation of any real world concept as a collection of classes. Reuse Provides full reuse on Windows platforms. 5 Does not provide reuse on all platforms Libraries Provides a full standard library 10 Clarity of code Very easy for anyone familiar with C++ or Java 7 But does require some previous experience with Object Oriented languages Exception handling Full standard exception handling facilities 10 Verbose Not as verbose as C++, slightly more verbose than 7 Java, more verbose than VB Size Language is fairly small, and easy to learn 8 Syntax Very simple syntax easy for those familiar with 8 c++ and java. Some previous Object Oreiented language experience is needed Binding Supports late binding which is a powerful feature 7 but can lead to runtime errors Portability C# code is technically portable. Code is slightly 6 more portable than C and C++. MUCH LESS PORTABLE THAN JAVA. Compiled code may or may not be portable, based on the specific functionality implemented by the C# program. Reflection Ability to inspect its own objects and object environment 10

Sample Program: this is a sample of connecting to a database using System; using System.IO; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; public class DataReaderSample { public static void Main() { try { string myconnectionstring ="server=localhost; uid=; pwd=; database=mydb"; string myselectquery = "SELECT empno, empname,empsal FROM emp"; SqlConnection myconnection = new SqlConnection(myConnString); SqlCommand mycommand=new SqlCommand(mySelectQuery,myConnection); myconnection.open(); SqlDataAdapter mydatareader; mycommand.execute(out mydatareader); SqlInt32 empno; SqlString empname; sqlint32 empsal; int emp; while (mydatareader.read()) { empno = mydatareader.getsqlint32(0); empname = mydatareader.getsqlstring(1); empsal = mydatareader.getsqlint32(2); Console.Write(empno); Console.Write(empname); Console.Write(empsal); myconnection.close(); catch (Exception e){ Console.WriteLine ("Exception: {0", e.tostring());

Sources 1. A Comparative Overview of C# - http://genamics.com/developer/csharp_comparative.htm 2. Sun Homepage http://java.sun.com 3. C# In a Nutshell, Peter Drayton, Ben Albahari, Ted Neward, 2002 O Reilly 4. Microsoft Website http://msdn.microsoft.com/vstudio/techinfo/articles/upgrade/csharpintro.asp 5. Clear Common C# Hurdles http://www.dotnet247.com/247reference/a.aspx?u=http://www.fawcette.com/arch ives/premier/mgznarch/vbpj/2001/10oct01/dp0110/dp0110-1.asp 6. C# and its Types http://www.dotnet247.com/247reference/a.aspx?u=http://www.csharpcorner.com/language/csandtypesgag.asp