SlideShare ist ein Scribd-Unternehmen logo
1 von 28
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#

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
C# programming language
C# programming languageC# programming language
C# programming language
 
C#.NET
C#.NETC#.NET
C#.NET
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
class and objects
class and objectsclass and objects
class and objects
 
ASP.NET Basics
ASP.NET Basics ASP.NET Basics
ASP.NET Basics
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 

Ähnlich wie Introduction to c#

Ähnlich wie Introduction to c# (20)

C# for beginners
C# for beginnersC# for beginners
C# for beginners
 
Dr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slidesDr archana dhawan bajaj - csharp fundamentals slides
Dr archana dhawan bajaj - csharp fundamentals slides
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
 
Java
Java Java
Java
 
Java introduction
Java introductionJava introduction
Java introduction
 
For Beginners - C#
For Beginners - C#For Beginners - C#
For Beginners - C#
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
UsingCPP_for_Artist.ppt
UsingCPP_for_Artist.pptUsingCPP_for_Artist.ppt
UsingCPP_for_Artist.ppt
 
Csharp_mahesh
Csharp_maheshCsharp_mahesh
Csharp_mahesh
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Data types and Operators
Data types and OperatorsData types and Operators
Data types and Operators
 
Net framework
Net frameworkNet framework
Net framework
 
Dr archana dhawan bajaj - c# dot net
Dr archana dhawan bajaj - c# dot netDr archana dhawan bajaj - c# dot net
Dr archana dhawan bajaj - c# dot net
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 
C++ process new
C++ process newC++ process new
C++ process new
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 

Mehr von OpenSource Technologies Pvt. Ltd. (13)

OpenSource Technologies Portfolio
OpenSource Technologies PortfolioOpenSource Technologies Portfolio
OpenSource Technologies Portfolio
 
Cloud Computing Amazon
Cloud Computing AmazonCloud Computing Amazon
Cloud Computing Amazon
 
Empower your business with joomla
Empower your business with joomlaEmpower your business with joomla
Empower your business with joomla
 
Responsive Web Design Fundamentals
Responsive Web Design FundamentalsResponsive Web Design Fundamentals
Responsive Web Design Fundamentals
 
MySQL Training
MySQL TrainingMySQL Training
MySQL Training
 
PHP Shield - The PHP Encoder
PHP Shield - The PHP EncoderPHP Shield - The PHP Encoder
PHP Shield - The PHP Encoder
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
Introduction to Ubantu
Introduction to UbantuIntroduction to Ubantu
Introduction to Ubantu
 
WordPress Plugins
WordPress PluginsWordPress Plugins
WordPress Plugins
 
WordPress Complete Tutorial
WordPress Complete TutorialWordPress Complete Tutorial
WordPress Complete Tutorial
 
Intro dotnet
Intro dotnetIntro dotnet
Intro dotnet
 
Asp.net
Asp.netAsp.net
Asp.net
 
OST Profile
OST ProfileOST Profile
OST Profile
 

Kürzlich hochgeladen

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKJago de Vreede
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Kürzlich hochgeladen (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

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 …