SlideShare a Scribd company logo
Basic Introduction to C#
Why C# ?
• Builds on COM+ experience
• Native support for
    – Namespaces
    – Versioning
    – Attribute-driven development
•   Power of C with ease of Microsoft Visual Basic®
•   Minimal learning curve for everybody
•   Much cleaner than C++
•   More structured than Visual Basic
•   More powerful than Java
C# – The Big Ideas
            A component oriented language
• The first “component oriented” language in
     the C/C++ family
   – In OOP a component is: A reusable program that can be
     combined with other components in the same system to form
     an application.
   – Example: a single button in a graphical user interface, a small
     interest calculator
   – They can be deployed on different servers and communicate
     with each other

• Enables one-stop programming
          – No header files, IDL, etc.
          – Can be embedded in web pages
C# Overview
• Object oriented
• Everything belongs to a class
  – no global scope
• Complete C# program:
           using System;
           namespace ConsoleTest
           {
                   class Class1
                   {
                            static void Main(string[] args)
                            {
                            }
                   }
           }
C# Program Structure
•   Namespaces
    –   Contain types and other namespaces
•   Type declarations
    –   Classes, structs, interfaces, enums,
        and delegates
•   Members
    –   Constants, fields, methods, properties, events, operators,
        constructors, destructors
•   Organization
    –   No header files, code written “in-line”
    –   No declaration order dependence
Simple Types
• Integer Types
   – byte, sbyte (8bit), short, ushort (16bit)
   – int, uint (32bit), long, ulong (64bit)

• Floating Point Types
   – float (precision of 7 digits)
   – double (precision of 15–16 digits)

• Exact Numeric Type
   – decimal (28 significant digits)

• Character Types
   – char (single character)
   – string (rich functionality, by-reference type)

• Boolean Type
   – bool (distinct type, not interchangeable with int)
Arrays
• Zero based, type bound
• Built on .NET System.Array class
• Declared with type and shape, but no bounds
   – int [ ] SingleDim;
   – int [ , ] TwoDim;
   – int [ ][ ] Jagged;
• Created using new with bounds or initializers
   – SingleDim = new int[20];
   – TwoDim = new int[,]{{1,2,3},{4,5,6}};
   – Jagged = new int[1][ ];
     Jagged[0] = new int[ ]{1,2,3};
Statements and Comments

•   Case sensitive (myVar != MyVar)
•   Statement delimiter is semicolon        ;
•   Block delimiter is curly brackets       { }
•   Single line comment is                  //
•   Block comment is                        /* */
     – Save block comments for debugging!
Data
• All data types derived from
  System.Object
• Declarations:
    datatype varname;
    datatype varname = initvalue;
• C# does not automatically initialize local
  variables (but will warn you)!
Value Data Types
• Directly contain their data:
  –   int      (numbers)
  –   long     (really big numbers)
  –   bool     (true or false)
  –   char     (unicode characters)
  –   float    (7-digit floating point numbers)
  –   string   (multiple characters together)
Data Manipulation

=      assignment
+      addition
-      subtraction
*      multiplication
/      division
%      modulus
++ increment by one
--     decrement by one
strings
• Immutable sequence of Unicode characters
  (char)

• Creation:
  – string s = “Bob”;
  – string s = new String(“Bob”);

• Backslash is an escape:
  – Newline: “n”
  – Tab: “t”
string/int conversions
• string to numbers:
  – int i = int.Parse(“12345”);
  – float f = float.Parse(“123.45”);


• Numbers to strings:
  – string msg = “Your number is ” + 123;
  – string msg = “It costs ” +
                  string.Format(“{0:C}”, 1.23);
String Example
using System;
namespace ConsoleTest
{
       class Class1
       {
                 static void Main(string[ ] args)
                 {
                             int myInt;
                             string myStr = "2";
                             bool myCondition = true;

                            Console.WriteLine("Before: myStr = " + myStr);
                            myInt = int.Parse(myStr);
                            myInt++;
                            myStr = String.Format("{0}", myInt);
                            Console.WriteLine("After: myStr = " + myStr);

                            while(myCondition) ;
                 }
       }
}
Arrays
•   (page 21 of quickstart handout)
•   Derived from System.Array
•   Use square brackets          []
•   Zero-based
•   Static size
•   Initialization:
    – int [ ] nums;
    – int [ ] nums = new int[3]; // 3 items
    – int [ ] nums = new int[ ] {10, 20, 30};
