C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
Basic Elements of C++ Chapter 2.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Chapter 3: Data Types and Operators JavaScript - Introductory.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Copyright Curt Hill Variables What are they? Why do we need them?
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
A Sample Program #include using namespace std; int main(void) { cout
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Chapter 2 Variables.
Programming Fundamentals
Basic Elements of C++.
Computing with C# and the .NET Framework
Basic Elements of C++ Chapter 2.
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Variables.
Chapter 2: Java Fundamentals
Data Types Imran Rashid CTO at ManiWeber Technologies.
Java Programming Language
Java Basics Data Types in Java.
Primitive Types and Expressions
Unit 3: Variables in Java
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Variables and Constants
Presentation transcript:

C# C1 CSC 298 Elements of C# code (part 1)

C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class, method, property: capitalize the first letter of each word in the name, e.g. class ThisIsMyClass  variable: same except for the first letter which is lowercase, e.g. myVariable  constant: capitalize every letter. Write an underscore between the words in the name MAX_VALUE (this style is not always followed)

C# C3 Common types  Similar list as in C/C++ and Java  int for an integer (-1, 3, …)  float or double for a floating point value (1.2f, -4.24, …) Careful: double x = 4. // error: need 4.0  char for a character ('F', …)  bool for boolean values (true or false)  Others (but we won't use them much): byte, sbyte, short, ushort, uint, long, ulong

C# C4 Other common types  string: for text string s = "Hello, world!";  The string type has many useful methods (Substring, StartsWith,…). Check the documentation.  Note that a string is immutable.  decimal: for precise manipulation of floating point values (e.g. for currency amounts). decimal d = m;

C# C5 const keyword  Sometimes, a variable should not change (e.g. a mathematical constant such as pi)  To make a variable constant, use const when declaring it const double PI=3.1416;... PI = 4.0; // Error // PI is a constant  All constants are implicitly static (one copy shared by all instances of the class).

C# C6 Doing Arithmetic  Usual arithmetic operations: *, /, + and –  Between integers, / is the integer division 27/12 is 2 (not 2.25)  % : a%b is the remainder of the division of a by b, e.g. 27%12 is 3 since %5 is 4 2%3 is 2 6.2%2.9 is 0.4

C# C7 An example  Input 2 integers and test if the first one is a multiple of the second one  input: use Console.ReadLine, which gives back a string  translate the string to an int with int.Parse  test : if (i1%i2==0)…  display the result with MessageBox.Show  See CommonTypeExample.cs on the web site

C# C8 Casting  Consider double x = 3.8; int i = x; // Error!  Convert to an int using a cast, i.e. write int i = (int)x; // OK, i is 3  C# is strongly typed. It only performs safe automatic conversions (int to double…). Other conversions are the programmer's responsibility.  In general, sometype var; var = (sometype)expression; /* not always allowed. The compiler will tell you (e.g. bool b = (bool)3.4 is an error) */

C# C9 Formatting output  Use placeholders {} to write values within a string when using Console.Write (or WriteLine) double x = 3.41, y = 5.6; Console.Write("x={0}, y={1}, x<y is {2}", x, y, x<y); // prints x=3.41, y=5.6, x<y is True  The placeholder can contain display information, e.g. Console.Write("x={0:E}",x); //prints x= E+000  Many other options: check the documentation (search for String.Format method)

C# C10 string formatting  A good trick: string filename means that \ doesn't mark escape // characters as in \n \t  string.Format to get a formatted string. double x = 3.14; string s = string.Format("x={0}",x); // s is x=3.14 // s = "x=" + x; is also OK (but // formatting can't be controlled as well)

C# C11 Types in C#  Every property, method and variable has a type  2 categories  value types, for simple objects. An example is an integer value (int).  reference (or class) types, e.g. string

C# C12 Defining your own type  Value type: use a struct  Reference type: use a class  How to choose?  if the type is lightweight (contains only simple data), use a struct. If not, use a class.  Differences?  In terms of programming, not many: a struct can have fields, methods, properties (static or not) like a class.  small differences, e.g. a struct has always a default constructor (though you can't write it). The compiler will tell you these details.  Major difference is how the type is handled by the CLR.

C# C13 struct ≠ class  a value type object (=an instance of a struct) is stored on the stack of the method that uses it.  a reference type object (=an instance of a class) is defined on the heap.  stack: defined by the compiler. The size of every object that goes on the stack must be known at compile time.  heap: managed at run time  Accessing the stack is more efficient than accessing the heap.

C# C14 Example public struct PointS { public int x, y;} public class PointC { public int x, y;} // in some method PointS ps = new PointS(); PointC pc = new PointC(); ps x 0 y 0 pc x 0 y 0 stack heap address of the PointC object on the heap

C# C15 ref keyword (1)  Arguments in a method call are passed by value public class MyClass{ public void Frodo(int n){n++;} } // in some method of some other class MyClass c = new MyClass(); int j = 10; c.Frodo(j); Console.WriteLine(j); // What is printed?  Answer: 10 (not 11!). Why? Because Frodo works on a copy of j  What if we want Frodo to change j? Use the ref keyword.

C# C16 ref keyword (2) public class MyClass{ public void Frodo(ref int n){n++;} } // in some method of some other class MyClass c = new MyClass(); int j = 10; c.Frodo(ref j); Console.WriteLine(j); // What is printed?  Answer: 11. Now, Frodo works directly with j (the variable is called n in the context of Frodo, but it refers to the same memory location as j).  A detail: a variable passed by reference (with ref) must be initialized before being passed. Use the out keyword if you don't want to initialize the variable (see documentation).

C# C17 C# aliases  The types available in C# are defined by the CLR.  C# offers the option to use C# specific names for some (of the more common) types.  e.g. int for the CLR defined Int32, string for the CLR defined String, short for the CLR defined Int16, etc…  you can use either notation in your programs int.Parse(...) is the same as Int32.Parse(...)