Download Algorithms - Programming Using C Sharp - Lecture Slides and more Slides Advanced Algorithms in PDF only on Docsity!
Summary of the Last Lecture
- Introduction to the course
- Algorithms
- Programming Languages
- C#, .NET Platform and CLR
- Visual Studio 2008 installation
Today
- C# program’s structure with Hello World program
- How to use VS 2008
- What is an Assembly?
- Basics: literals, identifiers, variables, keywords, comments
- Console input / output
- Assignment, arithmetic operations and precedence
- Memory concepts
- Data Types (value vs reference types) and type casting
“Hello World” Program
- Let’s develop our very first application using Visual Studio 2008
- Create a project
- Build, compile, run and debug
- Useful windows and customizing its locations
- Solution Explorer
- Toolbox
- Properties
- Error List
- Debugging windows
- Intellisense
- Menu and the toolbar
- Enable Line numbers: Tools\Options\Text Editor\All Languages\Line numbers checkbox.
- Help and MSDN
C# Program’s Structure
- C# is 100% object-oriented:
- Everything is a class the program itself has to be a class
class Program
{ // classes start with a {
} // classes end with a }
- Classes are grouped into namespaces
- You can use existing namespaces by using directive
using System;
- You can create your own namespace
namespace World
{ // namespaces start with a {
// class definition goes here
} // namespaces end with a }
C# Program’s Structure
- Programmers use blank lines and space characters to
make applications easier to read.
- Together, blank lines, space characters and tab characters
are known as whitespace. Whitespace is ignored by the compiler.
- Certain indentation makes the code easier to read. You
can let the IDE format your code by selecting
Edit > Advanced > Format Document.
- Set tab size: Tools\Options\Text Editor\C#\Tabs\Tab size.
C# Program’s Structure
- .NET class library has thousands of methods
Console.WriteLine("Hello World");
using System;
System .Console.WriteLine("Hello World");
namespace class method string
- using directive tells the compiler where to look for a .NET class used in this application
- The Console.WriteLine method displays a line of text in the console window.
- The string in parentheses is the argument to the Console.WriteLine method.
- Method Console.WriteLine performs its task by displaying its argument in the console window.
.NET Framework Class Library (FCL)
Set of classes, interfaces, and value types that exposes
some functionality for re-use.
The foundation on which .NET Framework
applications, components, and controls are built.
Thousands of types are organized into namespaces Example: Object base type and types for integers, characters are in the System namespace
Uses a dot syntax naming scheme that connotes a
hierarchy. Groups related types into namespaces so they can be searched and referenced more easily. The first part of the full name — up to the rightmost dot — is the namespace name. The last part of the name is the type name. Example: System.Collections.ArrayList namespace type
C# command-line compiler: csc.exe
What is an Assembly?
- When we compiled HelloWorld.cs using C# compiler, we
created an assembly called HelloWorld.exe
- An assembly is a .NET unit of modules put together that the
runtime (CLR) can run
- An assembly could be:
- EXE (/target:exe or /target:winexe)
- DLL (/target:library)
- Module (/target:module)
- Visual Studio generates either an EXE or a DLL.
- An assembly could be a single file or contain multiple files
- Multiple files could be .NET modules or resource files (gif/jpg)
- csc /addmodule:
.NET Tool: ildasm.exe
• Let’s analyze “Hello World” program with
ildasm.exe
Syntax Errors
- The syntax of a programming language specifies the rules for creating a proper application in that language.
- A syntax error occurs when the compiler encounters code that violates C#’s language rules.
- Example : Forgetting to include a using directive for a namespace that contains a class used in your application results in a syntax error, containing a message such as: “The name 'Console' does not exist in the current context.”
- When this occurs, check that you provided the proper using directives and that the names in the using directives are spelled correctly, including proper use of Uppercase and Lowercase letters.
- To find the namespace:
Literals
Console.WriteLine("Hello World");
string literal
Fixed (constant) values
They cannot be changed during program’s execution
They can be output by Console.WriteLine
Different format for different types:
String literals Sequences of characters Within double quotes (quotes are not part of the string) Almost any character is ok (letters, digits, symbols) " 10 > 22 $&*%? " Numeric literals Integer 3 454 -43 + Real 3.1415 +45.44 -54.6 1.2334e 1.2334e3 is 1.2334 times 10 to the power 3 (scientific notation) Docsity.com
Identifiers
Names of programmer defined elements in a program
Names of classes, methods, variables, etc. namespace World { public class HelloWorld {
Syntax (rules):
- Sequence of letters (a .. z, A ..Z), digits (0 ..9) underscore _
- Cannot start with a digit or underscore
- Case-sensitive ( n umber1 and N umber1 are not the same)
Examples:
Program1 valid number_1 valid mySum valid 1number not valid
- Pick meaningful names to improve readability and understandability of your program (be consistent)
Console Output
using System;
namespace World { public class HelloWorld { static void Main(string[] args) {
Console.WriteLine("Welcome\nto\nC#\nProgramming"); } } }
- A single statement can display multiple lines by using newline characters.
- Like space characters and tab characters, newline characters are whitespace characters.
- The below application outputs 4 lines of text, using newline characters to indicate when to begin each new line.
Console Output
- The backslash () is called an escape character , and is used as
the first character in an escape sequence.
- The escape sequence \n represents the newline character.
20
Common Escape Sequences Description \n Newline. Positions the screen cursor at the beginning of the next line. \t Horizontal tab. Moves the screen cursor to the next tab stop. \r Carriage return. Positions the screen cursor at the beginning of the current line—does not advance the cursor to the next line. \ Backslash. Used to place a backslash character in a string. \“ Double quote. Console.Write(^ ""in quotes"" ); displays "in quotes"