Successfully reported this slideshow.
Upcoming SlideShare
×

# C sharp basics

1,706 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### C sharp basics

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