C# - Tutorialspoint

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. 108Acces

compiler, which is again a part of the .NET Framework. Visual C# Express and Visual Web Developer Express edition are trimmed down versions of Visual Studio and has the same appearance. They retain most features of Visual Studio. In this tutorial, we have used Visual C# 2010 Express. You can download it fromMicros oft Vis ual St di . It gets installed automatically on your