Session 1 C# Basics.

Slides:



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

C Language.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Java Syntax Primitive data types Operators Control statements.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Module 1: Introduction to C# Module 2: Variables and Data Types
CIS Computer Programming Logic
DAT602 Database Application Development Lecture 5 JAVA Review.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to C# C# is - elegant, type-safe, object oriented language enabling to build applications that run on the.NET framework - types of applications.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Applications Development
1 Enumerations and Structs Chapter 9. 2 Objectives You will be able to: Write programs that define and use enumeration variables. Write programs that.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
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.
C Sharp Web & Internet Programming Group Diana, Aren, Jeff, & Adam, Farrin 5/5/20081CS 311.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming with Microsoft Visual Basic th Edition
Object Oriented Software Development 4. C# data types, objects and references.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
 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 C# By: Abir Ghattas Michel Barakat.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Session 02 Module 3: Statements and Operators Module 4: Programming constructs Module 5: Arrays.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Value Types. 2 Objectives Discuss concept of value types –efficiency –memory management –value semantics –boxing –unboxing –simple types Introduce struct.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Computing with C# and the .NET Framework
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Chapter 3: Understanding C# Language Fundamentals
CS360 Windows Programming
Conditional Statements
Module 2: Understanding C# Language Fundamentals
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Presentation transcript:

Session 1 C# Basics

Objectives Explain the need for C# Discuss flow of control of a C# program List the fundamental data types in C# Discuss the concepts of Boxing and UnBoxing Discuss Structures and enumerators Discuss a simple C# program

Microsoft .NET New platform for developing windows & web applications Supports more than 20 languages Revolutionizes facilities available for Windows programming

Introduction to C# C# compiler is the most efficient compiler in the .NET family Provides native support for COM (Component Object Model) Enhances developer productivity and increases safety, by enforcing strict type checking Allows restricted use of native pointers

C# Program Flow A simple C# program – /* This is the Hello world Program in C# */ using System; class HelloWorldDemo { public static void Main() Console.WriteLine (“This is the Hello World program”); }

C# Constructs Variables in C# are declared as follows AccessModifier DataType Variable Public int Private string Protected float

C# Constructs – Contd… prefix the variable name with a ‘@’ symbol. To use a keyword as a variable name, prefix the variable name with a ‘@’ symbol. using System; class VariableDemo { public static void Main() string @string; @string = ”string is a keyword but used as a variable name in this example”; Console.WriteLine (@string); }

C# Constructs – Contd… using System; class DefaultValDemo { public static void Main() int[] array1 = new int[5]; Console.WriteLine(“10 multiplied by default value of second array element is {0}“ , 10 * array1[2]); }

C# Data Types C# Data type Description Example object Base data type for all other types. object o = null; string Declares a variable that can store string values string s = “hello”; int Declares a variable that can store integer values. int val = 12; byte Declares a variable that can store byte integer values. byte val = 12; float Declares a variable, which can store real number values that have integer and decimals parts float val = 1.23F; bool bool val1 = true; bool val2 = false; char Declares a variable, which can store char values. char val = 'h';

Default Values Default values of the common data types: Type Numeric (int,float,short) Bool False Char ‘\0’ Enum

Input / Output In C# Uses methods of the Console class in the System namespace The most widely used methods are – Console.ReadLine() Console.WriteLine()

Input / Output In C# - Contd… using System; class TestDefaultValues { static void Main() int number, result; number=5; result=100 * number; Console.WriteLine (“Result is {0} when 100 is multiplied by number {1}”, result, number); } The highlighted line uses the placeholder {0} where the value of the specified variables (result and number) will be substituted and displayed.

Input / Output In C# - Contd… using System; class InputStringDemo { public static void Main() string input; input = Console.ReadLine(); Console.WriteLine (“The input string was :{0}”, input); }

