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:

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
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++
Advanced Object-Oriented Programming Features
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
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.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
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,
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
GoodOO Programming Practice in Java © Allan C. Milne v
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Methods Methods are how we implement actions – actions that objects can do, or actions that can be done to objects. In Alice, we have methods such as move,
Object Oriented Software Development 4. C# data types, objects and references.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Topics Instance variables, set and get methods Encapsulation
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
C# for C++ Programmers 1.
Classes (Part 1) Lecture 3
INF230 Basics in C# Programming
Static data members Constructors and Destructors
Chapter 3: Using Methods, Classes, and Objects
Instructor: Ioannis A. Vetsikas
CS360 Windows Programming
CSC 143 Inheritance.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Conditional Statements
Simple Classes in Java CSCI 392 Classes – Part 1.
Tonga Institute of Higher Education
CMSC 202 Classes and Objects.
How to organize and document your classes
Chapter 14 Abstract Classes and Interfaces
Class.
Java Programming Language
Lab4 problems More about templates Some STL
Creating and Using Classes
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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: to describe the behavior of the class  Visibility: public, private (default is private in C#)  Field initialization: fields are automatically initialized to a default value (0 for numbers, false for bools, '\0' for chars, and null for reference types).  use new to instantiate the class or struct  Specific to C#  properties (see later in this chapter)  enums (see later in this chapter)  delegates (events): see in a later lecture

C# D3 Syntax public class MyClass { public int x; //field initialized to 0 automatically string s; // s is private and null // Default constructor: given automatically if there are no // other constructors. If there are other constructors in the // class, it must be explicitly written. public MyClass(){ /*code */ } // Method public int foo(int n){ /*code */ } }

C# D4 Overloading  In a class (or struct), several methods may have the same name  Distinguish between the methods with the list of parameters (the access modifier or the return type won't do it) public void Frodo(int n){} private void Frodo(double x){} public int Frodo(int n){} //Error!  The compiler will tell you if there is any ambiguity  Commonly used with constructors public MyClass(){} public MyClass(string s){} public MyClass(object n){}

C# D5 Properties (1)  Consider the PersonInfo class that has a field age public class PersonInfo{ int age;...}  To give access to the variable age to a user  Make age public: but the user can set age to an invalid value (e.g. -10)  Define getters and setters, e.g. public int GetAge(){return age;} public void SetAge(int a){ if (a >=0 ) age = a;} Cumbersome: to add 1 to age of PersonInfo pi pi.SetAge(pi.GetAge()+1);  Better: use properties

C# D6 Properties (2)  A property defines get and set methods, e.g. public int Age // Age property { get{return age;} set{if (value >=0 ) age = value;} }  The user doesn't call get and set. Instead, the user manipulates the property like a field. Behind the scenes, the compiler generates a call to set or get To add 1 to age of PersonInfo pi: pi.Age += 1; Can't set age to -10: pi.Age = -10; // not done!  A property looks like a public field to the user (same convenient syntax). But the class retains control over the setting of the value of the property.

C# D7 const vs readonly  const (review): to define a variable whose value is defined on the line of declaration. Well suited for variables whose values are known at compilation time. const double PI = ;  But if we don't know the value at compilation time? Use a readonly variable. A value can be assigned to a readonly variable on the line of declaration or in a constructor. readonly double rate; public Mortgage(){ rate = getRateFromDataBase();}

C# D8 static keyword revisited  static can be applied to a field, property or method.  static means that the field, property or method belongs to the class, and is not a feature of an instance of the class. Use the class name to access it, e.g. Math.PI.  Note: a static method can only access the static features of a class.  A class can define a static constructor. In a static constructor, initialize the class static variables (e.g. a static readonly variable) static MyClass(){ /* code */ } // can't use public or private // (always private by default)

C# D9 Enumerations (enum)  A type to define a list of constants. public enum SummerMonths{ June=6, July, August, September} // Under the hood, June is 6, // July 7, etc... // May omit =6. Then June is 0, July 1,... // In some method SummerMonths m = SummerMonths.July; Console.WriteLine(m); // July is printed // Strongly typed m = 7; // error m = (SummerMonths)7; // OK

C# D10 operator overloading  C# offers the option to overload operators (not as versatile as C++, not allowed by Java).  All operators must be declared static  e.g. for a Rational class (describing rational numbers) public static bool operator<=(Rational r1, Rational r2){ /* is r1 <= r2? */ } e.g.  Often, they must be defined in pairs (, == with !=, etc…)  See example Rational.cs on the class web site.

C# D11 Namespace  To avoid class name collisions (likely in a large project), e.g.  if you define a String class, it might be confused with the System.String class  Put your String class in a namespace namespace SeattleCentral{ public class String{ /*code*/ } }  To use the class along with System.String, use the fully qualified class name SeattleCentral.String s1; System.String s2;  Our small projects won't require using namespaces (our classes will reside in the default namespace).

C# D12 Assembly  A key element of.NET  An assembly contains compiled code (IL code) and data describing the code (Metadata). It can also contain resources (icon files…)  In general, several classes are compiled within an assembly (e.g. System.dll)  View the content of an assembly with the.NET tool ildasm (intermediate language disassembler)  In.NET, all classes within one project are compiled into one assembly (you can also do it "by hand" using the command line compiler cs). .NET defines a keyword internal for assembly visibility. It is the default visibility for a top level class (i.e. which is not nested within another class).