CSharp Tutorial - RxJS, Ggplot2, Python Data Persistence .

Transcription

C#

C#About the TutorialC# is a simple, modern, general-purpose, object-oriented programming languagedeveloped by Microsoft within its .NET initiative led by Anders Hejlsberg. This tutorialcovers basic C# programming and various advanced concepts related to C#programming language.AudienceThis tutorial has been prepared for the beginners to help them understand basics ofc# Programming.PrerequisitesC# programming is very much based on C and C programming languages, so ifyou have a basic understanding of C or C programming, then it will be fun to learnC#.Disclaimer & Copyright Copyright 2014 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of TutorialsPoint (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy,distribute or republish any contents or a part of contents of this e-book in any mannerwithout written consent of the publisher. We strive to update the contents of ourwebsite and tutorials as timely and as precisely as possible, however, the contentsmay contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guaranteeregarding the accuracy, timeliness or completeness of our website or its contentsincluding this tutorial. If you discover any errors on our website or in this tutorial,please notify us at contact@tutorialspoint.com.i

C#ContentsAbout the Tutorial . iAudience. iPrerequisites. iDisclaimer & Copyright . iContents . ii1.OVERVIEW. 1Strong Programming Features of C# . 12.ENVIRONMENT. 3The .Net Framework . 3Integrated Development Environment (IDE) for C#. 4Writing C# Programs on Linux or Mac OS. 43.PROGRAM STRUCTURE. 5Creating Hello World Program . 5Compiling and Executing the Program . 6C# Keywords . 104.BASIC SYNTAX. 12The using Keyword . 13The class Keyword . 14Comments in C#. 14Member Variables . 14Member Functions. 14Instantiating a Class . 14Identifiers . 15C# Keywords . 155.DATA TYPES. 17ii

C#Value Type . 17Reference Type . 18Object Type . 19Dynamic Type . 19String Type. 19Pointer Type . 206.TYPE CONVERSION . 21C# Type Conversion Methods . 227.VARIABLES. 24Defining Variables. 24Initializing Variables. 25Accepting Values from User . 26Lvalue and Rvalue Expressions in C#: . 268.CONSTANTS AND LITERALS. 28Integer Literals. 28Floating-point Literals . 29Character Constants. 29String Literals. 30Defining Constants. 319.OPERATORS. 33Arithmetic Operators. 33Relational Operators. 35Logical Operators. 38Bitwise Operators . 40Assignment Operators . 43Miscillaneous Operators . 46iii

C#Operator Precedence in C# . 4810.DECISION MAKING. 51if Statement. 52if.else Statement . 54The if.else if.else Statement. 56Nested if Statements . 58Switch Statement . 60The ? : Operator. 6511.LOOPS . 66While Loop . 67For Loop. 69Do.While Loop . 72Nested Loops . 75Loop Control Statements . 78Infinite Loop . 8312.ENCAPSULATION . 84Public Access Specifier . 84Private Access Specifier . 86Protected Access Specifier . 88Internal Access Specifier . 8813.METHODS. 91Defining Methods in C#. 91Calling Methods in C# . 92Recursive Method Call . 95Passing Parameters to a Method . 96Passing Parameters by Value . 97iv

C#Passing Parameters by Reference . 99Passing Parameters by Output. 10014.NULLABLES . 104The Null Coalescing Operator (?). 10515.ARRAYS. 107Declaring Arrays . 107Initializing an Array. 107Assigning Values to an Array. 108Accessing Array Elements . 108Using the foreach Loop . 110C# Arrays . 111Multidimensional Arrays . 112Two-Dimensional Arrays. 112Jagged Arrays. 115Passing Arrays as Function Arguments. 117Param Arrays . 118Array Class . 119Properties of the Array Class. 119Methods of the Array Class. 12016.STRINGS. 124Creating a String Object . 124Properties of the String Class . 126Methods of the String Class . 12617.STRUCTURES . 135Defining a Structure. 135Features of C# Structures. 137v

C#Class versus Structure . 13818.ENUMS . 141Declaring enum Variable . 14119.CLASSES. 143Defining a Class. 143Member Functions and Encapsulation . 145C# Constructors . 148C# Destructors . 151Static Members of a C# Class . 15220.INHERITANCE. 156Base and Derived Classes. 156Initializing Base Class . 158Multiple Inheritance in C#. 16021.POLYMORPHISM. 163Static Polymorphism. 163Dynamic Polymorphism . 16522.OPERATOR OVERLOADING . 170Implementing the Operator Overloading . 170Overloadable and Non-Overloadable Operators. 17323.INTERFACES. 181Declaring Interfaces . 18124.NAMESPACES . 184Defining a Namespace . 184The using Keyword. 185Nested Namespaces. 187vi

C#25.PREPROCESSOR DIRECTIVES . 190Preprocessor Directives in C#. 190The #define Preprocessor . 191Conditional Directives. 19226.REGULAR EXPRESSIONS . 194Constructs for Defining Regular Expressions . 194Character Escapes. 194Character Classes . 196Grouping Constructs . 198Quantifier . 199Backreference Constructs . 200Alternation Constructs. 201Substitution . 202Miscellaneous Constructs . 202The Regex Class . 20327.EXCEPTION HANDLING . 208Exception Classes in C# . 209Handling Exceptions . 210Creating User-Defined Exceptions. 212Throwing Objects. 21328.FILE I/O. 214C# I/O Classes . 214The FileStream Class . 215Advanced File Operations in C# . 217Reading from and Writing to Text Files . 218The StreamReader Class . 218The StreamWriter Class . 220vii

C#Reading from and Writing into Binary files . 222The BinaryWriter Class. 224Windows File System . 228The DirectoryInfo Class . 228The FileInfo Class . 23029.ATTRIBUTES. 234Specifying an Attribute . 234Predefined Attributes . 234AttributeUsage . 234Conditional . 235Obsolete . 237Creating Custom Attributes . 238Constructing the Custom Attribute . 239Applying the Custom Attribute . 24130.REFLECTION. 243Applications of Reflection . 243Viewing Metadata . 24331.PROPERTIES. 251Accessors . 251Abstract Properties. 25532.INDEXERS . 259Use of Indexers . 259Overloaded Indexers. 26233.DELEGATES. 266Declaring Delegates . 266Instantiating Delegates . 266viii

C#Multicasting of a Delegate . 268Using Delegates . 27034.EVENTS. 272Using Delegates with Events . 272Declaring Events . 27235.COLLECTIONS . 279ArrayList Class. 280Hashtable Class. 284SortedList Class . 288Stack Class . 292Queue Class . 295BitArray Class. 29736.GENERICS .

covers basic C# programming and various advanced concepts related to C# programming language. Audience This tutorial has been prepared for the beginners to help them understand basics of c# Programming. Prerequisites C# programming is very much based o