15
8/8/2019 Introduction to Dotnet and c# http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 1/15 http://msdn.microsoft.com/en-us/library/zw4w595w.aspx NET Framework Developer's Guide .NET Framework Conceptual Overview The .NET Framework is an integral Windows component that supports building and running the next generation of applications and XML Web services. The .NET Framework is designed to fulfill the following objectives: y  To provide a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely. y  To provide a code-execution environment that minimizes software deployment and versioning conflicts. y  To provide a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party. y  To provide a code-execution environment that eliminates the performance problems of scripted or interpreted environments. y  To make the developer experience consistent across widely varying types of applications, such as Windows-based applications and Web-based applications. y  To build all communication on industry standards to ensure that code based on the .NET Framework can integrate with any other code. The .NET Framework has two main components: the common language runtime and the .NET Framework class library. The common language runtime is the foundation of the .NET Framework. You can think of the runtime as an agent that manages code at execution time, providing core services such as memory management, thread management, and remoting, while also enforcing strict type safety and other forms of code accuracy that promote security and robustness. In fact, the concept of code management is a fundamental principle of the runtime. Code that targets the runtime is known as managed code, while code that does not target the runtime is known as unmanaged code. The class library, the other main component of the .NET Framework, is a comprehensive, object- oriented collection of reusable types that you can use to develop applications ranging from traditional command- line or graphical user interface (GUI) applications to applications based on the latest innovations provided by ASP.NET, such as Web Forms and XML Web services. The .NET Framework can be hosted by unmanaged components that load the common language runtime into their processes and initiate the execution of managed code, thereby creating a software environment that can exploit both managed and unmanaged features. The .NET Framework not only provides several runtime hosts, but also supports the development of third-party runtime hosts. For example, ASP.NET hosts the runtime to provide a scalable, server-side environment for managed code. ASP.NET works directly with the runtime to enable ASP.NET applications and XML Web services, both of which are discussed later in this topic. Internet Explorer is an example of an unmanaged application that hosts the runtime (in the form of a MIME type extension). Using Internet Explorer to host the runtime enables you to embed managed components or Windows Forms controls in HTML documents. Hosting the runtime in this way makes managed mobile code (similar to Microsoft® ActiveX® controls) possible, but with significant improvements that only managed code can offer, such as semi-trusted execution and isolated file storage.

Introduction to Dotnet and c#

Embed Size (px)

Citation preview

Page 1: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 1/15

http://msdn.microsoft.com/en-us/library/zw4w595w.aspx

NET Framework Developer's Guide

.NET Framework Conceptual Overview

The .NET Framework is an integral Windows component that supports building and running the next generation of applications and XML Web services. The .NET Framework is designed to fulfill the following objectives:

y  To provide a consistent object-oriented programming environment whether object code is stored and

executed locally, executed locally but Internet-distributed, or executed remotely.

y  To provide a code-execution environment that minimizes software deployment and versioning conflicts.

y  To provide a code-execution environment that promotes safe execution of code, including code created by

an unknown or semi-trusted third party.

y  To provide a code-execution environment that eliminates the performance problems of scripted or

interpreted environments.

y  To make the developer experience consistent across widely varying types of applications, such as

Windows-based applications and Web-based applications.

y  To build all communication on industry standards to ensure that code based on the .NET Framework can

integrate with any other code.

The .NET Framework has two main components: the common language runtime and the .NET Framework class

library. The common language runtime is the foundation of the .NET Framework. You can think of the runtime as

an agent that manages code at execution time, providing core services such as memory management, thread

management, and remoting, while also enforcing strict type safety and other forms of code accuracy that promote

security and robustness. In fact, the concept of code management is a fundamental principle of the runtime. Code

that targets the runtime is known as managed code, while code that does not target the runtime is known as

unmanaged code. The class library, the other main component of the .NET Framework, is a comprehensive, object-

oriented collection of reusable types that you can use to develop applications ranging from traditional command-

line or graphical user interface (GUI) applications to applications based on the latest innovations provided by

