2 Computer programming: creating a sequence of instructions to enable the computer to do something Programmers do not use machine language when creating computer programs. Instead, programmers tend to use high-level programming languages Each high-level language has its own syntax and limited set of vocabulary that is translated into machine code by a compiler Define a task/problem = Specification Plan your solution = Design Find suitable algorithm to solve it Find suitable data structures to use Write code Fix program error (bugs) Make your customer happy = Implementation = Testing & Debugging = Deployment 3 4 Wikipedia.org definition for C#. Object-oriented. Primarily imperative or procedural. LINQ (Language Integrated Query) adds some functional programming language capabilities. Structured (as opposed to monolithic). Strongly typed. ISO(International Organization for Standardization) and ECMA(European Computer Manufacturers Association) standardized. Programming language A syntax that allow to give instructions to the computer C# features: New cutting edge language Extremely powerful Easy to learn Easy to read and understand Object-oriented 5 6 EEE-425 Programming Languages (2013) 1
Object-oriented programming is an extension of procedural programming, which in addition to variables and procedures contains: objects, classes, encapsulation, interfaces, polymorphism, and inheritance For example: An Automobile is a class whose objects have the following attributes: year, make, model, color, and current running status Your 1997 red Chevrolet is an instance of the class that is made up of all Automobiles Objects are object-oriented components Attributes of an object represent its characteristics A class is a category of objects or a type of object An instance refers to an object based on a class Methods of classes are used to change attributes and discover values of attributes The Automobile class may have the following methods: getgas(), accelerate(), applybreaks() 7 8 Sample C# Console Application program: using System; namespace ConsoleApplication1 class HelloCSharp static void Main(string[] args) Console.WriteLine("Hello, C#"); 9 10 Include the standard namespace "System" Define a class called "HelloCSharp" Class names should use HelloCsharp and start with a CAPITAL letter. using System; Define the Main() class HelloCSharp method the program entry point static void Main() Console.WriteLine("Hello, C#"); Print a text on the console by calling the method "WriteLine" of the class "Console" using System; class HelloCSharp static void Main() Console.WriteLine("Hello, C#"); The symbol should be under the corresponding. The symbol should be alone on a new line. The block after the symbol should be indented by a TAB. 11 12 EEE-425 Programming Languages (2013) 2
System using Such formatting makes the source code unreadable. class HelloCSharp static void Main( ) Console. WriteLine ("Hello, C#" ) ;Console. WriteLine ( "Hello again" ) ; ; using brings in namespaces namespace disambiguation of names like Internet hierarchical names and C++ naming class like in C++ or Java single inheritance up to object 13 static void Main() Defines the entry point for an assembly. Four different overloads taking string arguments and returning int s. Console.WriteLine(Write) Takes a formatted string: Composite Format Indexed elements: e.g., 0 can be used multiple times only evaluated once index [,alignment][:formatting] Environment for execution of.net programs Powerful library of classes Programming model Common execution engine for many programming languages C# Visual Basic.NET Managed C++... and many others 16 Building blocks of.net Framework C# C++ VB.NET J# F# JScript Perl Delphi ASP.NET Web Forms, MVC, AJAX Mobile Internet Toolkit Windows Forms WPF Silverlight WCF and WWF (Communication and Workflow Tier) FCL ADO.NET, LINQ and XML (Data Tier) Base Class Library (BCL) Common Language Runtime (CLR) CLR Operating System (OS) 18 EEE-425 Programming Languages (2013) 3
The common classes that are used in many programs System.Console.WriteLine XML, Networking, Filesystem, Crypto, containers Can inherit from many of these classes Many languages run on.net framework C#, C++, J#, Visual Basic even have Python (see IronPython) Common Language Runtime (CLR) Managed execution environment Executes.NET applications CLR Controls the execution process Automatic memory management (garbage collection) Programming languages integration Multiple versions support for assemblies Integrated type safety and security MSIL to Native Compilers (JIT) Base Class Library Support Thread Support Type Checker Security Engine Code Manager COM Marshaler Exception Manager Debug Engine Garbage Collector (GC) Class Loader 21 From MSDN CLR is actually an implementation by Microsoft of the CLI (Common Language Infrastructure). CLI is an open specification. CLR is really a platform specific implementation. CLR via C#, Jeffrey Richter 24 EEE-425 Programming Languages (2013) 4
Framework Class Library (FCL) Provides basic functionality to developers: Console applications WPF(Windows Presentation Framework) and Silverlight rich-media applications Windows Forms GUI applications Web applications (dynamic Web sites) Web services, communication and workflow Server & desktop applications Applications for mobile devices 25 26 Visual Studio Integrated Development Environment (IDE) Development tool that helps us to: Write code Design user interface Compile code Execute / test / debug applications Browse the help Manage project's files Single tool for: Writing code in many languages (C#, VB, ) Using different technologies (Web, WPF, ) For different platforms (.NET CF, Silverlight, ) Full integration of most development activities (coding, compiling, testing, debugging, deployment, version control,...) Very easy to use! 27 28 1. File New Project... 2. Choose C# console application 3. Choose project directory and name 29 30 EEE-425 Programming Languages (2013) 5
4. Visual Studio creates some source code for you Namespace not required Some imports are not required Class name should be changed The process of compiling includes: Syntactic checks Type safety checks Translation of the source code to lower level language (MSIL) Creating of executable files (assemblies) You can start compilation by Using Build->Build Solution/Project Pressing [F6] or [Shift+Ctrl+B] 31 32 The process of running application includes: Compiling (if project not compiled) Starting the application You can run application by: Using Debug->Start menu By pressing [F5] or [Ctrl+F5] 33 34 The process of debugging application includes: Spotting an error Finding the lines of code that cause the error Fixing the code Testing to check if the error is gone and no errors are introduced Iterative and continuous process Visual Studio has built-in debugger It provides: Breakpoints Ability to trace the code execution Ability to inspect variables at runtime 35 36 EEE-425 Programming Languages (2013) 6
A computer program is a set of instructions that you write to tell a computer what to do Live Demo-Examples Procedural Programming involves creating computer memory locations, called variables, and a set of operations, called procedures. In objectoriented programming, you envision program components as objects that are similar to concrete objects in the real world The C# language was developed as an objectoriented and component-oriented language 37 38 To write a C# program that produces a line of console output, you must pass a literal string as a parameter to the Console.Out.WriteLine() method You can define a C# class or variable by using any name or identifier that begins with an underscore or a letter, that contains only letters or digits, and that is not a C# reserved keyword To create a C# program, you can use the Microsoft Visual Studio environment or any text editor After you write and save a program, you must compile the source code Program comments are nonexecuting statements that add to document a program or to disable statements As an alternative to using the command line, you can compile and write your program within the Visual Studio IDE When you need to repeatedly use a class from the same namespace, you can shorten the statements you type by using a clause that indicates a namespace where the class can be found 39 40 Questions? 1. Create console application that prints your first and last name. 2. Write a program to print the numbers 1, 101 and 1001. 3. Create a console application that prints the current date and time. 4. Create a console application that calculates and prints the square of the number 1234. 5. Write a program that prints the first 10 members of the sequence: 2, -3, 4, -5, 6, -7,... 6. Write a program to read your age from the console and print how old you will be after 10 years. 41 42 EEE-425 Programming Languages (2013) 7