Programming in C Language Overview CSE 459 24

  • Slides: 26
Download presentation
Programming in C# Language Overview CSE 459. 24 Prof. Roger Crawfis

Programming in C# Language Overview CSE 459. 24 Prof. Roger Crawfis

Overview l Quickly go through some of the major differences between C++ or Java

Overview l Quickly go through some of the major differences between C++ or Java and C#. l Overview of a C# program. l Built-in C# types. l Canonical Hello World program

My View on C# l l l I have been using C# in our

My View on C# l l l I have been using C# in our research for since C# 1. 0 in 2004. I think it is a great language!!! However, programming languages are a dime a dozen and somewhat insignificant to large-scale programming. l l The. NET libraries are the real time savers. Or stated differently – well tested, well maintained and well documented reusable components. I could do everything in C, but view C# as an automatic codegenerator for many recurring design and programming patterns. l A strongly-typed language prevents many simple typos from being bugs. l

Language l l Namespaces Classes l l l Fields Properties Methods Attributes Events l

Language l l Namespaces Classes l l l Fields Properties Methods Attributes Events l l l Methods Properties Events l l if, else, while, for, switch foreach Additional Features l Interfaces (contracts) l Control Statements Operation Overloading Structs Enums Delegates OO Features l l l Type Unification Inheritance Polymorphism

C# Program Structure l Namespaces l l Type declarations l l l Classes, struct’s,

C# Program Structure l Namespaces l l Type declarations l l l Classes, struct’s, interfaces, enum’s, and delegates Contain members Members l l l Contain types and other namespaces Constants, fields, methods, operators, constructors, destructors Properties, indexers, events Organization l l No header files, types imported using assemblies (dll’s). Type reflection

Major Language Differences l Automatic memory management l l Garbage Collection No pointers Everything

Major Language Differences l Automatic memory management l l Garbage Collection No pointers Everything inherits from System. Object l Additional language constructs to aid in implementing common patterns: l l l l Iterators – foreach and yield statements Data encapsulation – Properties Function pointers or Functors – delegates Observer Design Pattern – events Aspect-oriented programming – Attributes (loosely) Functional programming – LINQ and anonymous methods

Other Language Differences l Salient points l l l l Conditionals must evaluate to

Other Language Differences l Salient points l l l l Conditionals must evaluate to a Boolean No pointers. Use the dot “. ” to access both namespaces and fields/methods. All fields are initialized by the CLR (zero for value types, null for reference types). The switch statement does not “fall-thru” (unless empty) The switch statement can take bool’s , enum’s, integral types and strings. Expressions must be useful (no a==b; ). Internal or assembly protection control.

Built-in Types l C# predefined types l l l The “root” Logical Signed Unsigned

Built-in Types l C# predefined types l l l The “root” Logical Signed Unsigned Floating-point Textual l object bool sbyte, short, int, long byte, ushort, uint, ulong float, double, decimal char, string Textual types use Unicode (16 -bit characters)

Common Value Types l All integral types: int, uint, long, short, byte, … l

Common Value Types l All integral types: int, uint, long, short, byte, … l float’s, double’s and decimal’s l Structs l l Aka - user-defined value (light-weight) types Can contain arbitrary data Non-extensible (sealed subclasses) System. Decimal is a struct.

MSDN Documentation l Let’s look at the MSDN documentation for Value. Type and few

MSDN Documentation l Let’s look at the MSDN documentation for Value. Type and few other types. Namespace l Assembly l Language filters l Versions l

Built-in Types - Strings l l l Strings are first-class immutable objects in C#

Built-in Types - Strings l l l Strings are first-class immutable objects in C# Behave like a value type Can be indexed like a char array. Has many methods (see System. String) The @ command allows specifying a string literal spanning lines. string s = “Hello”; char third = s[2]; string[] split = s. Split(third);

Instance Intialization l Must be initialized (unlike C++) l l double x; // x

Instance Intialization l Must be initialized (unlike C++) l l double x; // x == 0 string f; // f. equals(“”) A a; // a == null Fundamental difference between double and class A? l Reference types vs. value types

Reference Types l Classes, l l l Inherited from System. Object Transparently refers to