ASP.NET, such as Web Forms and XML Web services.

The .NET Framework can be hosted by unmanaged components that load the common language runtime into their

processes and initiate the execution of managed code, thereby creating a software environment that can exploit

both managed and unmanaged features. The .NET Framework not only provides several runtime hosts, but also

supports the development of third-party runtime hosts.

For example, ASP.NET hosts the runtime to provide a scalable, server-side environment for managed code.

ASP.NET works directly with the runtime to enable ASP.NET applications and XML Web services, both of which are

discussed later in this topic.

Internet Explorer is an example of an unmanaged application that hosts the runtime (in the form of a MIME type

extension). Using Internet Explorer to host the runtime enables you to embed managed components or Windows

Forms controls in HTML documents. Hosting the runtime in this way makes managed mobile code (similar to

Microsoft® ActiveX® controls) possible, but with significant improvements that only managed code can offer, such

as semi-trusted execution and isolated file storage.

Page 2: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 2/15

The following illustration shows the relationship of the common language runtime and the class library to your

applications and to the overall system. The illustration also shows how managed code operates within a larger

architecture.

.NET Framework in context

The following sections describe the main components and features of the .NET Framework in greater detail.

Features of the Common Language Runtime

The common language runtime manages memory, thread execution, code execution, code safety verification,

compilation, and other system services. These features are intrinsic to the managed code that runs on the common

language runtime.

With regards to security, managed components are awarded varying degrees of trust, depending on a number of 

factors that include their origin (such as the Internet, enterprise network, or local computer). This means that a

managed component might or might not be able to perform file-access operations, registry-access operations, or

other sensitive functions, even if it is being used in the same active application.

The runtime enforces code access security. For example, users can trust that an executable embedded in a Web

page can play an animation on screen or sing a song, but cannot access their personal data, file system, or

Page 3: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 3/15

network. The security features of the runtime thus enable legitimate Internet-deployed software to be exceptionally

feature rich.

The runtime also enforces code robustness by implementing a strict type-and-code-verification infrastructure called

the common type system (CTS). The CTS ensures that all managed code is self-describing. The various Microsoft

and third-party language compilers generate managed code that conforms to the CTS. This means that managed

code can consume other managed types and instances, while strictly enforcing type fidelity and type safety.

In addition, the managed environment of the runtime eliminates many common software issues. For example, the

runtime automatically handles object layout and manages references to objects, releasing them when they are no

longer being used. This automatic memory management resolves the two most common application errors,

memory leaks and invalid memory references.

The runtime also accelerates developer productivity. For example, programmers can write applications in their

development language of choice, yet take full advantage of the runtime, the class library, and components written

in other languages by other developers. Any compiler vendor who chooses to target the runtime can do so.

Language compilers that target the .NET Framework make the features of the .NET Framework available to existing

code written in that language, greatly easing the migration process for existing applications.

While the runtime is designed for the software of the future, it also supports software of today and yesterday.

Interoperability between managed and unmanaged code enables developers to continue to use necessary COM

components and DLLs.

The runtime is designed to enhance performance. Although the common language runtime provides many standard

runtime services, managed code is never interpreted. A feature called just-in-time (JIT) compiling enables all

managed code to run in the native machine language of the system on which it is executing. Meanwhile, the

memory manager removes the possibilities of fragmented memory and increases memory locality-of-reference to

further increase performance.

Finally, the runtime can be hosted by high-performance, server-side applications, such as Microsoft® SQL Server

and Internet Information Services (IIS). This infrastructure enables you to use managed code to write your

business logic, while still enjoying the superior performance of the industry's best enterprise servers that support

runtime hosting.

.NET Framework Class Library

The .NET Framework class library is a collection of reusable types that tightly integrate with the common language

runtime. The class library is object oriented, providing types from which your own managed code can derive

functionality. This not only makes the .NET Framework types easy to use, but also reduces the time associated with

learning new features of the .NET Framework. In addition, third-party components can integrate seamlessly with