Arrays Continued
• Use Length for # of items in array:
   – nums.Length
• Static Array methods:
   – Sort           System.Array.Sort(myArray);
   – Reverse System.Array.Reverse(myArray);
   – IndexOf
   – LastIndexOf
   Int myLength = myArray.Length;
   System.Array.IndexOf(myArray, “K”, 0, myLength)
Arrays Final
• Multidimensional

   // 3 rows, 2 columns
   int [ , ] myMultiIntArray = new int[3,2]

   for(int r=0; r<3; r++)
   {
         myMultiIntArray[r][0] = 0;
         myMultiIntArray[r][1] = 0;
   }
Conditional Operators

   == equals
   !=     not equals

   <      less than
   <= less than or equal
   >      greater than
   >= greater than or equal

   &&    and
   ||    or
If, Case Statements

if (expression)      switch (i) {
                        case 1:
   { statements; }
                             statements;
else if                      break;
                        case 2:
   { statements; }
                             statements;
else                         break;
                        default:
   { statements; }           statements;
                             break;
                     }
Loops

for (initialize-statement; condition; increment-statement);
{
   statements;
}


while (condition)
{
  statements;
}

      Note: can include break and continue statements
Classes, Members and Methods
  • Everything is encapsulated in a class
  • Can have:
     – member data
     – member methods

        Class clsName
        {
            modifier dataType varName;
            modifier returnType methodName (params)
            {
               statements;
               return returnVal;
            }
        }
Class Constructors
• Automatically called when an object is
  instantiated:

  public className(parameters)
  {
     statements;
  }
Hello World

namespace Sample
{
    using System;