The if construct Used for performing conditional branching Syntax - if (expression) { //One or more statements to be executed if the expression evaluates to true } [else //One or more statements to be executed if the expression evaluates to false }] The expression should always evaluate to an expression of Boolean type

Selection Statement string str = ”hello”; if (str) System.Console.WriteLine (“The value is True”); if (str == “hello”) System.Console.WriteLine (“The Value is True”); The above piece of code will display an error message - Error CS0029 : Cannot implicitly convert type 'int' to 'bool'

The switch Statement Syntax switch(variable) { case value: break; default: }

The switch Statement – Contd… switch(weekday) { case 1: Console.WriteLine (“You have selected Monday”); break; case 2: Console.WriteLine (“You have selected Tuesday”); default: Console.WriteLine (“Sunday is the Default choice!”); }

Iteration Constructs Perform a certain set of instructions for a certain number of times or while a specific condition is true Types of iteration constructs – The While Loop The Do Loop The For Loop The foreach Loop

The while loop The while loop iterates through the specified statements till the condition specified is true Syntax – The break statement - to break out of the loop at anytime The continue statement - to skip the current iteration and begin with the next iteration

The do loop Syntax -

The for loop Syntax -

The foreach loop (1) The foreach loop is used to iterate through a collection or an array Syntax -

The foreach loop – Contd… using System; public class ForEachDemo { static void Main (String[] args) int index; String[] array1=new String[3]; for (index=0;index<3;index++) array1[index]=args [index]; } foreach (String strName in array1) Console.WriteLine (strName);

Fundamental Types Of C# C# divides data types into two fundamental categories Value Types int, char , and structures Reference Types - classes, interfaces, arrays and strings

Fundamental Types Of C# - Contd… Just hold a value in memory Are stored in a stack Value Types Contains the address of the object in the heap = null means that no object has been referenced Reference Types

Value Types using System; class DataTypeTest { public static void Main()    {     int variableVal = 100;      funcTest(variableVal); Console.WriteLine(“This value of the variable is {0}",variableVal);    }    static void funcTest (int variableVal)    { int tempVar = 10;        variableVal = tempVar*20;    } }

Reference Types using System; class DataTypeTest { public int variableVal; } class DataTypeTestRef static void Main() DataTypeTest dataTest = new DataTypeTest(); dataTest.variableVal = 100; funcDataTypeTest(dataTest); Console.WriteLine (dataTest.variableVal);

Reference Types - Contd static void funcDataTypeTest(DataTypeTest dataTest) { int tempVar = 10;       dataTest.variableVal = tempVar*20; }

Value types vs. Reference types Variable Holds Actual Value Allocated Inline (Stack) Heap Default Value Zero Null Parameter to functions Copy Value Copy Reference

Boxing & Unboxing Boxing is conversion of a value type into a reference type Unboxing is the conversion of a reference type into a value type

Boxing & Unboxing – Contd… ... class BoxEx { int objsTaker(object objectX) //objsTaker takes an object //and processes it here } object objsConverter() //objsConverter does the processing //and returns an object //Implementation of code int variable1; variable1 = 5; BoxEx boxVar = new BoxEx(); boxVar.objsTaker(variable1); //line 1 int convertVar = (int) boxVar.objsConverter(); //line 2

Data Types In C# C# provides us with a Unified Type System All data types in C#, are derived from just one class, the object class using System; class ObjectProff { public static void Main() string objectVal; objectVal = 7.ToString(); Console.WriteLine (“The value now is ”+objectVal); }

Static Members Members are not associated with any particular object or the class Only one instance of this member is possible … static int staticMem; static int instanceCount() { //instanceCount Implementation }

Arrays A group of values of similar data type Belong to the reference type and hence are stored on the heap The declaration of arrays in C# follow the syntax given below - DataType[number of elements] ArrayName; int[6] array1;

Structures Custom data Types Can have methods defined within them Cannot implement inheritance Represent Value types … struct structEx { public int structDataMember; public void structMethod1() //structMethod1 Implementation }

Enumerators public class Holiday { public enum WeekDays Monday, Tuesday, Wednesday, Thursday, Friday } public void GetWeekDays (String EmpName, WeekDays DayOff) //Process WeekDays static void Main() Holiday myHoliday = new Holiday(); myHoliday.GetWeekDays (“Richie”, Holiday.WeekDays.Wednesday);

Enumerators – Contd… Enumerators in C# have numbers associated with the values By default, the first element of the enum is assigned a value of 0 and is incremented for each subsequent enum element The default value can be overridden during initialization public enum WeekDays { Monday=1, Tuesday=2, Wednesday=3, Thursday=4, Friday=5 }

Compiling and Executing C# Program Click 'Start  Programs Microsoft Visual Studio.Net 2003' Start Page screen showing recently used projects is displayed on the screen

Compiling and Executing C# Program – Contd…

Compiling and Executing C# Program – Contd… Click File  New  Project to start a new project Select 'Visual C# Projects' from the list on the right hand side and 'Console Application' from the list on the left hand side Type the name of the project in the 'Name' section as Example 1. In the Solution Explorer rename ConsoleApplication1 to Chapter1 Rename the project to Example1 and Class1.cs to HelloWorldDemo.cs

Compiling and Executing C# Program – Contd…

Compiling and Executing C# Program – Contd… Save the file and Select Build  Build Solution option to build the solution Select Start without Debugging option from the Debug menu to execute the application

Compiling and Executing C# Program – Contd… Another method to compile and execute a program is using notepad and DOS prompt Step 1 – Type your code in Notepad Step 2 – Save the file with a .cs extension Step 3 – Switch to DOS prompt and type the following command csc <filename>.cs To run the C# file, type the name of the file without the extension

Summary C# is an object oriented language with many powerful features and is an integral part of the .Net platform Variables in C# are declared in the following way AccessModifier DataType VariableName; C# provides the if and switch…case constructs to perform operations based on the value of an expression. C# provides the following types of iteration constructs, while loop do loop for loop foreach loop

Summary In C#, the data types are divided into two fundamental categories namely value types and reference types. Boxing is the conversion of a value type into a reference type while Unboxing refers to converting a reference type into a value type. Structs in C# can have methods defined within them, and represent Value types. Enums (short for Enumerators) are a set of named numeric constants. C# programs can be written using the Visual Studio .NET IDE as well as Notepad