1 / 64

Visual programming Using C#

Visual programming Using C#. Chapter one Concepts. Prof. Yousef B. Mahdy.   Course Description.

mchurchill
Download Presentation

Visual programming Using C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual programming Using C# Chapter one Concepts Prof. Yousef B. Mahdy

  2.   Course Description • Course introduces programming using the C# programming language to solve business-related problems. Content includes program development and design, visual and object-oriented programming, screen design, structured programming techniques, and event-driven programming using objects. • Programming assignment concepts include arithmetic calculations, decision making, looping, reports to screen and paper, subroutines and functions, interactive processing, working with arrays, and introductory concepts of file creation and access. • This class will teach you how to program in C# and how to use Microsoft's new .NET platform. This is not an introductory programming class.

  3. Tentative Schedule • Introduction to .NET, & Visual Studio .NET • C# Basics and decisions structures • User-defined Types , Methods & Arrays • Class Design , Inheritance & Polymorphism • Strings and Exceptions • WinForms & GUI Concepts I • GUI Concepts & User Interfaces • Threads, Streams, & Networking • Database, SQL, and ADO.NET • Graphics

  4. Course resources • Textbook C# 2012, How to Program • Software • Visual Studio .NET 2010 Or 2013 Or C# 2010 /2013 Express edition • The .NET framework above 4.0

  5. Background • Modern software programming languages (like C# and VB.NET) utilize a human-friendly syntax that is not directly understandable by computers. Software commands in this human-friendly syntax are referred to as Source Code. Before a computer can execute the source code, special programs called compilers must rewriteit into machine instructions, also known as object code. This process (commonly referred to simply as “compilation”) can be done explicitly or implicitly. • Explicit Compilation • Explicit compilation converts the upper level language into object code prior to program execution. Ahead of time (AOT) compilers are designed to ensure that, the CPU can understand every line in the code before any interaction takes place. This is carried out by compilers.

  6. Compiler • Software that translate high-level language (C++, Java, C#, etc) to machine language. • Compiler X can covert high-level Y to machine language Z. • A big problem facing developers is the many different types of processors that run code. • Windows, Macintosh, and Unix machines use a wide variety of hardware, as do personal digital assistants, cell phones, large computers, and other platforms. • One way to make a program work on each of these devices is to translate the program to the native instruction.

  7. 1 • So if we have 3 programming languages and 3 devices, how many compilers do we need? • So, how they solved this?! • This can be solved by using Implicit compilation.

  8. Implicit Compilation • Implicit compilation is a two-step process.  The first step is converting the source code to intermediate language (IL) by a language-specific compiler. The second step is converting the IL to machine instructions. The main difference with the explicit compilers is that only executed fragments of IL code are compiled into machine instructions, at runtime. The .NET framework calls this compiler the JIT (Just-In-Time) compiler.  • IL is a CPU- and platform-independent instruction set. • It can be executed in any environment supporting the .NET framework

  9. 1

  10. Portability • Delivering portability is a key aspect when developing a program targeting a wide range of platforms. A couple of questions need answers to enable execution on multiple platforms: • What kind of CPU is used? • What Operating System (OS) will the program be running on? • To enable maximum reach of the software, the source code has to be compiled with a wide range of explicit compilers. • The implicit way delivers portability quite more effortlessly, because the first step of the process is much more platform agnostic. Each target platform has a JIT compiler deployed and as long as the IL can be interpreted the program can execute. The initial compiler does not need to know all of the places where the software might run.

  11. Example 1: Java • Every developer who uses Java knows that Java bytecode runs in a JRE (Java Runtime Environment). The most important element of the JRE is Java Virtual Machine (JVM), which analyzes and executes Java byte code. • A virtual machine (VM) is a software implementation of a machine (i.e. a computer) that executes programs like a physical machine. • Originally, Java was designed to run based on a virtual machine separated from a physical machine for implementing WORA (Write Once Run Anywhere), although this goal has been mostly forgotten. Therefore, the JVM runs on all kinds of hardware to execute the Java Bytecode without changing the Java execution code.

  12. JVM Structure

  13. Java: Execution Engine • The bytecode that is assigned to the runtime data areas in the JVM via class loader is executed by the execution engine. • The execution engine reads the Java Bytecode in the unit of instruction. It is like a CPU executing the machine command one by one. Each command of the bytecode consists of a 1-byte OpCode and additional Operand. The execution engine gets one OpCode and execute task with the Operand, and then executes the next OpCode. • But the Java Bytecode is written in a language that a human can understand, rather than in the language that the machine directly executes. Therefore, the execution engine must change the bytecode to the language that can be executed by the machine in the JVM. The bytecode can be changed to the suitable language in one of two ways:

  14. 1 • Interpreter: Reads, interprets and executes the bytecode instructions one by one. As it interprets and executes instructions one by one, it can quickly interpret one bytecode, but slowly executes the interpreted result. This is the disadvantage of the interpret language. The 'language' called Bytecode basically runs like an interpreter. • JIT (Just-In-Time) compiler: The JIT compiler has been introduced to compensate for the disadvantages of the interpreter. The execution engine runs as an interpreter first, and at the appropriate time, the JIT compiler compiles the entire bytecode to change it to native code. After that, the execution engine no longer interprets the method, but directly executes using native code.

  15. 2 • Execution in native code is much faster than interpreting instructions one by one. The compiled code can be executed quickly since the native code is stored in the cache (Java 6 and above).  • However, it takes more time for JIT compiler to compile the code than for the interpreter to interpret the code one by one. Therefore, if the code is to be executed just once, it is better to interpret it instead of compiling. Therefore, the JVMs that use the JIT compiler internally check how frequently the method is executed and compile the method only when the frequency is higher than a certain level.

  16. 3

  17. Example 2: NET Framework • In the .NET Framework, all the Microsoft .NET languages use a Common Language Runtime (CLR), which solves the problem of installing separate runtimes for each of the programming languages. • When the CLR is installed on a computer then it can run any language that is Microsoft .NET compatible. Before the Microsoft Intermediate Language (MSIL like bytecode in Java) can be executed, it must be converted by a .NET Framework Just-In-Time (JIT) compiler to native code, which is CPU-specific code that runs on the same computer architecture as the JIT compiler. • More …………………in this course

  18. Introduction C# • Microsoft developed C#, a new programming language based on the C and C++ languages. Microsoft describes C# in this way: ”C# is a simple, modern, object–oriented, and typesafe programming language derived from C and C++. C# (pronounced c sharp) is firmly planted in the C and C++ family tree of languages and will immediately be familiar to C and C++ programmers. C# aims to combine the high productivity of visual basic and raw power of C++.” • Anders Hejlsberg (أندرس هيلسبرج), the principal architect of C#, is known for his work with Borland on Turbo Pascal and Delphi (based on object–oriented Pascal). After leaving Borland, Hejlsberg worked at Microsoft on Visual J++.

  19. 1 • Some aspects of C# will be familiar to those, who have programmed in C, C++, or Java. • C# incorporates the Smalltalk concept, which means everything is an object. In other words, all types in C# are objects. • C# properties are similar to Visual Basic language properties. • The Rapid Application Development (RAD) goal in C# is assisted by C#’s use of concepts and keyword, such as class, structure, statement, operator, and enumeration. • The language also utilizes the concepts contained in the Component Object Model (COM) architecture. • Unlike Visual Basic or Delphi, Events is a type in C# and can belong to an object. Members of a class object can have variables, methods, properties, attributes, and events. Attributes are another nice feature of C# language.

  20. 2 • C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. • You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. • Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET Framework.

  21. 3 • C# syntax is highly expressive, yet it is also simple and easy to learn. The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. • C# syntax simplifies many of the complexities of C++ and provides powerful features such as nullable value types, enumerations, delegates, lambda expressions and direct memory access, which are not found in Java. • C# supports generic methods and types, which provide increased type safety and performance, and iterators, which enable implementers of collection classes to define custom iteration behaviors that are simple to use by client code. Language-Integrated Query (LINQ) expressions make the strongly-typed query a first-class language construct.

  22. 4 • As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. • A class may inherit directly from one parent class, but it may implement any number of interfaces. Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. • In C#, a struct is like a lightweight class; it is a stack-allocated type that can implement interfaces but does not support inheritance.

  23. 5 • In addition to these basic object-oriented principles, C# makes it easy to develop software components through several innovative language constructs, including the following: • Encapsulated method signatures called delegates, which enable type-safe event notifications. • Properties, which serve as accessors for private member variables. • Attributes, which provide declarative metadata about types at run time. • Inline XML documentation comments. • Language-Integrated Query (LINQ) which provides built-in query capabilities across a variety of data sources.

  24. 6 • If you have to interact with other Windows software such as COM objects or native Win32 DLLs, you can do this in C# through a process called "Interop." Interop enables C# programs to do almost anything that a native C++ application can do. C# even supports pointers and the concept of "unsafe" code for those cases in which direct memory access is absolutely critical. • The C# build process is simple compared to C and C++ and more flexible than in Java. There are no separate header files, and no requirement that methods and types be declared in a particular order. A C# source file may define any number of classes, structs, interfaces, and events.

  25. 7 • In summary: • The goal of C# is to provide a simple, safe, modern, object-oriented, Internet-centric, high-performance language for .NET development. C# is now a fully mature language, and it draws on the lessons learned over the past three decades. In much the way that you can see in young children the features and personalities of their parents and grandparents, you can easily see in C# the influence of Java, C++, Visual Basic (VB), and other languages, but you can also see the lessons learned since C# was first introduced.

  26. The .NET Platform • .NET Framework (pronounced dot net) is a software framework developed by Microsoft that runs primarily on Microsoft Windows. • The .NET platform is a development framework that provides a new application programming interface (API) to the services and APIs of classic Windows operating systems while bringing together a number of disparate technologies that emerged from Microsoft during the late 1990s. This includes COM+ component services, a commitment to XML and object-oriented design, support for new web services protocols such as SOAP, WSDL, and UDDI, and a focus on the Internet, all integrated within the Distributed interNet Applications (DNA) architecture.

  27. 1 • It includes a large class library known as Framework Class Library (FCL) and provides language interoperability (each language can use code written in other languages) across several programming languages. • Programs written for .NET Framework execute in a software environment (as contrasted to hardware environment), known as Common Language Runtime (CLR), an application virtual machine that provides services such as security, memory management, and exception handling. FCL and CLR together constitute .NET Framework

  28. 2 • FCL provides user interface, data access, database connectivity, cryptography, web application development, numeric algorithms, and network communications. Programmers produce software by combining their own source code with .NET Framework and other libraries. .NET Framework is intended to be used by most new applications created for Windows platform. Microsoft also produces an integrated development environment largely for .NET software called Visual Studio. • The .NET Framework sits on top of the operating system, which can be any flavor of Windows, and consists of a number of components, currently including:

  29. 3 • Five official languages: C#, VB, Visual C++, Visual J#, and JScript.NET • The CLR, an object-oriented platform for Windows and web development that all these languages share • A number of related class libraries, collectively known as the Framework Class Library • The following slides breaks down the .NET Framework into its system architectural components.

  30. 4

  31. .NET Framework Architecture • The OSmanages theresources, the processes and the users of the machine • Provides to the applications some services (threads, I/O, GDI+, DirectX, COM, COM+, MSMQ, IIS, WMI, …) • CLR is a separate process in the OS Operating System (OS)

  32. .NET Framework Architecture (2) • CLR managesthe execution of the.NET code • Manages the memory, concurrency, security, ... CLR Common Language Runtime (CLR) Operating System (OS)

  33. .NET Framework Architecture (3) • Rich object-oriented library with fundamental classes • Input-output, collections, text processing, networking,security, multi-threading, … Base Class Library (BCL) Common Language Runtime (CLR) Operating System (OS)

  34. .NET Framework Architecture (4) • Database access • ADO.NET, LINQ, LINQ-to-SQL and Entity Framework • Strong XML support ADO.NET, LINQ and XML (Data Tier) Base Class Library (BCL) Common Language Runtime (CLR) Operating System (OS)

  35. .NET Framework Architecture (5) • Windows Communication Foundation (WCF) and Windows Workflow Foundation (WWF) for the SOA world WCF and WWF (Communication and Workflow Tier) ADO.NET, LINQ and XML (Data Tier) Base Class Library (BCL) Common Language Runtime (CLR) Operating System (OS)

  36. .NET Framework Architecture (6) • User interface technologies: Web based, Windows GUI, WPF, Silverlight, mobile, … Windows Forms WPF Silverlight ASP.NET Web Forms, MVC, AJAX Mobile Internet Toolkit WCF and WWF (Communication and Workflow Tier) ADO.NET, LINQ and XML (Data Tier) Base Class Library (BCL) Common Language Runtime (CLR) Operating System (OS)

  37. .NET Framework Architecture (7) • Programming language on your flavor! … Delphi C# C++ VB.NET J# F# JScript Perl Windows Forms WPF Silverlight ASP.NET Web Forms, MVC, AJAX Mobile Internet Toolkit WCF and WWF (Communication and Workflow Tier) ADO.NET, LINQ and XML (Data Tier) Base Class Library (BCL) Common Language Runtime (CLR) Operating System (OS)

  38. Common Language Runtime (CLR) • The most important component of the .NET Framework is the CLR, which provides the environment in which programs are executed. The CLR includes a virtual machine, analogous in many ways to the Java virtual machine. At a high level, the CLR activates objects, performs security checks on them, lays them out in memory, executes them, and garbage-collects them. (The Common Type System is also part of the CLR.). • Managed execution environment: • Controls the execution of managed .NET programming code • Something like virtual machine: • Like the Java Virtual Machine (JVM)

  39. 1 • Not an interpreter • Compilation on-demand is used • Known as Just In Time (JIT) compilation • Possible compilation in advance (Ngen). • Responsibilitiesof CLR: • Execution of theIL codeandthe JIT compilation • Managing memory and application resources • Ensuring type safety • Interaction with the OS • Managing security • Code access security • Role-based security

  40. 2 • Managing exceptions • Managing concurrency – controlling the parallel execution of application threads • Managing application domains and their isolation • Interaction with unmanaged code • Supportingdebug /profile of .NET code

  41. The framework's Base Class Library • The framework's Base Class Library provides a large range of features including user interface, data and data access, database connectivity, cryptography, web application development, numeric algorithms, and network communications. The class library is used by programmers, who combine it with their own code to produce applications. • Programs written for the .NET Framework execute in a software environment that manages the program's runtime requirements.

  42. Managed and Unmanaged Code • Managed Code: • Managed code is what Visual Basic .NET and C# compilers create. It runs on the CLR (Common Language Runtime), which, among other things, offers services like garbage collection, run-time type checking, and reference checking. So, think of it as, "My code is managed by the CLR." • Visual Basic and C# can only produce managed code, so, if you're writing an application in one of those languages you are writing an application managed by the CLR. If you are writing an application in Visual C++ .NET you can produce managed code if you like, but it's optional.

  43. 1 • Unmanaged Code: • Unmanaged code compiles straight to machine code. So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable. • No free memory management or anything else the CLR provides. • Since you cannot create unmanaged code with Visual Basic or C#, in Visual Studio all unmanaged code is written in C/C++.

  44. 2

  45. Compilation and the MSIL • In .NET, programs aren't compiled into executable files; they are compiled into assemblies that consist of MicrosoftIntermediateLanguage (IL) instructions, which the CLR then converts into machine code and executes. • The MSIL (often shortened to IL) files C# produces are nearly identical to the IL files other .NET languages produce; the platform is language-agnostic. A key fact about the CLR is that it is common: the same runtime supports development in C# as well as in VB.NET. • C# code is compiled into IL when you build your project. The IL is saved in a file on disk. When you run your program, the IL is compiled again, using the JustInTime (JIT) compiler (a process often called JITing). The result is machine code, executed by the machine's processor.

  46. 1 • The standard JIT compiler runs ondemand. When a method is called, the JIT compiler analyzes the IL and produces highly efficient machine code, which runs very fast. As the application runs, compilation happens only as needed, and once JIT-compiled, the code is cached for future use. As .NET applications run, they tend to become faster and faster, as the already compiled code is reused.

  47. 2

  48. Just-In-Time Compilation     • The JIT compiler is part of the Common Language Runtime (CLR). The CLR manages the execution of all .NET applications. In addition to JIT compilation at runtime, the CLR is also responsible for garbage collection, type safety and for exception handling. • Different machine configurations use different machine level instructions. As Figure 1 shows, the source code is compiled to exe or dll by the .NET compiler. Common Intermediate Language (CIL) consists of instructions that any environment supporting .NET can execute and includes metadata describing structures of both data and code. The JIT Compiler processes the CIL instructions into machine code specific for an environment. Program portability is ensured by utilizing CIL instructions in the source code.

  49. 1

  50. 2 • The JIT compiler compiles only those methods called at runtime. It also keeps track of any variable or parameter passed through methods and enforces type-safety in the runtime environment of the .NET Framework. • There are three types of JIT compilation in the .NET framework: • Pre-JIT Compiler (Compiles entire code into native code completely) • Econo JIT Compiler (Compiles code part by part freeing when required) • Normal JIT Compiler (Compiles only that part of code when called and places in cache

More Related