    public class HelloWorld
                               Constructor
    {
        public HelloWorld()
        {
        }

          public static int Main(string[]
  args)
          {
              Console.WriteLine("Hello
  World!");
              return 0;
          }
    }
Another Example
using System;
namespace ConsoleTest
{
      public class Class1
      {
             public string FirstName = "Kay";
             public string LastName = "Connelly";

            public string GetWholeName()
            {
                        return FirstName + " " + LastName;
            }

            static void Main(string[] args)
            {
                  Class1 myClassInstance = new Class1();

                  Console.WriteLine("Name: " + myClassInstance.GetWholeName());

                  while(true) ;
            }
      }
}
Summary
• C# builds on the .NET Framework
  component model
• New language with familiar structure
  – Easy to adopt for developers of C, C++, Java,
    and Visual Basic applications
• Fully object oriented
• Optimized for the .NET Framework
ASP .Net and C#
• Easily combined and ready to be used in
  WebPages.
• Powerful
• Fast
• Most of the works are done without
  getting stuck in low level programming and
  driver fixing and …
An ASP.Net Simple Start
End of The C#

More Related Content

PPT
C# basics
PPTX
C# programming language
PPTX
Chapter 1 introduction to e-commerce
PPTX
Visual Programming
PPT
Programming in c#
PPT
Introduction To C#
PPTX
CSharp Presentation
PPTX
C# 101: Intro to Programming with C#
C# basics
C# programming language
Chapter 1 introduction to e-commerce
Visual Programming
Programming in c#
Introduction To C#
CSharp Presentation
C# 101: Intro to Programming with C#

What's hot (20)

PPT
C# Exceptions Handling
PPT
C#.NET
PPT
7.data types in c#
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPT
Core java concepts
PPT
Looping statements in Java
PPTX
Interface in java
PDF
Java variable types
PDF
Object-oriented Programming-with C#
PPTX
Inheritance in java
PPTX
Control Statements in Java
PPTX
C# classes objects
PPT
Java Networking
PPTX
Interfaces in java
PPTX
Access Modifier.pptx
PPTX
Java abstract class & abstract methods
PPTX
Arrays in Java
PDF
Java I/o streams
PDF
Python programming : Classes objects
PPTX
Java constructors
C# Exceptions Handling
C#.NET
7.data types in c#
Basic Concepts of OOPs (Object Oriented Programming in Java)
Core java concepts
Looping statements in Java
Interface in java
Java variable types
Object-oriented Programming-with C#
Inheritance in java
Control Statements in Java
C# classes objects
Java Networking
Interfaces in java
Access Modifier.pptx
Java abstract class & abstract methods
Arrays in Java
Java I/o streams
Python programming : Classes objects
Java constructors
Ad

Similar to Introduction to c# (20)

PDF
C# for beginners
PDF
Dr archana dhawan bajaj - csharp fundamentals slides
PDF
4java Basic Syntax
PPT
Introduction to Java(basic understanding).ppt
PPT
PPTX
Java introduction
PPT
For Beginners - C#
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPT
C++ - A powerful and system level language
PPT
lecture5-cpp.pptintroduccionaC++basicoye
PPT
Introduction to Inheritance in C plus plus
PPT
UsingCPP_for_Artist.ppt
PPT
Csharp_mahesh
PPTX
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
PPTX
Oop c++class(final).ppt
PPT
Data types and Operators
PPTX
Net framework
C# for beginners
Dr archana dhawan bajaj - csharp fundamentals slides
4java Basic Syntax
Introduction to Java(basic understanding).ppt
Java introduction
For Beginners - C#
lecture02-cpp.ppt
lecture02-cpp.ppt
lecture02-cpp.ppt
lecture02-cpp.ppt
C++ - A powerful and system level language
lecture5-cpp.pptintroduccionaC++basicoye
Introduction to Inheritance in C plus plus
UsingCPP_for_Artist.ppt
Csharp_mahesh
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
Oop c++class(final).ppt
Data types and Operators
Net framework
Ad

More from OpenSource Technologies Pvt. Ltd. (13)

PDF
OpenSource Technologies Portfolio
PPT
Cloud Computing Amazon
PPT
Empower your business with joomla
PPTX
Responsive Web Design Fundamentals
PPT
PHP Shield - The PHP Encoder
PPT
Introduction to Ubantu
PPT
PPT
WordPress Complete Tutorial
OpenSource Technologies Portfolio
Cloud Computing Amazon
Empower your business with joomla
Responsive Web Design Fundamentals
PHP Shield - The PHP Encoder
Introduction to Ubantu
WordPress Complete Tutorial

Recently uploaded (20)

PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
1. Introduction to Computer Programming.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mushroom cultivation and it's methods.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
TLE Review Electricity (Electricity).pptx
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPT
Teaching material agriculture food technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Getting Started with Data Integration: FME Form 101
OMC Textile Division Presentation 2021.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
1. Introduction to Computer Programming.pptx
Empathic Computing: Creating Shared Understanding
Mushroom cultivation and it's methods.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Programs and apps: productivity, graphics, security and other tools
TLE Review Electricity (Electricity).pptx
A comparative study of natural language inference in Swahili using monolingua...
MIND Revenue Release Quarter 2 2025 Press Release
Group 1 Presentation -Planning and Decision Making .pptx
cloud_computing_Infrastucture_as_cloud_p
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Building Integrated photovoltaic BIPV_UPV.pdf
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Diabetes mellitus diagnosis method based random forest with bat algorithm
Getting Started with Data Integration: FME Form 101

Introduction to c#

  • 2. Why C# ? • Builds on COM+ experience • Native support for – Namespaces – Versioning – Attribute-driven development • Power of C with ease of Microsoft Visual Basic® • Minimal learning curve for everybody • Much cleaner than C++ • More structured than Visual Basic • More powerful than Java
  • 3. C# – The Big Ideas A component oriented language • The first “component oriented” language in the C/C++ family – In OOP a component is: A reusable program that can be combined with other components in the same system to form an application. – Example: a single button in a graphical user interface, a small interest calculator – They can be deployed on different servers and communicate with each other • Enables one-stop programming – No header files, IDL, etc. – Can be embedded in web pages
  • 4. C# Overview • Object oriented • Everything belongs to a class – no global scope • Complete C# program: using System; namespace ConsoleTest { class Class1 { static void Main(string[] args) { } } }
  • 5. C# Program Structure • Namespaces – Contain types and other namespaces • Type declarations – Classes, structs, interfaces, enums, and delegates • Members – Constants, fields, methods, properties, events, operators, constructors, destructors • Organization – No header files, code written “in-line” – No declaration order dependence
  • 6. Simple Types • Integer Types – byte, sbyte (8bit), short, ushort (16bit) – int, uint (32bit), long, ulong (64bit) • Floating Point Types – float (precision of 7 digits) – double (precision of 15–16 digits) • Exact Numeric Type – decimal (28 significant digits) • Character Types – char (single character) – string (rich functionality, by-reference type) • Boolean Type – bool (distinct type, not interchangeable with int)
  • 7. Arrays • Zero based, type bound • Built on .NET System.Array class • Declared with type and shape, but no bounds – int [ ] SingleDim; – int [ , ] TwoDim; – int [ ][ ] Jagged; • Created using new with bounds or initializers – SingleDim = new int[20]; – TwoDim = new int[,]{{1,2,3},{4,5,6}}; – Jagged = new int[1][ ]; Jagged[0] = new int[ ]{1,2,3};
  • 8. Statements and Comments • Case sensitive (myVar != MyVar) • Statement delimiter is semicolon ; • Block delimiter is curly brackets { } • Single line comment is // • Block comment is /* */ – Save block comments for debugging!
  • 9. Data • All data types derived from System.Object • Declarations: datatype varname; datatype varname = initvalue; • C# does not automatically initialize local variables (but will warn you)!
  • 10. Value Data Types • Directly contain their data: – int (numbers) – long (really big numbers) – bool (true or false) – char (unicode characters) – float (7-digit floating point numbers) – string (multiple characters together)
  • 11. Data Manipulation = assignment + addition - subtraction * multiplication / division % modulus ++ increment by one -- decrement by one
  • 12. strings • Immutable sequence of Unicode characters (char) • Creation: – string s = “Bob”; – string s = new String(“Bob”); • Backslash is an escape: – Newline: “n” – Tab: “t”
  • 13. string/int conversions • string to numbers: – int i = int.Parse(“12345”); – float f = float.Parse(“123.45”); • Numbers to strings: – string msg = “Your number is ” + 123; – string msg = “It costs ” + string.Format(“{0:C}”, 1.23);
  • 14. String Example using System; namespace ConsoleTest { class Class1 { static void Main(string[ ] args) { int myInt; string myStr = "2"; bool myCondition = true; Console.WriteLine("Before: myStr = " + myStr); myInt = int.Parse(myStr); myInt++; myStr = String.Format("{0}", myInt); Console.WriteLine("After: myStr = " + myStr); while(myCondition) ; } } }
  • 15. Arrays • (page 21 of quickstart handout) • Derived from System.Array • Use square brackets [] • Zero-based • Static size • Initialization: – int [ ] nums; – int [ ] nums = new int[3]; // 3 items – int [ ] nums = new int[ ] {10, 20, 30};
  • 16. Arrays Continued • Use Length for # of items in array: – nums.Length • Static Array methods: – Sort System.Array.Sort(myArray); – Reverse System.Array.Reverse(myArray); – IndexOf – LastIndexOf Int myLength = myArray.Length; System.Array.IndexOf(myArray, “K”, 0, myLength)
  • 17. Arrays Final • Multidimensional // 3 rows, 2 columns int [ , ] myMultiIntArray = new int[3,2] for(int r=0; r<3; r++) { myMultiIntArray[r][0] = 0; myMultiIntArray[r][1] = 0; }
  • 18. Conditional Operators == equals != not equals < less than <= less than or equal > greater than >= greater than or equal && and || or
  • 19. If, Case Statements if (expression) switch (i) { case 1: { statements; } statements; else if break; case 2: { statements; } statements; else break; default: { statements; } statements; break; }
  • 20. Loops for (initialize-statement; condition; increment-statement); { statements; } while (condition) { statements; } Note: can include break and continue statements
  • 21. Classes, Members and Methods • Everything is encapsulated in a class • Can have: – member data – member methods Class clsName { modifier dataType varName; modifier returnType methodName (params) { statements; return returnVal; } }
  • 22. Class Constructors • Automatically called when an object is instantiated: public className(parameters) { statements; }
  • 23. Hello World namespace Sample { using System; public class HelloWorld Constructor { public HelloWorld() { } public static int Main(string[] args) { Console.WriteLine("Hello World!"); return 0; } }
  • 24. Another Example using System; namespace ConsoleTest { public class Class1 { public string FirstName = "Kay"; public string LastName = "Connelly"; public string GetWholeName() { return FirstName + " " + LastName; } static void Main(string[] args) { Class1 myClassInstance = new Class1(); Console.WriteLine("Name: " + myClassInstance.GetWholeName()); while(true) ; } } }
  • 25. Summary • C# builds on the .NET Framework component model • New language with familiar structure – Easy to adopt for developers of C, C++, Java, and Visual Basic applications • Fully object oriented • Optimized for the .NET Framework
  • 26. ASP .Net and C# • Easily combined and ready to be used in WebPages. • Powerful • Fast • Most of the works are done without getting stuck in low level programming and driver fixing and …