Reference Types l Classes, l l l Inherited from System. Object Transparently refers to a memory location Similar to pointers in other languages l l including user-defined classes Other view is that: Everything is a pointer. Can be set to null memory a { A a = new A(); A b = a; } } b fields in class A for instance a

Value Types Contain the actual value, not the location l Inherited from System. Value.

Value Types Contain the actual value, not the location l Inherited from System. Value. Type l l Which is (logically) derived from System. Object. Treated specially by the runtime l Need to be boxed in order to get a reference l memory 137 a { int a = 137; int b = a; } 137 b

Boxing and Unboxing l Value types are not indirectly referenced l l l More

Boxing and Unboxing l Value types are not indirectly referenced l l l More efficient. Less memory. In order to treat all types uniformly, a value type needs is converted to a reference type. l called boxing. Reverse is unboxing { int a = 137; object o 1 = a; object o 2 = o 1; int b = (int)o 2; } memory 137 a o 1 int 137 b o 2

Differences Between Types l Copy semantics: Polynomial a = new Polynomial(); Polynomial b =

Differences Between Types l Copy semantics: Polynomial a = new Polynomial(); Polynomial b = a; b. Coefficient[0] = 10; Console. Write. Line(a. Coefficient[0]); int a = 1; int b = a; b = 10; Console. Write. Line(a); l Copies l l of value types make a real copy Important for parameter passing Boxing still copies

Function Parameters l In C++ you could pass a variable by: l l In

Function Parameters l In C++ you could pass a variable by: l l In C# there seems to be some confusion and misinformation (including C# in a Nutshell) on this. l l Value (creating a new copy and passing it) Pointer (passing in the address) Reference (same as pointer, less cluttered syntax). The MSDN documentation for ref has this correct. If you were raised on pointers and understand this (which you are not), then it is quite simple. l The ref and out keywords indicate that you should pass: l l Value types – address of value type is passed. Reference types – address of pointer is passed.

Function Parameters l ref parameters Use the value that is passed in l Change

Function Parameters l ref parameters Use the value that is passed in l Change the value of a value type passed in l Change the instance that a reference type points to. l A valid (or initialized) instance or value type must be passed in. l l Null is a valid value.

Function Parameters l out parameters Set the value of a value type passed in.

Function Parameters l out parameters Set the value of a value type passed in. l Set the instance that a reference type points to. l Does not need to be initialized before calling. l Compiler forces you to set a value for all possible code paths. l

Function Parameters l For variable number of parameters public void f(int x, params char[]

Function Parameters l For variable number of parameters public void f(int x, params char[] ar); call f(1), f(1, ‘s’, ‘f’), f(1, “sf”. To. Char. Array()); l Must be the last argument in the list

Control Statements l If, Else l While l Foreach

Control Statements l If, Else l While l Foreach

Switch Statement l Switch statements must be expressions that can be statically evaluated. l

Switch Statement l Switch statements must be expressions that can be statically evaluated. l Restricted to primitive types, strings and enums. l There is no fall through on switch cases unless the case is an empty case:

Switch Statement switch (my. Employee. Name) { case “Boss”: Console. Writeline(“Your fired!”); break; case

Switch Statement switch (my. Employee. Name) { case “Boss”: Console. Writeline(“Your fired!”); break; case “Henry”: case “Jane”: Console. Writeline(“I need a pay raise. ”); break; default: Console. Writeline(“Busy working. ”); break; }

Jump Statements l Several C# statements provide a break in the execution: break –

Jump Statements l Several C# statements provide a break in the execution: break – jumps out of a while or for loop or a switch statement. l continue – starts the next iteration of a loop. l goto – do not use. l return – returns out of the current method. l throw – Causes an exception and ends the current try block or method recursively. l

Implicit Type Declarations l C# l 3. 0 feature Aids readability in declaring instances

Implicit Type Declarations l C# l 3. 0 feature Aids readability in declaring instances of complex types with redundant initialization: var my. String. Builder = new String. Builder(); var my. List = new List<IDictionary<string, int>>(); Added for LINQ support (anonymous types). l Avoid using! l Note: still strongly and statically typed l

Homework and Quiz l Memorize the C# keywords from the appendix in the book

Homework and Quiz l Memorize the C# keywords from the appendix in the book that start with the letters a-b. l Read the first two chapters of the book. l Look over the MSDN documentation for System. String and in particular the Split method.