C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Third Edition
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Review of C++ Programming Part II Sheng-Fang Huang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
COMPUTER SCIENCE I C++ INTRODUCTION
A First Program Using C#
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
JavaScript, Fourth Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe.
Input, Output, and Processing
Chapter 2: Using Data.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mathematical Calculations in Java Mrs. G. Chapman.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Copyright Curt Hill Variables What are they? Why do we need them?
C Sharp Web & Internet Programming Group Diana, Aren, Jeff, & Adam, Farrin 5/5/20081CS 311.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,
Chapter 1.2 Introduction to C++ Programming
Creating and Using Objects, Exceptions, Strings
Chapter 1.2 Introduction to C++ Programming
Multiple variables can be created in one declaration
Advanced Programming Lecture 02: Introduction to C# Apps
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Presentation transcript:

C# Tutorial From C++ to C#

Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx C# reserved words (keywords) us/library/x53a06bb.aspx us/library/x53a06bb.aspx C# Programming Guide us/library/67ef8sbd(v=vs.100).aspx us/library/67ef8sbd(v=vs.100).aspx

Some History C# Appeared in 2000 Team lead: Anders Hejlsberg From: Denmark Sponsor: Microsoft C Designer: Bjarne Stroustrup From: Denmark Sponsor: AT&T

How to Write? - First C# Program using 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*/ }

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 } } }

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.

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.

Functions namespace 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; } }}

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 ==).

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.

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); }

Random Number Generation namespace 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(); }

Object-Oriented Programming In 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 }

Microsoft Kinect Programming Wq8 Wq8 mDs mDs

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.Boolean data type 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. Multiple inheritance C#, unlike Java, supports operator overloading.operator overloading C# is more type safe than C++.type safe

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.