classes in the .NET Framework.

For example, the .NET Framework collection classes implement a set of interfaces that you can use to develop your

own collection classes. Your collection classes will blend seamlessly with the classes in the .NET Framework.

As you would expect from an object-oriented class library, the .NET Framework types enable you to accomplish a

range of common programming tasks, including tasks such as string management, data collection, database

connectivity, and file access. In addition to these common tasks, the class library includes types that support a

variety of specialized development scenarios. For example, you can use the .NET Framework to develop the

following types of applications and services:

y  Console applications. See Building Console Applications.

Page 4: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 4/15

y  Windows GUI applications (Windows Forms). See Windows Forms.

y  Windows Presentation Foundation (WPF) applications. See Introduction to Windows Presentation

Foundation.

y  ASP.NET applications. See Creating ASP.NET Web Pages.

y  Web services. See Creating Web Services in Managed Code.

y  Windows services. See Introduction to Windows Service Applications.

For example, the Windows Forms classes are a comprehensive set of reusable types that vastly simplify Windows

GUI development. If you write an ASP.NET Web Form application, you can use the Web Forms classes.

Page 5: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 5/15

 

1. 1Apakah Itu Framework .NET ?

Framework .NET adalah suatu komponen windows yang terintegrasi yang dibuat dengan tujuanuntuk mensupport pengembangan berbagai macam jenis aplikasi serta untuk dapat mejalankan

  berbagai macam aplikasi generasi mendatang termasuk pengembangan aplikasi Web Services

XML.Framework .NET di design untuk dapat memenuhi beberapa tujuan berikut ini :

Untuk menyediakan environment kerja yang konsisten bagi bahasa pemrograman yang berorientasi objek (object-oriented programming - OOP ) baik kode objek itu di simpan

dan di eksekusi secara lokal, atau dieksekusi secara lokal tapi didistribusikan melaluiinternet atau di eksekusi secara remote.

Untuk menyediakan environment kerja di dalam mengeksekusi kode yang dapat

meminimaliasi proses software deployment  dan menghindari konflik penggunaan versisoftware yang di buat.

Untuk menyediakan environment kerja yang aman dalam hal pengeksekusian kode,

termasuk kode yang dibuat oleh pihak ketiga (third party).

Untuk menyediakan environment kerja yang dapat mengurangi masalah pada persoalan performa dari kode atau dari lingkungan interpreter nya.

Membuat para developer lebih mudah mengembangkan berbagai macam jenis aplikasi

yang lebih bervariasi, seperti aplikasi berbasis windows dan aplikasi berbasis web.

Membangun semua komunikasi yang ada di dalam standar industri untuk memastikan  bahwa semua kode aplikasi yang berbasis Framework .NET dapat berintegrasi dengan

 berbagai macam kode aplikasi lain.

Page 6: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 6/15

1.2 Arsitektur Framework .NET

Framework .NET terdiri dari dua buah komponen utama, yaitu C ommon Language Runtime

( C  LR) dan .NET Framework C lass Library atau kadang juga sering disebut dengan  Base C lass Library (BC  L).

Common Language Runtime (CLR) adalah pondasi utama dari Framework .NET. CLR 

merupakan komponen yang bertanggung jawab terhadap berbagai macam hal, seperti bertanggung jawab untuk melakukan managemen memory, melakukan eksekusi kode,

melakukan verifikasi terhadap keamanan kode, menentukan hak akses dari kode, melakukan

kompilasi kode, dan berbagai layanan system lainnya. Dengan adanya fungsi CLR ini, maka

aplikasi berbasis .NET biasa juga disebut dengan managed code, sedangkan aplikasi di luar itu

 biasa disebut dengan un-managed code.

 .NET  Framework Class Library atau sering juga disebut   Base Case Library (BCL)  adalah

koleksi dari reusable types yang sangat terintegrasi secara melekat dengan CLR. Class library  bersifat berorientasi terhadap objek yang akan menyediakan types dari fungsi-fungsi managed 

