1 / 202

Overview of C#

Overview of C#. Overview of C#. C# can be used to develop two categories of programs, namely, Executable application programs and Component libraries. C# Programs. Executable Programs. Library Programs. Application Program. CLR. Output. CLR. Output. Continued….

riona
Download Presentation

Overview of 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. Overview of C#

  2. Overview of C# • C# can be used to develop two categories of programs, namely, • Executable application programs and • Component libraries C# Programs Executable Programs Library Programs Application Program CLR Output CLR Output

  3. Continued… • Executable programs are written to carry out certain tasks and require the method Main in one of the classes. • In contrast, component libraries don’t require a Main declaration because they are not standalone applications.

  4. Data Types

  5. Tokens • There are following five types of tokens: • Keywords • Identifiers • Literals • Operators • punctuators

  6. Keywords • Keywords are an essential part of a language definition. They implement specific features of the language. They are reserved, and cant be used as identifiers except when they are prefaced by the @ character. • Example : • abstract • as • base • checked • const • do • double • ref

  7. Identifiers • Identifiers are programmer-designed tokens. They are used for naming classes, methods, variables, labels, namespaces, interfaces, etc. • C# identifiers enforce the following rules: • They can have alphabets, digits and underscore characters. • They must not begin with a digit. • Upper case letters and lower case letters are distinct. • Keywords in standalone mode cant be used as identifiers.

  8. Literals • Literals are the ways in which the values that are stored in variables are represented. C# literals Numeric Literals Boolean Literals Character Literals Integer Real Single Character String Literals

  9. Continued… • Examples : • Integer literals : 123 -321 0 654321 0X2 • Real literals : 0.0083 -0.75 435.36 0.65e4 • Boolean literals : true false • Single character literals : ‘X’ ‘5’ ‘;’ • String literals : “Hello” “2001” “5+3”

  10. Continued… • Backslash Character Literals

  11. Operators • Operators are symbols used in expressions to describe operations involving one or more operands. • Examples : • + • - • / • * • %

  12. Punctuators • Punctuators are symbols used for grouping and separating code. They define the shape and function of a program. • Punctuators (also called as ‘separators’) in C# include: • Parentheses ( ) • Braces { } • Brackets [ ] • Semicolon ; • Colon : • Comma , • Period .

  13. C# Data Types C# Data Types Value Types Pointers Reference Types Predefined types User defined types Predefined types User defined types • Integers • Real no • Booleans • Characters • Enumerations • Structures • Classes • Arrays • Delegates • Interfaces • Objects • Strings

  14. Value Types Simple Types Character Numeric Boolean Decimal Floating Point Integral Unsigned Signed

  15. Integral Type Signed Integers Unsigned Integers sbyte long byte ulong short int ushort uint

  16. Size and Range of Signed Integer Types

  17. Size and Range of Unsigned Integer Types

  18. Floating-Point Types Floating-Point double float

  19. Others • Decimal Type : A high precision, 128-bit data type. Can store values in the range 1.0 X 10-28 to 7.9 X 1028 with 28 significant digits. To specify a number to be decimal type, we must append the character M (or m) to the value, like 123.45M, otherwise the value will be treated as double. • Character Type : The char type assumes a size of two bytes but, in fact it can hold only a single character. • Boolean Type : Used to test a particular condition during the execution of a program. It can take the two values : true or false (in contrast to C and C++, we cant use zero for false and non-zero for true).

  20. Accessibility

  21. Access Modifiers • Access modifiers are keywords used to specify the declared accessibility of a member or a type. This section introduces the four access modifiers: • public • protected • internal • private • The following five accessibility levels can be specified using the access modifiers: • public: Access is not restricted. • protected: Access is limited to the containing class or types derived from the containing class. • Internal: Access is limited to the current assembly. • protected internal: Access is limited to the current assembly or types derived from the containing class. • private: Access is limited to the containing type.

  22. Variables & Constants

  23. Variables - Declaration • Syntax : type var1, var2, …., varN; • Examples : • int count; • float x, y; • double pi; • ulong n;

  24. Variables - Initialization • Syntax : variableName = value; • Variables must have Value before being used. • Examples : • i = 0; • y = ‘x’; • String assignment expression : x = y = z; • At the time of declaration : int val = 100; char y = ‘x’; bool test = true; ulong ul = 123UL; decimal d = 1.23M;

  25. Default Values • Following are automatically initialized to their default values: • Static variables • Instance variables • Array elements

  26. Constants • The variables whose value don’t change during the execution of a program can be made unmodifiable by using the const keyword while initializing them. • Examples : • const int rows = 10; • const int m; //Illegal as constants must be m = 10; //Declared and initialized simultaneously • Non-constant values cant be used : int m = 10; //Non-constant value const int n = m * 5; // Error

  27. Types Of Variables • C# defines several categories of variables as : • Static variables • Instance variables • Array elements • Value parameters • Reference parameters • Output parameters • Local variables

  28. Continued… • Example : Class ABC { static int m; int n; void fun (int x, ref int y,out int z, int [] a) { int j = 10; ………… } } This code contains the following variables: • Static variable m • Instance variable n • Array elements a[0] • Value parameter x • Reference parameter y • Output parameter z • Local variable j

  29. C# Statements • A statement is an executable combination of tokens ending with a semicolon. • C# implements several types of statements including :

  30. Sample Program

  31. A Simple C# Program Class SampleOne { public static void Main() { System.Console.WriteLine (“Hello”) } }

  32. Class Declaration • The first line : class sampleOne declares a class, which is an object-oriented construct. C# is a true object-oriented language and therefore, ‘everything’ must be placed inside a class. class a keyword and declares that a new class definition follows. SampleOne is a C# identifier that specifies the name of the class to be defined.

  33. The Braces • C# is a block structured language, meaning code blocks are always enclosed by braces{ and }. • Therefore every C# program begins with an opening brace ‘{’ and ends with a corresponding closing brace ‘}’. • Note that there is no semicolon after the closing brace.

  34. The Main Method • The third line : public static void Main() defines a method named Main. Every C# program must include the Main() method in one of the classes. This is the ‘starting point’ for executing the program. A C# application can have any number of classes but ‘only one’ class can have the Main() method to initiate the execution.

  35. Continued… • The meaning and purpose of other keywords are: • public : The keyword public is an access modifier that tells the C# compiler that the Main method is accessible by anyone. • static : The keyword static declares that the Main method is a global one and can be called without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execution before any objects are created. • void : The keyword void is a type modifier that states that the Main method does not return any value (but simply prints some text to the screen).

  36. The Output Line • The only executable statement in the program is System.Console.WriteLine(“Hello”). The WriteLine Method is a static method of the Console class, which is located in the namespace System. This line prints the string Hello to the screen. The method WriteLine always appends a new-line character to the end of the string. This means any subsequent output will start on a new line. • Note the semicolon at the end of the statement. Every C# statement must end with a semicolon.

  37. Executing The Program

  38. Executing The Program • After creating the program (source code), save it with .cs file extension in a folder of your system. Example sampleone.cs • For compiling the program, go to the folder where you have stored the file sampleone.cs and then type the following command : csc sampleone.cs • The C# compiler (CSC) compile your code and create an executable file (IL code) by name : sampleone.exe in the same directory (if the code is error-free). If there are any errors the compiler will produce appropriate error message. Errors should be corrected and the program should be compiled again.

  39. Continued… • For executing the program, simply type the name of the executable file at the command prompt. That is : sampleone • Note that no file extension is used. This will display the output : Hello on the screen. (.cs file) C# Code C# Compilation IL Code (.exe file) JIT Compilation Native Machine Code Execution Output

  40. Namespace System

  41. Namespace • Consider the output statement of the previous sample again : System.Console.WriteLine(); • In this, we noted that System is the namespace (scope) in which the Console class is located. A class in a namespace can be accessed using the dot operator. Namespaces are always the way C# segregates the .Net library classes into reasonable groupings. • C# supports a feature known as using directive that can be used to import the namespace System into the program. Once a namespace is imported, we can use the elements of that namespace without using the namespace as prefix.

  42. A Program Using Namespace & Comments /* This program uses namespace and comment lines */ Using System; //System is a namespace Class SampleTwo { //Main method begins public static void Main() { Console.WriteLine (“Hello”) } //Main method ends }

  43. Continued… • Note that the first statemnet in the program is : using System; This tells the compiler to look in the System library for unresolved class names. Note that we have not used the System prefix to the Console class in the output line • When the compiler parses the Console.writeLine method, it will understand that the method is undefined. However, it will then search through the namespaces specified in using directives and, upon finding the method in the System namespace, will compile the code without any complaint.

  44. Adding Comments • C# permits two types of comments, namely, • Single-line comments : using System; //single line comment • Multiline comments : /* This is an example of Multiline comments in C# language */

  45. Main() Variation

  46. Main Returning A Value //Main returning a value Using System; Class SampleThree { public static int Main() { Console.WriteLine (“Hello”); return 0; //Return statement } } • The value returned serves as the program’s termination status code. The purpose of this code is to allow communication of success or failure to the execution environment.

  47. Main With A Class Class TestClass //class definition { public void fun( ) { System.console.WriteLine (“Hello”); } } Class SampleSeven { public static void Main() { TestClass test = new TestClass( ); //creating test object test.fun( ); //calling fun( ) function } }

  48. Using Aliases For Classes • Syntax : using alias-name = class-name; • Example : using A = System.Console; //A is alias for System.Console Class SampleFour { public static void mMain() { A.WriteLine (“Hello”); } }

  49. Passing String Objects To WriteLine Method Using System; Class SampleFive { public static void Main() { string name = “C sharp”; Console.WriteLine (name); } }

  50. Command Line Arguments

More Related