C# basics

8,329 views

Published on

  • Be the first to comment

C# basics

  1. 1. Basics of C# 2008 .NET 3.0/3.5
  2. 2. Session Objectives <ul><li>What is C#? </li></ul><ul><li>Understand the basic structure of a C# program. </li></ul><ul><li>Obtain a basic familiarization of what a &quot;Namespace&quot; is. </li></ul><ul><li>Obtain a basic understanding of what a Class is. </li></ul><ul><li>Learn what a Main method does. </li></ul><ul><li>Learn how to obtain command-line input. </li></ul><ul><li>Learn about console input/output (I/O). </li></ul>
  3. 3. Basic Structure of C# Program <ul><li>// Namespace Declaration using System; // Program start class class WelcomeCSS {     // Main begins program execution.     static void Main()     {         // Write to console         Console.WriteLine(&quot;Welcome to the C# !&quot;);      } } </li></ul>
  4. 4. Getting Command-Line Input <ul><li>What is meant by command line arguments? </li></ul><ul><li>How to take command line input? </li></ul><ul><li>How to convert command line input to required type? </li></ul><ul><li>Structure of Main() method in C# </li></ul>
  5. 5. Interactive via Command Line <ul><li>Taking input from user interactively </li></ul><ul><li>Usage of Console.ReadLine() </li></ul><ul><li>Conversion Functions </li></ul>
  6. 6. Object Oriented Programming Fundamentals <ul><li>Class </li></ul><ul><li>Object </li></ul><ul><li>Method </li></ul><ul><li>Attribute </li></ul><ul><li>Abstraction </li></ul><ul><li>Encapsulation </li></ul><ul><li>Polymorphism </li></ul><ul><li>Inheritance </li></ul><ul><li>Differences b/w object based and OO languages </li></ul>
  7. 7. Console Application in VS 2008 <ul><li>What is Solution (.sln)? </li></ul><ul><li>IntelliSense </li></ul><ul><li>Automatic Syntax checking </li></ul><ul><li>Properties Window </li></ul><ul><li>Solution Explorer </li></ul><ul><li>Server Explorer </li></ul>
  8. 8. Basics <ul><li>Variables </li></ul><ul><li>Initialization of Variables </li></ul><ul><li>Scope of Variables </li></ul><ul><li>Scope Clashes for Local Variables </li></ul><ul><li>Scope Clashes for Fields and Local Variables </li></ul><ul><li>Constants </li></ul><ul><li>C# Data Types </li></ul><ul><ul><li>Value Types </li></ul></ul><ul><ul><li>Reference Types </li></ul></ul>
  9. 9. Integer Types Name CTS Type Description sbyte System.Sbyte 8-bit signed integer short System.Int16 16-bit signed integer int System.Int32 32-bit signed integer long System.Int64 64-bit signed integer byte System.Byte 8-bit unsigned integer ushort System.UInt16 16-bit unsigned integer uint System.UInt32 32-bit unsigned integer ulong System.UInt64 64-bit unsigned integer
  10. 10. Floating-Point Types Name CTS Type Description float System.Single 32-bit single-precision floating point double System.Double 64-bit double precision floating point
  11. 11. Decimal Type Name CTS Type Description decimal System.Decimal 128-bit high precision decimal notation
  12. 12. Boolean Type <ul><li>bool type can store either true/false. </li></ul>
  13. 13. Character Type Name CTS Type Description char System.Char 16-bit Unicode character
  14. 14. Escape Sequences Escape Sequence Character ’ Single quotation mark ” Double quotation mark Backslash Null a Alert  Backspace f Form feed New line Carriage return Tab character v Vertial tab
  15. 15. Predefined Reference Types Name CTS Type Description object System.Object The root type string System.String Unicode character string
  16. 16. Object Type
  17. 17. Methods of Object Type <ul><li>Equals() </li></ul><ul><li>GetHashCode() </li></ul><ul><li>GetType() </li></ul><ul><li>ToString() </li></ul>
  18. 18. The string Type
  19. 19. Operators in C# <ul><li>Arithmetic +, -, *, /, % </li></ul><ul><li>Logical &, |, ^, ~, &&, ||, ! </li></ul><ul><li>Comparison ==, !=, <, >, <=, >= </li></ul><ul><li>String Concatenation + </li></ul><ul><li>Increment and Decrement ++, -- </li></ul><ul><li>Bit Shifting (<<, >>) </li></ul><ul><li>Assignment =, +=, -=, /=, %=, ^=, &=, |=, ^=, <<=, >>= </li></ul><ul><li>Member Access . </li></ul>
  20. 20. Operators in C# (contd…) <ul><li>Indexing [] </li></ul><ul><li>Casting ( ) </li></ul><ul><li>Conditional ?: </li></ul><ul><li>Delegate Concatenation and removal +,- </li></ul><ul><li>Object Creation (new) </li></ul><ul><li>Size information (sizeof, typeof, is, as) </li></ul><ul><li>Overflow exception control (checked, unchecked) </li></ul><ul><li>Namespace alias qualifier :: </li></ul><ul><li>Operator Shortcuts </li></ul>
  21. 21. checked operator <ul><li>byte b=255; </li></ul><ul><li>checked </li></ul><ul><li>{ </li></ul><ul><li>b++; </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(b.ToString()); </li></ul><ul><li>When executed, it throws an Exception </li></ul>
  22. 22. unchecked operator <ul><li>byte b=255; </li></ul><ul><li>unchecked </li></ul><ul><li>{ </li></ul><ul><li>b++; </li></ul><ul><li>} </li></ul><ul><li>Console.WriteLine(b.ToString()); </li></ul><ul><li>It won’t throw Exception. However, data would be lost. </li></ul>
  23. 23. is operator <ul><li>Used to check whether an object is compatible with a type. </li></ul><ul><li>Eg: </li></ul><ul><li>int x=10; </li></ul><ul><li>if(x is object) </li></ul><ul><li>Cosole.WriteLine(“x is an object”); </li></ul>
  24. 24. as operator <ul><li>Used to perform explicit type conversions of reference types. </li></ul><ul><li>object o1=“Some String”; </li></ul><ul><li>string s1=o1 as string; </li></ul>
  25. 25. sizeof operator <ul><li>Used to find size required by a type on stack. </li></ul><ul><li>unsafe </li></ul><ul><li>{ </li></ul><ul><li>Console.WriteLine(sizeof(int)); </li></ul><ul><li>} </li></ul><ul><li>Unsafe has to be used when we use pointers. </li></ul>
  26. 26. typeof operator <ul><li>Used to find the Sysem.Type object representing a specified type. </li></ul><ul><li>Eg: </li></ul><ul><li>Console.WriteLine(typeof(string)); </li></ul>
  27. 27. Type Conversions <ul><li>Implicit </li></ul><ul><li>Explicit </li></ul>
  28. 28. Boxing and Unboxing
  29. 29. Flow Control <ul><li>Conditional Statements </li></ul><ul><li>Loops </li></ul><ul><li>Jump Statements </li></ul>
  30. 30. Conditional Statements <ul><li>if </li></ul><ul><li>if – else </li></ul><ul><li>if – else if – else </li></ul><ul><li>switch </li></ul>
  31. 31. Loops <ul><li>while </li></ul><ul><li>do… while </li></ul><ul><li>for </li></ul><ul><li>foreach </li></ul>
  32. 32. Jump Statements <ul><li>goto </li></ul><ul><li>break </li></ul><ul><li>continue </li></ul><ul><li>return </li></ul>
  33. 33. Enumerations <ul><li>Example </li></ul><ul><li>Switch example with enumeration </li></ul>
  34. 34. Arrays
  35. 35. Namespaces
  36. 36. using Statement
  37. 37. More on Compiling Options Option Output /t:exe A console application (default) /t:library A class library with manifest /t:module A component without a manifest /t:winexe A windows application (without a console window)
  38. 38. Console I/O <ul><li>Console.ReadLine() </li></ul><ul><li>Console.WriteLine() </li></ul><ul><li>Console.Read() </li></ul><ul><li>ConsoleWrite() </li></ul>
  39. 39. Using Comments <ul><li>Internal Comments </li></ul><ul><ul><li>Single-line (//) </li></ul></ul><ul><ul><li>Multiline (/* */) </li></ul></ul><ul><li>XML Documentation Comments (///) </li></ul><ul><li>Csc /t:library /doc:Math.xml Math.cs </li></ul>

×