C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,

Slides:



Advertisements
Similar presentations
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Wednesday, 9/4/02, Slide #1 1 CS 106 Intro to CS 1 Wednesday, 9/4/02  Today: Introduction, course information, and basic ideas of computers and programming.
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++
C# Programming: From Problem Analysis to Program Design1 2 Your First C# Program.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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;
CH1 – A 1 st Program Using C#. Program Set of instructions which tell a computer what to do. Machine Language Basic language computers use to control.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
11 Getting Started with C# Chapter Objectives You will be able to: 1. Say in general terms how C# differs from C. 2. Create, compile, and run a.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Javascript. Outline Introduction Fundamental of JavaScript Javascript events management DOM and Dynamic HTML (DHTML)
1 Introduction to Java Brief history of Java Sample Java Program Compiling & Executing Reading: => Section 1.1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
Applied Computing Technology Laboratory QuickStart C# Learning to Program in C# Amy Roberge & John Linehan November 7, 2005.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
By Mr. Muhammad Pervez Akhtar
 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.
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
C# Diline Giriş.
Object Lifetime and Pointers
4. Java language basics: Function
Chapter 7 User-Defined Methods.
C# — Console Application
Java Course Review.
Chapter 3 GC 101 Java Fundamentals.
Yanal Alahmad Java Workshop Yanal Alahmad
Computing Fundamentals
Objectives Identify the built-in data types in C++
Variables, Expressions, and IO
CS360 Windows Programming
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Advanced Programming Lecture 02: Introduction to C# Apps
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
MSIS 655 Advanced Business Applications Programming
T. Jumana Abu Shmais – AOU - Riyadh
Variables, Identifiers, Assignments, Input/Output
Chapter 2: Java Fundamentals
Lab 1 Introduction to C++.
Chapter 3 – Introduction to C# Programming
elementary programming
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Java Looking at our first console application in Eclipse
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
IS 135 Business Programming
Presentation transcript:

C# Part 1 Intro to C#

Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking, detection of attempts to use uninitialized variables, and automatic garbage collection –This doesn’t mean you still can’t write bad code! Garbage collection! Wasn’t made to compete with C or Assembly Event driven, just like VB

Background cont. Standards –ECMA-334 –ISO/IEC 23270

History Anders Hejlsberg started to work on “Cool” (C- like Object Oriented Language) Microsoft decided due to possible trademark concerns to change the name to C# C# has gone through 3 versions

Programming Very basic program (See p1.cs)

Compiling Click on Start –All Programs –Microsoft Visual Studio 2008 –Tools –Visual Studio 2008 Command Prompt (Later we will use Visual Studio to compile) cd to the directory where you are storing the source files (You can also use the build script) csc source.cs

using System; –Comparable to using namespace std; in C++ class FirstProgram –You can name the class whatever you want, unlike Java. static void Main() –Very similar to C++’s int main() –You MUST use a capital M in “Main” Console.WriteLine() –Allows you to output data to the console –(Very exciting I know)

Class Example (This is just to show you what you need to do to compile) Write a simple hello world program Input: None Output: “Hello, world”

Commenting Comments should be plentiful! 2 types –Single line using // –Multi line using /* */

Built in data types All the usual suspects are there…

Variables Declaring variables is just like in C++/Java datatype variable_name; The name can contain letters, digits, and the underscore character (_). The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended at the beginning of a name. An underscore is often used with special commands, and it's sometimes hard to read. Case matters (that is, upper- and lowercase letters). C# is case-sensitive; thus, the names count and Count refer to two different variables. C# keywords can't be used as variable names.

If you feel the need to use a reserved keyword as a variable, use Ie = 5; This creates a constant When would you want do this? Most likely, never.

Some modifiers: Static –Allows you to define a variable that you can access without having an instance of the class –HOWEVER….this is only a single value Public/private/protected –Controls who can access a variable in a class

Class Example Show how a static variable works vs non-static

Arithmetic Performing arithmetic operations is the same as in C++/Java var1 = var2 * var3; Example: int x,y; x = y + 5;

Outputting variables Use {#} to reference a variable in a string Ex. “Hello {0}”, where {0} would be the first variable passed into Console.WriteLine –Ex. Console.WriteLine(“Hello, {0}”, world); You can also concatenate using the plus symbol (See p2.cs)

Input To get input from the user use one the following methods: Console. –ReadLine This allows you to read a String from standard input –Read This allows you to read a single character from standard input –ReadKey This allows you detect any key with any modifiers *Note* - ReadLine returns a string!!!!!!!!!

Class Example Get 2 numbers from the user and perform basic arithmetic on them (+ - * /) Input 2 numbers Output: # + # = # # * # = # # / # = # # - # = #

Class Exercise Write a program that will ask for the users name and output it to the screen with a friendly message Example run: Please enter your name: Nathan Hello, Nathan, Big Brother is watching!

Control Structures Everything is the same as C++ Conditional –if (condition) statement –Ex. If (x > 5) { return 0; } While loop –while (expression) statement –Ex. while(x > 5) { Console.WriteLine(“while…”); } Do-while loop –do statement while (condition); do { Console.WriteLine(“Do, while”); } (x > 5);

Control Structures cont For loop –for (initialization; condition; increase) statement; –Ex. for (int x = 0; x < 5; x++) { Console.WriteLine(“for”); } Foreach loop –foreach (iterator in array) –Ex. foreach( int i in arr) { Console.WriteLine(“{0}”, i); }

Class Example Write a program using a for loop that accepts 5 numbers and tallies them up and then outputs the result

Class Exercise Write a program using a while loop that tallies up numbers from the user until they enter 0 Example: Enter a number: 5 Enter a number: 10 Enter a number: 0 Total = 15

Class Exercise Write a program using a while loop that tallies up numbers from the user until they enter 0 Example: Enter a number: 5 Enter a number: 10 Enter a number: 0 Total = 15

Command line parameters To use command line parameters (ie prog.exe param1) add string[] args as a parameter to the main function –Ex. static void Main(string[] args) This would be very useful for debugging your program (ie prog.exe -debug) (See p4.cs)

Functions Used to turn sections of code into a single line Functions are defined just like in C/C++ [modifier] return-type function-name(parameters) Some modifiers: extern, virtual, static Example: int doublenum(int num)… (See p5.cs)

Classes A class is an encapsulation of methods and variables To define a class: class ClassName {… Everything needs to be defined in a class Example: class cClass { }

The class that you define Main in, you should create a new instance of the class to access the methods/properties Ie class x = new class(); In C# you use dot notation to access methods Ie x.method(); (See p6.cs)

Class Example Write a program that has 2 classes: 1 with the Main function. The second class will be a small Math operations class Implement the function pow

Class Exercise Create 2 classes, 1 with the Main function. The second class with be a coin class, that will be able to calculate how much money you have in coins class cCoin { private intPennies = 0; public void setPennies(int n); public double calc(); //outputs the value in dollars: $0.23 Note: For time sake – just implement methods for pennies and nickels.

Credits