際際滷

際際滷Share a Scribd company logo
C# Basics Muhammad Kamran Rafi [email_address]
Variables Declarations byte - 0 to 255 char -  2 bytes bool sbyte - -128 to 127 short - 2 byte int ushort - 0 to 65,535 int - 4 bytes uint - 4 bytes positive float double decimal long ulong string
Using Variables C# is Strongly Typed float x  = 10.9f; double y = 15.3; y = x;  // okay x = (float) y;  // conversion required Variables must have Value  before  being used. Hence declarations usually include initialization
Simple Arrays int[] myArray1 = new int[5]; int[] myArray2 = {1, 2, 3, 4, 5}; for (int i=0;i<10; i++) Console.Write (myarray[i]); Console.WriteLine();
Looping Statements Same as C++ while (count > 0)  process_list (count--); do  process_list( ++count ); while (count < 10); for (int i=1; i<=10; i++) Different from C++ while ( count ) // illegal C# unless count is bool
Decision Statements if (count >= 10) {  dostuff(); domorestuff(); } else ... switch (choice) { case 'Y':  //must be empty case 'y':  do_yes_stuff(); break; default: ... Almost Just Like C++
Simple Console Output System.Console.WriteLine System.Console.Write System.Consolue.WriteLine (&quot;count = {0} and sum = {1}&quot;, count, sum);
Simple Console Input string inputline; char  charvalue; int  intvalue; Console.Write (&quot;Enter a string: &quot;); inputline = Console. ReadLine (); Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;,inputline); Console.Write (&quot;Enter a character: &quot;); charvalue = (char) Console. Read (); Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;, charvalue); Console. ReadLine (); Console.Write (&quot;Enter an integer: &quot;); inputline = Console.ReadLine(); intvalue  =  Convert.ToInt32(inputline) ; Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;, intvalue);
Class/Interface Definitions Like Java class modifiers: abstract, static, sealed visibility: public, protected, internal, private interface name starts with I like IComparable, ISerializable const vs. readonly attributes const, predefined type, compile time evaluation, static readonly, value assigned only once (decl or construct)
Static Classes They only contain static members. They cannot be instantiated. They are sealed. They cannot contain instance Constructors
Abstract Classes An abstract class cannot be instantiated.  An abstract class may contain abstract methods .  It is not possible to modify an abstract class with the  sealed  modifier, which means that the class cannot be inherited.  A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods.
Inheritance (C++ syntax) public class Person : IComparable { } public class Employee : Person, ISerializable { }
Constructor public Person (string name, int ssn)  { } public Employee(string name, int ssn, int phone) : base(name, ssn) { } public Employee(string name) : this (name, 999, 999) { }
Overriding Methods Must mark all overridable methods in the superclass with virtual public virtual int foo() Must mark all overriding methods in the subclass with override public override int foo() Use base.MethodName() to call the superclass method
Overloading operators Like C++, we can overload and redefine operators like == and +. public static bool operator== (MyClass lhs, MyClass rhs) { } public static MyClass operator+(MyClass lhs, MyClass rhs)

More Related Content

What's hot (18)

Introduction to c#
Introduction to c#Introduction to c#
Introduction to c#
OpenSource Technologies Pvt. Ltd.
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
Dr. SURBHI SAROHA
Ch7 Basic Types
Ch7 Basic TypesCh7 Basic Types
Ch7 Basic Types
SzeChingChen
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
gourav kottawar
C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3C Programming Language Step by Step Part 3
C Programming Language Step by Step Part 3
Rumman Ansari
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
trupti1976
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
Dr-archana-dhawan-bajaj
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
Venkateswarlu Vuggam
Reflection in Go
Reflection in GoReflection in Go
Reflection in Go
strikr .
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
Venkateswarlu Vuggam
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
HINAPARVEENAlXC
Operators
OperatorsOperators
Operators
Allah Ditta
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
R.Karthikeyan - Vivekananda College
Chapter 2
Chapter 2Chapter 2
Chapter 2
application developer
Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3Objective-C to Swift - Swift Cloud Workshop 3
Objective-C to Swift - Swift Cloud Workshop 3
Randy Scovil
Type conversions
Type conversionsType conversions
Type conversions
sanya6900
Pi j1.2 variable-assignment
Pi j1.2 variable-assignmentPi j1.2 variable-assignment
Pi j1.2 variable-assignment
mcollison
Variables and data types IN SWIFT
 Variables and data types IN SWIFT Variables and data types IN SWIFT
Variables and data types IN SWIFT
LOVELY PROFESSIONAL UNIVERSITY