code. Hal ini tidak hanya berpengaruh kepada kemudahan dalam hal penggunaan, tetapi jugadapat mengurangi waktu yang diperlukan pada saat eksekusi. Dengan sifat tersebut, maka

komponen pihak ketiga akan dengan mudah diaplikasikan ke dalam aplikasi yang dibuat.Dengan adanya BCL ini, maka kita bisa menggunakan Framework .NET untuk membuat

 berbagai macam aplikasi, seperti :

Aplikasi console

Aplikasi berbasis windowd (Windows Form) 

Aplikasi ASP.NET (berbasis web) 

Aplikasi Web Services XML

Aplikasi berbasis Windows Services

Page 7: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 7/15

Mengapa C# ?

Pertanyaan di atas mungkin muncul di benak kita semua pada saat pertama kali mendengar tentang C#. Hal tersebut sangat beralasan, dengan melihat kenyataan bahwa sebelum C#muncul, telah banyak bahasa pemrograman yang ada, seperti C, C++, Java, Perl dan lain-lain.

 Ada beberapa alasan kenapa memilih C#, yaitu :Sederhana (simple)C# menghilangkan beberapa hal yang bersifat kompleks yang terdapat dalam beberapa macambahasa pemrograman seperti Java dan C++, termasuk diantaranya mengilangkan macro,templates, multiple inheritance dan virtual base classes. Hal-hal tersebut yang dapatmenyebabkan kebingunan pada saat menggunakannya, dan juga berpotensial dapat menjadimasalah bagi para programmer C++. Jika anda pertama kali belajar bahasa C# sebagai bahasapemrograman, maka hal-hal tersebut di atas tidak akan membuat waktu anda terbuang terlalubanyak untuk mempelajarinya.C# bersifat sederhana, karena bahasa ini didasarkan kepada bahasa C dan C++. Jika andafamiliar dengan C dan C++ atau bahkan Java, anda akan menemukan aspek-aspek yang begitufamiliar, seperti statements, expression, operators, dan beberapa fungsi yang diadopsilangsung dari C dan C++, tetapi dengan berbagai perbaikan yang membuat bahasanya menjadilebih sederhana.Modern

  Apa yang membuat C# menjadi suatu bahasa pemrograman yang modern? Jawabannyaadalah adanya beberapa fitur seperti exception handling , garbage collection, extensible datatypes, dan code security  (keamanan kode/bahasa pemrograman). Dengan adanya fitur-fitur tersebut, menjadikan bahasa C# sebagai bahasa pemrograman yang modern.

17  Pengenalan BahasaC# ( C SH101)

Page 8: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 8/15

Object-Oriented LanguageKunci dari bahasa pemrograman yang bersifat Object  Oriented  adalah encapsulation,inheritance, dan  polymorphism. Secara sederhana, istilah-istilah tersebut bisa didefinisikansebagai berikut (definisi dan penjelasan lebih lanjut akan di uraikan pada bab-bab selanjutnya).encapsulation, dimana semua fungsi ditempatkan dalam satu paket (single package).inheritance, adalah suatu cara yang terstruktur dari suatu kode-kode pemrograman dan fungsi

untuk menjadi sebuat program baru dan berbentuk suatu paket. polymorphism, adalah kemampuan untuk mengadaptasi apa yang diperlukan untuk dikerjakan.Sifat-sifat tersebut di atas, telah di miliki oleh C# sehingga bahasa C# merupakan bahasa yangbersifat Object Oriented .Powerfull dan fleksibelC# bisa digunakan untuk membuat berbagai macam aplikasi, seperti aplikasi pengolah kata,grapik, spreadsheets, atau bahkan membuat kompiler untuk sebuah bahasa permrograman.EfisienC# adalah bahasa pemrograman yang menggunakan jumlah kata-kata yang tidak terlalu

banyak. C# hanya berisi kata-kata yang biasa disebut dengan keywords. Keywords ini

digunakan untuk menjelaskan berbagai macam informasi. Jika anda berpikiran bahwa bahasa

