Download presentation
Presentation is loading. Please wait.
Published byKathryn Gilbert Modified over 9 years ago
1
By: Md Rezaul Huda Reza creativereza@yahoo.com
2
UNIVERSITY of South Asia 2 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 2 Brief introduction to Microsoft.NET Language versus platform neutrality Evolution of C# C# Variables. Decision and iteration statements
3
UNIVERSITY of South Asia 3 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 3 Quite difficult to remember Visual Basic was THE Microsoft development language C++ for “serious” programming Microsoft Foundation Classes for use with C++ for creating GUIs ASP (now referred to as “classic” ASP) Visual Studio IIS SQL Server 3
4
UNIVERSITY of South Asia 4 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 4 A programming infrastructure created by Microsoft for building, deploying, and running applications and services that use.NET technologies, such as desktop applications and Web Applications. Not an operating system Offers Multi-language Independence One application can be written in more than one language Includes over 2,500 reusable types (classes) Enables Creation of dynamic Web pages and Web services 4
5
UNIVERSITY of South Asia 5 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 5 A new(ish) language - C# We could call it “Java in disguise with a dash of C++” Common Intermediate Language (CIL) Common Language Runtime (CLR) language integration shared libraries potential platform independence Simplified deployment of Windows applications 5
6
UNIVERSITY of South Asia 6 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 6 The ability of different languages to interoperate (e.g. call each other’s code, share libraries) .NET really does support multiple languages Developed by Microsoft: C# C++/CLI VB.NET J# 6
7
UNIVERSITY of South Asia 7 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 7 7 Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Program written in any.NET supported language(s) (e.g. VB.NET, C#, J# etc) Common Intermediate Language (CIL) (.exe or.dll) Common Intermediate Language (CIL) (.exe or.dll) compile Common Language Runtime Executes code, garbage collects etc Common Language Runtime Executes code, garbage collects etc execute
8
UNIVERSITY of South Asia 8 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 8 Has the rapid graphical user interface (GUI) features of previous versions of Visual Basic Has the added power of C++ C#’s class library is called the.NET Framework Class Library = FCL object-oriented class libraries similar to Java’s APIs 8
9
UNIVERSITY of South Asia 9 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 9 Can be used to develop a number of applications Windows desktop applications Database access components Dynamic web pages Software components Web services Console based applications Mobile applications 9 In VAD (COMP1565)
10
UNIVERSITY of South Asia 10 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 10 On all machines at Greenwich Can be obtained via Student Intranet (see IT Support | Free MSDN Software). This method involves “burning” 3 CDs If you have broadband, you can download the Microsoft Visual C# Express Edition from: msdn.microsoft.com/vstudio/express/visualcsharp / 10
11
UNIVERSITY of South Asia 11 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 11 Development tool that contains a rich set of productivity and debugging features Supports managed and unmanaged applications Supports C#, C++, VB.NET, … Many useful tools and wizards Windows Forms Designer ASP.NET Web Forms Designer Web Services support SQL Server integration with ADO.NET and XML not part of the.NET Framework
12
UNIVERSITY of South Asia 12 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 12 A project is a group of files which together make up a program being developed When finished, a program can be delivered in some compact form (eg as a.exe file) There are different types of applications: Windows Forms (GUI) applications Web-related applications Console applications – no graphics, others 12
13
UNIVERSITY of South Asia 13 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 13 For how to get here - see the Lab “Hello World”
15
UNIVERSITY of South Asia 15 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 15 A command that performs an action They are found inside methods A method is a named sequence of statements Must follow a set of rules = syntax Example of syntax rules: You must terminate all statements with “;” The specification of what statements do is known as semantics The trick to good programming is learning its syntax and semantics Extra reading: Syntax vs semantics: what's the difference? Syntax vs semantics: what's the difference? 15
16
UNIVERSITY of South Asia 16 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 16 Identifiers are names of various program elements in the code that uniquely identify an element. names of things like variables or fields. specified by the programmer. Keywords are reserved words in the C# language. they can't be used as identifiers. Examples : class, public, or void 16
17
UNIVERSITY of South Asia 17 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 17
18
UNIVERSITY of South Asia 18 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 18 A Variable represents a storage location that holds a value. A “box” holding temporary information Has a unique name The value of a variable can be changed through assignment or through use of the ++ and -- operators. A variable must be definitely assigned before its value can be obtained. 18
19
UNIVERSITY of South Asia 19 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 19 Combination of alphabetic characters (a–z, and A–Z), numeric digits (0–9), and the underscore ( _ ) The name must start with a letter or an underscore No embedded spaces concatenate (append) words together Keywords cannot be used Be descriptive with meaningful names Eg footballTeamScore Convention: start with lower case 19
20
UNIVERSITY of South Asia 20 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 20 In order to use a variable in your program, the compiler must be aware of it Once the compiler knows about a variable, it would reserve an amount of memory space for that variable How much memory space should the compiler reserve? – it depends what data type of variable is of. 20
21
UNIVERSITY of South Asia 21 Md Rezaul Huda Reza creativereza@yahoo.com A data type is an amount of space needed to store the information related to a particular variable. C# is a type-safe language, the C# compiler guarantees that values stored in variables are always of the appropriate type 2 types value Reference 21
22
UNIVERSITY of South Asia 22 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 22
23
UNIVERSITY of South Asia 23 Md Rezaul Huda Reza creativereza@yahoo.com Value types byte: Holds 8-bit unsigned integers. holds positive numbers. short: Holds 16-bit signed integers. int: Holds 32-bit signed integers long: Holds 64-bit signed integers. char: Holds 16-bit Unicode characters. float: Holds a 32-bit signed floating-point value. mandatory F suffix double: 64-bit signed floating-point value. optional D suffix decimal: 128-bit signed floating-point value. Good for financial calculations. mandatory M suffix bool: Boolean type. I can have the value: true or false. Reference types – more in future lectures String A string is an empty space, a character, a word, or a group of words 23
24
UNIVERSITY of South Asia 24 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 24 Syntax: DataType VariableName; Specify the type at the beginning. Give it a name Examples: int studentCount; // number of students in the class int ageOfStudent double priceOfTicket; // cost of a movie ticket bool raining; string s; // string declaration string helloString ;//declaration 24
25
UNIVERSITY of South Asia 25 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 25 assign a value to a variable with the operator “= “ Syntax variable = expression; An expression can be Another variable Compatible literal value Mathematical equation Call to a method that returns a compatible value Combination of one or more items in this list Question: Where should a statement be placed inside the code? 25
26
UNIVERSITY of South Asia 26 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 26 Local variables can be given an inferred "type" of var instead of an explicit type. var VariableName = [ a value]; The compiler specifies its data type. you must initialize the variable (provide a value for it). var s = "Hello world"; // s is compiled as a string var i = 5; // i is compiled as an int Question: Why do you think you must initialize the variable if you declare is as a ‘var’? 26
27
UNIVERSITY of South Asia 27 Md Rezaul Huda Reza creativereza@yahoo.com int x; //this is a comment (in green) long LandArea; //declaration x = -3; // assignment double y = 3.0 ; //declaration and assignment LandArea = 5638648L; // note the L float Distance = 248.38F; // note the F decimal HourlySalary; HourlySalary = 24.25M; string CustomerName; CustomerName = “G Almas"; float totalAmount = 23.57F; var Number1 = 62834.9023F; var Number2 = 62834.9023; var Number3 = 62834.9023M; Question: What types are Number1 Number2 Number3 ?? 27
28
UNIVERSITY of South Asia 28 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 28 int myNumber, count, minIntValue, newValue; char firstInitial; string aSaying, fileLocation; bool isFinished = false; // initialised at declaration count = 2; myNumber = (45 - count) /2; newValue =25; minIntValue = -2147483648; firstInitial = ‘B’; aSaying = “First day of the rest of your life!\n "; fileLocation = @”C:\CSharpProjects\Chapter2”; /*@ placed before a string literal signal that the characters inside the double quotation marks should be interpreted verbatim (literally). This is useful for file paths*/ count = newValue + myNumber; 28 Q: What is the value of count?
29
UNIVERSITY of South Asia 29 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 29 Arithmetic: +, -,*, /,% String concatenation + Assignment =, +=, -=, *=, /= Unary operator num++; // num = num + 1; - postfix increment ++num; // num = num + 1; - prefix increment count--; // count = count – 1; - postfix decrement --count; // count = count – 1; - prefix decrement 29
30
UNIVERSITY of South Asia 30 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 30 NOT a value type Reference type Strings contain a reference to a memory location - (much) more in a future lecture Do not contain the actual data 30 Hello world string helloString; reference 1 Memory location helloString = “Hello World”; helloString = “Good bye!”; Good bye! helloString Memory location reference 2
31
UNIVERSITY of South Asia 31 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 31 Immutable You can never actually change the contents of a string. in order to change what a string variable references, a new string object must be created It can contain nulls: string nullString = null; 31 string s1 = "hello"; string s2 = s1; // s2 does at this point reference the same string //object as s1. Hello s1 reference 1 s2 reference 2 s2 = "goodbye"; a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello" s2 = "goodbye"; a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello" goodbye
32
UNIVERSITY of South Asia 32 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 32 Some characters that cannot be easily included. E.g. To allow a quotation mark to be included in a string literal, a special code or escape character sequence is inserted. Example: string valid = "This is a \"string\".";
33
UNIVERSITY of South Asia 33 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 33 Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples: conversions from smaller to larger integral types, Explicit conversions (casts): require a cast operator. The source and destination variables are compatible (i e numbers), but there is a risk of data loss because the type of the destination variable is a smaller size than the source variable. 33
34
UNIVERSITY of South Asia 34 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 34 FromTo byte short, int, long, float, double, or decimal shortint, long, float, double, or decimal intlong, float, double, or decimal longfloat, double, or decimal charInt, long, float, double, or decimal floatdouble 34 int num = 21474836; long bigNum = num; // num long can hold any value an int can hold Example
35
UNIVERSITY of South Asia 35 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 35 Write the type you are trying to cast into within “()” before the variable name 35 // Cast double to int double x = 1234.7; int a; a = (int)x; // a is 1234 Q: What is the value of a? int i = 20; short j = i; Q: Is this correct ? Cannot implicitly convert type 'short' to 'int‘’ !
36
UNIVERSITY of South Asia 36 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 36 All numeric types have a Parse( ) method that can take a string as a parameter double.Parse(“string number”) int.Parse(“string number”) char.Parse(“string number”) bool.Parse(“string number”) Ex 36 // Convert string to number. string text = "500"; int num = int.Parse(text); int i; i = “20"; Q: Is this correct ? Cannot implicitly convert type 'string' to 'int‘’ !
37
UNIVERSITY of South Asia 37 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 37 All numeric types have a ToString( ) method Convert anything into string representation Ex Note : Changing from and to a string is very important The iext property of a text box contains only strings (same with labels, etc ) 37 // Convert a number to a string. double value = 123.456; string str = value.ToString()
38
UNIVERSITY of South Asia 38 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 38 Plus (+) with string Identifiers concatenates operand2 onto end of operand1 string result; string fullName; string firstName = “Rochelle”; string lastName = “Howard”; fullName = firstName + “ “ + lastName; Automatic casting to a string: if you “add” a string to any other value type, the result is a string” int age = 20; string ageString = “ 21” Q: what will (age + ageString) be? 38
39
UNIVERSITY of South Asia 39 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 39 The Convert class (System.Convert) can be used to convert from a type to another Convert.ToDouble( ) Convert.ToDecimal( ) Convert.ToInt32( ) Convert.ToBoolean( ) Convert.ToChar( ) EX 39 // Convert a type to another. string stringValue = “123434”; int newValue = Convert.ToInt32(stringValue);
40
UNIVERSITY of South Asia 40 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 40 Add the keyword const to a declaration Value cannot to be changed Standard naming convention Syntax const type identifier = expression; const double TAX_RATE = 0.0675; const int SPEED = 70; const char HIGHEST_GRADE = ‘A’; 40
41
UNIVERSITY of South Asia 41 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 41
42
UNIVERSITY of South Asia 42 Md Rezaul Huda Reza creativereza@yahoo.com UNIVERSITY of South Asia 42
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.