Similar to C sharp basics (20)

Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
shubhra chauhan
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
InfinityWorld3
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
SHASHIKANT346021
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
ShashikantSathe3
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
Cpprm
CpprmCpprm
Cpprm
Shawne Lee
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
1HK19CS090MOHAMMEDSA
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
ANURAG SINGH
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
gitesh_nagar
LEARN C#
LEARN C#LEARN C#
LEARN C#
adroitinfogen
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
02basics
02basics02basics
02basics
Waheed Warraich
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptx
Nithya K
Visual c sharp
Visual c sharpVisual c sharp
Visual c sharp
Palm Palm Nguy畛n
UNIT1 PPS of C language for first year first semester
UNIT1 PPS of C language for first year first semesterUNIT1 PPS of C language for first year first semester
UNIT1 PPS of C language for first year first semester
Aariz2
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
Tushar Desarda
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
SHASHIKANT346021
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
ShashikantSathe3
434090527-C-Cheat-Sheet. pdf C# program
434090527-C-Cheat-Sheet. pdf  C# program434090527-C-Cheat-Sheet. pdf  C# program
434090527-C-Cheat-Sheet. pdf C# program
MAHESHV559910
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
ANURAG SINGH
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
gitesh_nagar
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
Dr. SURBHI SAROHA
PROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptxPROGRAMMING IN C - Inroduction.pptx
PROGRAMMING IN C - Inroduction.pptx
Nithya K
UNIT1 PPS of C language for first year first semester
UNIT1 PPS of C language for first year first semesterUNIT1 PPS of C language for first year first semester
UNIT1 PPS of C language for first year first semester
Aariz2
20.1 Java working with abstraction
20.1 Java working with abstraction20.1 Java working with abstraction
20.1 Java working with abstraction
Intro C# Book

C sharp basics

  • 1. C# Basics Muhammad Kamran Rafi [email_address]
  • 2. Variables Declarations byte - 0 to 255 char - 2 bytes bool sbyte - -128 to 127 short - 2 byte int ushort - 0 to 65,535 int - 4 bytes uint - 4 bytes positive float double decimal long ulong string
  • 3. Using Variables C# is Strongly Typed float x = 10.9f; double y = 15.3; y = x; // okay x = (float) y; // conversion required Variables must have Value before being used. Hence declarations usually include initialization
  • 4. Simple Arrays int[] myArray1 = new int[5]; int[] myArray2 = {1, 2, 3, 4, 5}; for (int i=0;i<10; i++) Console.Write (myarray[i]); Console.WriteLine();
  • 5. Looping Statements Same as C++ while (count > 0) process_list (count--); do process_list( ++count ); while (count < 10); for (int i=1; i<=10; i++) Different from C++ while ( count ) // illegal C# unless count is bool
  • 6. Decision Statements if (count >= 10) { dostuff(); domorestuff(); } else ... switch (choice) { case 'Y': //must be empty case 'y': do_yes_stuff(); break; default: ... Almost Just Like C++
  • 7. Simple Console Output System.Console.WriteLine System.Console.Write System.Consolue.WriteLine (&quot;count = {0} and sum = {1}&quot;, count, sum);
  • 8. Simple Console Input string inputline; char charvalue; int intvalue; Console.Write (&quot;Enter a string: &quot;); inputline = Console. ReadLine (); Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;,inputline); Console.Write (&quot;Enter a character: &quot;); charvalue = (char) Console. Read (); Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;, charvalue); Console. ReadLine (); Console.Write (&quot;Enter an integer: &quot;); inputline = Console.ReadLine(); intvalue = Convert.ToInt32(inputline) ; Console.WriteLine(&quot;You just entered \&quot;{0}\&quot;&quot;, intvalue);
  • 9. Class/Interface Definitions Like Java class modifiers: abstract, static, sealed visibility: public, protected, internal, private interface name starts with I like IComparable, ISerializable const vs. readonly attributes const, predefined type, compile time evaluation, static readonly, value assigned only once (decl or construct)
  • 10. Static Classes They only contain static members. They cannot be instantiated. They are sealed. They cannot contain instance Constructors
  • 11. Abstract Classes An abstract class cannot be instantiated. An abstract class may contain abstract methods . It is not possible to modify an abstract class with the sealed modifier, which means that the class cannot be inherited. A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods.
  • 12. Inheritance (C++ syntax) public class Person : IComparable { } public class Employee : Person, ISerializable { }
  • 13. Constructor public Person (string name, int ssn) { } public Employee(string name, int ssn, int phone) : base(name, ssn) { } public Employee(string name) : this (name, 999, 999) { }
  • 14. Overriding Methods Must mark all overridable methods in the superclass with virtual public virtual int foo() Must mark all overriding methods in the subclass with override public override int foo() Use base.MethodName() to call the superclass method
  • 15. Overloading operators Like C++, we can overload and redefine operators like == and +. public static bool operator== (MyClass lhs, MyClass rhs) { } public static MyClass operator+(MyClass lhs, MyClass rhs)