pemrograman yang menggunakan sangat banyak kata-kata (keywords) akan lebih powerfull,

maka jawabannya adalah ³pemikiran itu tidak selalu benar´, karena hal itu justru bisa

menambah kerumitan para developer pada saat membuat suatu aplikasi. Berikut daftar 

keywords yang ada dalam bahasa C#:

Page 9: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 9/15

Type Data C#

Page 10: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 10/15

 

Page 11: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 11/15

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.datatype(VS.71).aspx

The DataType property supports the base .NET Framework data types shown below:

y  Boolean 

y  Byte 

y  Char 

y  DateTime 

y  Decimal 

y  Double 

y  Int16 

y  Int32 

y  Int64 

y  SByte 

y  Single 

y  String 

y  TimeSpan 

y  UInt16 

y  UInt32 

y  UInt64 

Page 12: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 12/15

http://msdn.microsoft.com/en-us/library/aa691323(VS.71).aspx

The following table summarizes all operators in order of precedence from highest to lowest:

Section Category Operators

Section 7.5 Primaryx.y f (x) a[x] x++ x-- new typeof checked unchecked

Section 7.6 Unary + - ! ~ ++x --x (T)x

Section 7.7 Multiplicative * / %

Section 7.7   Additive + -

Section 7.8 Shift << >>

Section 7.9 Relational andtype testing

< > <= >= is as

Section 7.9 Equality == !=

Section7.10

Logical AND &

Section7.10

Logical XOR ^

Section7.10

Logical OR |

Section

7.11

Conditional

 AND

&&

Section7.11

Conditional OR ||

Section7.12

Conditional ?:

Section7.13

  Assignment = *= /= %= += -= <<= >>= &= ^= |=

Page 13: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 13/15

 

Operator Logika dan Perbandingan 

http://handaru.blogsome.com/2007/08/15/operator-logika-dan-perbandingan/

Operator Logika 

Contoh  Operasi  Hasil 

a && b and benar jika a dan b adalah benar 

a || b or benar jika salah satu dari a atau b adalah benar 

!a not benar jika a tidak benar 

Operator Perbandingan 

Contoh  Operasi  Hasil 

a == b sama dengan benar jika a sama dengan b

a != b tidak sama dengan benar jika a tidak sama dengan b

a < b kurang dari benar jika a kurang dari b

a > b lebih dari benar jika a lebih dari b

a <= b kurang dari atau sama dengan benar jika a kurang dari atau sama dengan b

a >= b lebih dari atau sama dengan benar jika a lebih dari atau sama dengan b

Page 14: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 14/15

Accessibility Levels

When access is allowed to a member, it said to be accessible. Otherwise, it is inaccessible. Use the access

modifiers, public, protected, internal, or private, to specify one of the following declared accessibilities for

members.

Declared accessibility Meaning

public Access is not restricted.

protected Access is limited to the containing class or types derived from thecontaining class.

internal Access is limited to the current assembly.

protected internal Access is limited to the current assembly or types derived from thecontaining class.

private Access is limited to the containing type.

Only one access modifier is allowed for a member or type, except for the protected internal combination.

Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.

Depending on the context in which a member declaration takes place, only certain declared accessibilities are

permitted. If no access modifier is specified in a member declaration, a default accessibility is used.

Top-level types, which are not nested into other types, can only have internal or public accessibility. The default

accessibility for these types is internal.

Nested types, which are members of other types, can have declared accessibilities as indicated in the following

table.

Members of  Default member accessibility Allowed declared accessibility of the member

enum  public None

class  private  public protected internal private protected internal 

interface  public None

struct  private  public internal private 

Page 15: Introduction to Dotnet and c#

8/8/2019 Introduction to Dotnet and c#

http://slidepdf.com/reader/full/introduction-to-dotnet-and-c 15/15

Example Using Protected 

class A

{

protected int x = 123;

}

class B : A

{

void F()

{

A a = new A();

B b = new B();

a.x = 10; // Error

b.x = 10; // OK

}