CH02 - Basic Syntax Lecture Notes PDF

Document Details

ImpressedSynergy3690

Uploaded by ImpressedSynergy3690

Mansoura National University

Dr-Mohamed Handosa

Tags

C# programming C# syntax C# namespaces programming languages

Summary

These lecture notes provide an overview of C# basic syntax focusing on namespaces, methods, commenting, and other foundational concepts.

Full Transcript

CH02- Basic Syntax Based On Dr-Mohamed Handosa’s Lecture Notes 2.1 Program Structure A C# program consists of a set of one or more namespaces, each namespace contains a set of one or more classes. A namespace may contain other namespaces as well. Each class contains a set of one or more...

CH02- Basic Syntax Based On Dr-Mohamed Handosa’s Lecture Notes 2.1 Program Structure A C# program consists of a set of one or more namespaces, each namespace contains a set of one or more classes. A namespace may contain other namespaces as well. Each class contains a set of one or more methods. Each method, in turn, may contain a set of one or more statements and each statement represents a command to execute. The following figure shows the code structure of a C# program. Namespace Namespaces are used in C# to organize Code and group related functionalities together ‫طريقة لتقسيم الكود لمجموعات برمجية وتكون كل مجموعة مخصصة لوظائف معينة‬ https://www.programiz.com/csharp-programming/namespaces Example ‫مثال توضيحي‬- ‫لالطالع فقط‬ Sort the following shapes into 3 groups (Squares, Circles, triangles) Examples for Common Namespaces ‫لالطالع فقط‬ Namespace Usage (Classes) System basic functionalities like mathematical operations, data conversions System.IO file I/O operations System.Net network operations (send and receive data) System.Collections different lists of data System.Drawing Simple Graphics and Image operations System.Security Basic Security Classes Note: you can also create your own Namespaces in c# https://www.c-sharpcorner.com/article/working-with-namespaces-in-C-Sharp/ Basic Structure of C# Program The program made up of one or more namespaces. Each namespace contains one or more classes. Each class contains one or more methods. Each method contains one or more statements. Example The following example shows a simple C# program with a single statement that prints the text Hello World! to the screen: Example line 1 defines a namespace named HelloWorld line 2: two curly braces (an opening curly brace { and a corresponding closing curly brace } in line 10). Line 3 in the above code defines a class named Program. Line 4: opening curly brace { in line 4 and the corresponding closing curly brace } in line 9) Lines 5-8 define a method named Main. Any code lines within the Main method (i.e. within its curly braces) will be executed when the program runs. Write() method Note that "Hello World!" is enclosed within double quotation marks. In C#, string literals must be enclosed within double quotation marks. The Write() method itself is contained within a library class named Console, which in turn is contained within a namespace named System. two braces () containing the input data that we want to pass to the Write method, which is the string literal "Hello World!" in our example In C#, every code statement must end with a semicolon ;. Note that line 7 in our example ends with a semicolon ;. C# is case-sensitive C# is case-sensitive. If you change line 7 in the above example by modifying the name of the method from Write to write as follows The C# compiler will not compile the code and will show the following error message: This is because the words Write and write are considered different words in C#. The C# compiler will provide an error message if there is something wrong with your code. 2.2 The using Directive The using directive allows you to use everything that is defined in a namespace without specifying the fully qualified namespace. In its basic form, the using directive imports everything from a single namespace, as shown in the following example: 2.3 Write and WriteLine In C#, both Write and WriteLine are methods in the Console class that can be used to output something to the console. The only difference between the Write and WriteLine is that Console.Write is used to print data without printing the new line character, while Console.WriteLine is used to print data along with printing the new line character afterward. This would set a new line for the next output. line 9 uses the Write method to print 1 to the screen. The Write method will not print a new line Example character after printing 1. Therefore, the next output will be printed to the same line. Line 10 uses the WriteLine method to print 2 to the screen. The WriteLine method will print a new line character after printing 2. Therefore, the next output will be printed on a new line. Similarly, line 11 uses the WriteLine method to print 3 followed by a new line character. Finally, line 12 uses the Write method to print 4 to the screen. 2.4 Comments 1. Comments can be used to explain C# code, and to make it more readable. 2. It can also be used to prevent the execution of a portion of code when testing alternative code. There are 2 types of comments: 1. Single line comment // 2. Multiple Line comments 2.4.1 Single-Line Comments Single-line comments start with two forward slashes //. Any text between // and the end of the line is ignored by C#. 2.4.2 Multi-Line Comments Multi-line comments start with. Any text between will be ignored by C#. An example of a multi-line comment to explain the code: 2.5 Literals Literals refer to fixed values that the program may not alter during its execution. They can be of any of the basic data types like an integer literal, a floating-point literal, a character literal, or a string literal. In the above example, line 7 prints the integer literal 5 to the screen. Line 8 prints the floating-point literal 5.2 to the screen. A floatingpoint literal has an integer part, a decimal point, and a fractional part. It can also have an exponent part as in line 9, where 5.2E-3 means 5.2 × 10−3 or 0.0052. 2.6 Variables Variables are containers for storing data values. In C#, there are different types of variables (defined with different keywords). The following table shows some keywords that can be used to define variables. Data Type Sizes (in bytes) ‫لالطالع فقط‬ Type Description byte 8-bit unsigned integer sbyte 8-bit signed integer short 16-bit signed integer ushort 16-bit unsigned integer int 32-bit signed integer uint 32-bit unsigned integer long 64-bit signed integer ulong 64-bit unsigned integer float 32-bit Single-precision floating point type double 64-bit double-precision floating point type decimal 128-bit decimal type for financial and monetary calculations char 16-bit single Unicode character bool 8-bit logical true/false value https://www.tutorialsteacher.com/csharp/csharp-data-types ‫لالطالع فقط‬ Effect of Data Size on Data Range More size of bits/bytes→ More value Range to store. ‫امكانية تخزين قيم أكثر‬ For example: – byte (unsigned)has 8 bits, each bit has 2 options (0 or 1) , so we have 28 = 256 positive values (starting from 0 to 255) – sbyte has also 8 bit, and 256 values, but half of them are negative (starting from -128 to 127) Effect of Type Size ‫لالطالع فقط‬ 2.6.1 Defining Variables To define a variable, you must specify a type and a name. The general syntax takes the following form: Example In the above example, line 7 defines a variable named x of type int. Line 8 uses the assignment operator = to store the value 5 into the variable x. Finally, line 9 uses the WriteLine method to print the variable x to the screen. This will print the value of the variable to the screen (i.e. 5 ) Initialization You can initialize a variable when defining it. That is you can provide an initial value to the variable. In the previous example, lines 7 and 8 can be merged into a single line of code as follows: In the above code, line 1 defines a variable named x of type int and assigns it an initial value of 5. Note that if you assign a new value to an existing variable, it will overwrite the previous value. For example: Variable Variables represent memory locations that hold values. These values may be changed in the program. Variable name is an identifier ‫اسم للتعريف‬. https://www.tutlane.com/tutorial/csharp/csharp-value-type-and-reference-type- with-examples Example https://www.guru99.com/c-sharp-variables- operator.html Example The following example creates several variables of different types and prints their values to the screen: 2.6.2 Defining Multiple Variables To define more than one variable of the same type, use a comma separated list as in the following example: int x, y, z; In the above code, line 1 defines three variables of type int. The variables are named x , y , and z. You can give some of your variables initial values when defining them as in the following example: int x = 5, y, z = 50; 2.7 Constants You can use the const keyword if you don’t want others (or yourself) to overwrite the existing value of a given variable. The general syntax to define a constant is: For example, the following code will result in an error: 2.7 Constants-2 The const keyword is useful when you want a variable to always store the same value. An example that is often referred to as a constant, is PI (i.e. 22 ÷ 7 or 3.14159). Note that you cannot declare a constant variable without assigning it a value. If you do, the C# compiler will produce the following error message: 2.8 Identifiers All C# variables must be identified with unique names. These unique names are called identifiers. Identifiers can be short names (e.g., x and y ) or more descriptive names (e.g., age , totalPrice ). It is recommended to use descriptive names in order to create understandable and maintainable code. For example: general rules for creating variable names 1. A name cannot contain a white-space. 2. A name must begin with a letter or an underscore _. 3. A name can contain only letters, digits and the underscore. 4. Keywords (e.g. int, double, string) cannot be used as names. 5. Names are case-sensitive (e.g. size and Size are different). invalid variable names Examples ‫لالطالع فقط‬ Number Suffix ‫رموز لتوضيح نوع الرقم‬ http://www.blackwasp.co.uk/CSharpNumericLiterals.aspx ‫لالطالع فقط‬ examples of various types of integer literals Note: – In number system, each number can be represented as (binary, octal, decimal, hexadecimal) – For example: 7 in decimal = 111 in binary – Octal is not supported in current c# version Thanks

Use Quizgecko on...
Browser
Browser