1、IntroToCSharpArticle40 ArticleIntro to C# (vs. Objective-C and Java)Document version: 1.0.0Last updated: 10/31/2018ContentsOverview 3Objectives 3Introduction to C# 3“Hello C#!” 6.NET Runtime 9.NET and C# Program Basics 10C# Classes 12C# Structs 16C# Delegates & Events 17Garbage Collection 19Java Pro
2、grammer 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+ and Java programmers. C# combines the high productivity of Rapid Application Deve
3、lopment (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 high-level understanding of the C# programming language. A high-level understan
4、ding 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 programmers to build quickly a wide range of applications for the Microsoft .NET fr
5、amework. 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 libraries, array bounds checking, and so on. This allows developers to spend the majo
6、rity 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 platform, C# applications receive support from the runtime, and they use services pro
7、vided 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 eliminating memory leaks. C# supports operator overloading, similarly to C+; Java
8、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 directly by using the abstract keyword, not indirectly by lack of method/function impl
9、ementation (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 types, such as properties and indexers, are introduced to simplify common tasks s
10、uch 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 any allocated address or cast it to any other type, C# allows only type-safe ca
11、sting. 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. This allows the programmer to detect errors during compile, rather than at run t
12、ime. 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 modern application requirements, as well as integrating modern software concept
13、s: 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 functions and others, become first-class citizen types with a rich set of function
14、alities 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 requirements. C# provides a complete reflection mechanism, allowing object meta
15、data 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 InheritanceThe “is a” relationship can be defined between classes, enabling exte
16、nsibility 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 example, class, interface, and struct). There are no “global” (non-member) func
17、tions 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 even completely new) for C/C+/Objective-C developers.A few words about JavaJava
18、 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 has a simpler object model and fewer low-level facilities. Java applications typi
19、cally 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 specifically to have as few implementation dependencies as possible.A few words about
20、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 language. It is possible to compile any C program with an Objective-C compiler, and
21、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 (including primitive variables, preprocessing, expressions, function declarations, a
22、nd 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 based on the OpenStep standard, though not compliant with it. Objective-C is the pr
23、imary language used for Apples Cocoa API, and it was originally the main language on NeXTs NeXTSTEP OS. “Hello C#!”Historically, all articles about programming languages start with a “Hello World!” sample. This article will follow this traditionhere is a C# version of this popular program:C#using Sy
24、stem;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 classs WriteLine static method to output a string.Note: Running the ab
25、ove 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 HelloWorld public static void main(String args) System.out.println(H
26、ello 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, Objective-C has just a method. The second difference lies in the si
27、gnature 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” methods return type: in C# (and Java), it returns void while in Obj
28、ective-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 objectsdefining new classes. The class definitions in C# and Java
29、 are fairly similar:C# (and Java)class Fraction public int numerator; public int denominator; public void Print() /C# Console.WriteLine(0/1, numerator, denominator); /Java /System.out.println(new PrintfFormat(%d/%d).sprintf(numerator, denominator); In Objective-C, classes are defined in two parts:1.
30、 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 sometimes a class definition may span several f
31、iles by using a feature called a “category.” The same class/interface written in Objective-C:Objective-C#import interface Fraction: NSObject int numerator; int denominator;-(void) print;-(void) setNumerator: (int) n;-(void) setDenominator: (int) d;-(int) numerator;-(int) denominator;endObjective-C#import Fraction.h#import implementation Fraction-(void) print printf( %i/%i, numerator, denominator );-(void) setNumerator: (int) n numerator = n;-(void) setDenominator: (int) d denominator = d;-(int) denominator return denominat
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1