16
C# Tutorial From C++ to C#

C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspx us/library/kx37x362.aspx

Embed Size (px)

Citation preview

Page 1: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

C# Tutorial

From C++ to C#

Page 2: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Some useful links

• Msdn C# http://msdn.microsoft.com/en-us/library/kx37x362.aspx

• C# reserved words (keywords) http://msdn.microsoft.com/en-us/library/x53a06bb.aspx

• C# Programming Guide http://msdn.microsoft.com/en-us/library/67ef8sbd(v=vs.100).aspx

Page 3: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Some History

C#• Appeared in 2000 • Team lead: Anders Hejlsberg• From: Denmark• Sponsor: MicrosoftC++• 1998• Designer: Bjarne Stroustrup• From: Denmark• Sponsor: AT&T

Page 4: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

How to Write? - First C# Programusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program{ static void Main(string[] args)//C# is case-sensitive. You must spell Main with a capital M.//The Main method is special—it designates the program’s entry point.// It must be a static method { Console.WriteLine("Welcome to C# Programming Tutorial");/*Console is a built-in class that contains the methods for displaying messages on the screen and getting input from the keyboard*/ } }}

Page 5: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

How to read?namespace add{ class Program { static void Main(string[] args) { int number1; // declare first number to add int number2; // declare second number to add int sum; // declare sum of number1 and number2 Console.Write("Enter first integer: "); // prompt user // read first number from user number1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter second integer: "); // prompt user // read second number from user number2 = Convert.ToInt32(Console.ReadLine()); sum = number1 + number2; // add numbers Console.WriteLine("Sum is {0}", sum); // display sum } } }

Page 6: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Variables, Types, and Operators• A variable is used to declare a storage location in memory that holds a value. • Each variable must have an unambiguous name. • A variable must be declared with its “type”.• Similar to C and C++, C# has a number of built-in primitive data types:

int, long, float, double, decimal, string, char, bool.

C# supports the regular arithmetic operations: • (+) for addition• (–) for subtraction• (*) for multiplication• (/) for division • (%) for modulus (obtain the remainder of a division)• Unlike C or C++, C# can apply % operator to float or double values or variables. The remainder operator is valid

with all numeric types, and the result is not necessarily an integer. For example, the result of the expression 7.0 % 2.4 is 2.2.

Page 7: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

var• C# compiler can infer the type of a variable from an expression • using the var keyword

Examples:var myVariable = 99;var myOtherVariable = "Hello"; 1. Variables myVariable and myOtherVariable are implicitly typed variables. 2. The var keyword causes the compiler to deduce the type .In the above examples, myVariable is an int, and myOtherVariable is a string. • This is a convenience for declaring variables• After a variable has been declared, you can assign only values of the inferred type For the above example, you cannot assign float, double, or string values to myVariable after it is declared.

Page 8: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Functionsnamespace MaxFinder{ class Program { static void Main(string[] args) { Console.WriteLine("Enter three real numbers,\n"+ "Press 'Enter' after each :"); double num1=Convert.ToDouble(Console.ReadLine()); double num2=Convert.ToDouble(Console.ReadLine()); double num3=Convert.ToDouble(Console.ReadLine()); double result=Maximum(num1,num2,num3); Console.WriteLine("Maximum is "+result); } static double Maximum(double x, double y, double z) { double maxresult = x; if (y > maxresult) maxresult = y; if (z > maxresult) maxresult = z; return maxresult; } }}

Page 9: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Strict Boolean Data Type for Conditions

• C# requires statements that take conditions, such as while and if, to be an expression of a type that implements the Boolean type.

• This prevents certain types of programming mistakes common in C or C++ such as if (a = b) (use of assignment = instead of equality ==).

Page 10: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Static Members

• A static member represents class-wide information, that is, all objects of the class share the same piece of data.

• A static method is special because it can be called without first creating an object of the class.

• The Main method must be declared as static, because the Main method must be called to begin the program execution when there is no object instantiated.

Page 11: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Using the Math Class’ Static Method

static void Main(string[] args){ Console.WriteLine("Enter the lengths of the two legs for a right triangle \n" + "Press 'Enter' after each one:"); double side1 = Convert.ToDouble(Console.ReadLine()); double side2 = Convert.ToDouble(Console.ReadLine()); double h = Math.Sqrt(side1 * side1 + side2 * side2); //calling the static method of class Math Console.WriteLine("the hypotenuse of the right triangle is " + h); }

Page 12: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Random Number Generationnamespace random_number{ class Program { static void Main(string[] args) { Random number_x = new Random(); int face; for (int i = 1; i <= 20; i++) { face = number_x.Next(1, 7); Console.Write("{0} ", face); if (i % 5 == 0) Console.WriteLine(); } } }}

http://msdn.microsoft.com/en-us/library/system.random.aspx

Page 13: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Object-Oriented ProgrammingIn TimePeriod.cs using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace properties{ class TimePeriod { private double seconds; public double Hours { get { return seconds / 3600; } set { seconds = value * 3600; } } }}

In Program.cs using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace properties{ class Program { static void Main(string[] args) { TimePeriod t = new TimePeriod(); t.Hours = 24; //implicitly calling the set method System.Console.WriteLine("Time in hours: " + t.Hours); //calling “get” method } }}

Page 14: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Microsoft Kinect Programming

• http://www.youtube.com/watch?v=Mr71jrkzWq8

• http://www.youtube.com/watch?v=6Sa2diR2mDs

Page 15: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Some Features • C# supports strongly typed implicit variable declarations with the keyword var

• implicitly typed arrays with the keyword new[] followed by a collection initializer.• The type dynamic allows for run-time method binding, allowing for JavaScript like

method calls and run-time object composition.• C# has strongly typed and verbose function pointer support via the keyword delegate.• The C# languages does not allow for global variables or functions• C# supports a strict Boolean data type, bool. • In C#, memory address pointers can only be used within blocks specifically marked as

unsafe, and programs with unsafe code need appropriate permissions to run• Managed memory cannot be explicitly freed; instead, it is automatically garbage

collected. • Multiple inheritance is not supported, although a class can implement any number of

interfaces. • C#, unlike Java, supports operator overloading. • C# is more type safe than C++.

Page 16: C# Tutorial From C++ to C#. Some useful links Msdn C#  us/library/kx37x362.aspx us/library/kx37x362.aspx

Common Type System

• The type system applied in C# is named the “Common Type System” (CTS)

• CTS is a standard in Microsoft .NET framework that specifies the definitions and values of Types represented in the memory.

• It is intended to allow programs written in different programming languages to easily share information.