1、C# Classes 12C# Structs 16C# Delegates & Events 17Garbage Collection 19Java Programmer Point of View 21Objective-C Programmer Point of View 22Summary 23OverviewC# (pronounced C sharp) is a simple, modern, object-oriented, and type-safe programming language. It will immediately be familiar to C/C+ an
2、d Java programmers. C# combines the high productivity of Rapid Application Development (RAD) languages and the raw power of C+.This article introduces C#, and compares it to other modern object-oriented languages like Objective-C and Java.Objectives When youve finished the article, you will have: A
3、high-level understanding of the C# programming language. A high-level understanding of the .NET framework. A clear view of the differences and similarities in the implementation of basic scenarios in C# versus Java and Objective-C.Introduction to C#C# is an object-oriented language that enables prog
4、rammers to build quickly a wide range of applications for the Microsoft .NET framework. The goal of C# and the .NET framework is to shorten development time by freeing the developer from worrying about several low-level programming tasks such as memory management, type safety issues, standard librar
5、ies, array bounds checking, and so on. This allows developers to spend the majority of development time working on their applications and business logic instead. C# is a simple, modern object-oriented and type-safe programming language. Let us see why.Simple Being a part of the Microsoft .NET platfo
6、rm, C# applications receive support from the runtime, and they use services provided by the runtime. The .NET framework is a managed environment: the runtime manages memory allocationthis frees the developer from that task. Programmers do not have to deal with the burden of memory management and eli
7、minating memory leaks. C# supports operator overloading, similarly to C+; Java and Objective-C do not support it. Inheritance-related features are clearer and more precise than in C+:An abstract keyword is used instead of declaring pure virtual functions.Abstract classes are declared as such directl
8、y by using the abstract keyword, not indirectly by lack of method/function implementation (pure virtual functions). An abstract class may be completely implemented and yet be declared abstract for architectural reasons. An override keyword is used for overriding a virtual method. New class member ty
9、pes, such as properties and indexers, are introduced to simplify common tasks such as list iteration and property getting and setting. Less Error ProneC# is less error-prone than C+ due to the following characteristics: Unlike C+, where it is possible to declare any type of pointer and assign it to
10、any allocated address or cast it to any other type, C# allows only type-safe casting. Casting errors are detected at either compile time or at run time. However, in either situation, you will not miss any errors. The use of Generic types allows template classes to remain type-safe at compile time. T
11、his allows the programmer to detect errors during compile, rather than at run time. Running under the managed CLR, bounds checking is performed for managed arrays automatically while accessing the allocated array member object. This prevents accidental memory corruption.ModernC# is designed to meet
12、modern application requirements, as well as integrating modern software concepts: C#, as a .NET language, provides a larger set of basic types, including for example the Decimal type used to handle financial applications correctly. Known concepts, such as enumerated types, bit flags, first-class fun
13、ctions and others, become first-class citizen types with a rich set of functionalities that represent type-specific usage requirements. It contains a built-in exception-handling mechanism. C# provides a built-in event handler for simpler handling of integrated (for example, GUI and Web) application
14、requirements. C# provides a complete reflection mechanism, allowing object metadata to be explored during run time.Object OrientedC# supports the three pillars of Object-Oriented Programming: EncapsulationA class has tools to hide its implementation details and be responsible for its objects state I
15、nheritanceThe “is a” relationship can be defined between classes, enabling extensibility and reusability PolymorphismA program can handle different types of objects identically, sharing the same base, having polymorphic behavior, based on specific object typesEverything in C# is part of a type (for
16、example, class, interface, and struct). There are no “global” (non-member) functions or variables.Every type instance “is an object.” All types are derived from a common base class: object.While these concepts are very familiar to Java developers, they are fundamentally different (and in some cases
17、even completely new) for C/C+/Objective-C developers.A few words about JavaJava is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems Java platform. The language derives much of its syntax from C and C+ but ha
18、s a simpler object model and fewer low-level facilities. Java applications typically are compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is general-purpose, concurrent, class-based, and object-oriented. It is designed specifi
19、cally to have as few implementation dependencies as possible.A few words about Objective-CThe Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming. Objective-C is defined as a small but powerful set of extensions to the standard ANSI C langu
20、age. It is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class. Its additions to C are mostly inspired by Smalltalk, one of the first object-oriented programming languages. All of the syntax for non-object-oriented operations (incl
21、uding primitive variables, preprocessing, expressions, function declarations, and function calls) is identical to C, while the syntax for object-oriented features is an implementation of Smalltalk-style messaging. Today, Objective-C is used primarily on Apples Mac OS X and iOS: two environments base
22、d on the OpenStep standard, though not compliant with it. Objective-C is the primary language used for Apples Cocoa API, and it was originally the main language on NeXTs NeXTSTEP OS. ”Historically, all articles about programming languages start with a “Hello World!” sample. This article will follow
23、this traditionhere is a C# version of this popular program:C#using System;class HelloApp static void Main() Console.WriteLine(Hello C#!); This code references the System namespace, which provides the Console class, declares a class and enclosed Main static method, which, in turn, calls the Console c
24、lasss WriteLine static method to output a string.Note: Running the above sample directly under Windows Phone 7 will not produce any tangible output, as the Windows Phone 7 console is not accessible. The code provided is only for comparison.Let us see the same program written in Java:Javapublic class
25、 HelloWorld public static void main(String args) System.out.println(Hello Java!And now in Objective-C:Objective-C#import int main( int argc, const char *argv ) printf( Hello Objective-C!n ); return 0;The first difference is in code organization: while both C# and Java have a class with one method, O
26、bjective-C has just a method. The second difference lies in the signature of the method: in C#, the “Main” method must start with capital “M”, while in Java and Objective-C, it is not a requirement. The “String” argument for the “Main” method in C# is optional.The third difference is in the “Main” m
27、ethods return type: in C# (and Java), it returns void while in Objective-C it returns “int”. In C#, the Main method could also return an “int” value.The more we examine the code the more differences we will discover.Defining a ClassMuch of object-oriented programming consists of writing code for new
28、 objectsdefining new classes. The class definitions in C# and Java are fairly similar:C# (and Java)class Fraction public int numerator; public int denominator; public void Print() /C#0/1, numerator, denominator); /Java /System.out.println(new PrintfFormat(%d/%d).sprintf(numerator, denominator);In Ob
29、jective-C, classes are defined in two parts:1. An interface that declares the methods and instance variables of the class and names its superclass2. An implementation that actually defines the class (contains the code that implements its methods)Typically, these are split between two files, though s
30、ometimes a class definition may span several files by using a feature called a “category.” The same class/interface written in Objective-C:Foundation/NSObject.hinterface Fraction: NSObject int numerator; int denominator;-(void) print;-(void) setNumerator: (int) n;-(void) setDenominator: (int) d;-(int) numerator;-(int) denominator;end#import Fraction.himplementation Fraction-(void) print %i/%i, numerator, denominator ); (int) n numerator = n; (int) d denominator = d;-(int) denominator return denominat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1