Title: C
1C Basics
2Basic Characteristics
- Primary language for .Net
- 60 Java 25 C/C 15 New
- One class or partial class per file
3Code Organization
using System ///ltsummarygt ///This is the
standard hello world app ///lt/summarygt namespace
Bob class Hello /
another type comment that spans
multi-lines / public static void Main()
//Single line
comment System.Console.WriteLine(
Hello World)
4Type Mapping
5Control Statements (When diff from Java)
- switch (must have break case can be string)
- foreach (used in collection iteration)
- foreach ( int i in intarray)
- goto (not into a loop or nested block)
- for ( ) if (loc gt 0) goto Found
- continue Found m
6Preprocessor (like C/C)
- define, undef
- if, elif, else, endif
- line
- region, endregion
- error, warning
- pragma
7Enumerations
- Can be of several integral types(default is int).
- enum Status On, Off, Manual, Auto
- public enum Status byte On 1, Off 2,
Manual 4, Automatic 8
8Enumerations help type safety
int status const int ON 1 const int OFF
2 const int MANUAL 4 const int AUTO
8 status ON status 10000.
enum Status Status status status
Status.On status 10000 //compile error!!!!
If powers of 2, can use as bit flags currentStat
us Status.On Status.Manual use Flags
attribute so ToString gives correct output.
9Attributes
- Sends information (metadata) to the compiler
about our code - We already mentioned Flags
- Obsolete
- Serializable
- We can define our own.
10Arrays (Just like Java, except multi-dim)
- int x new int5
- int x 1,2,3,4,5
- has Properties
- x.Length
- utility methods
- Array.Reverse(x)
- Array.Sort(x)
- int , x new int 5, 6
11Properties
- Replaces Java getter and setter
- public class Person
-
- private string name
- public string Name
-
- get return name
- set name value
-
Person p new Person() p.Name
Bob System.Console.WriteLine(p.Name)
12Class/Interface Definitions Like Java
- class modifiers abstract, static, sealed
- visibility public, protected, internal,
protected internal, private - interface name starts with I like IComparable,
ISerializable - class names do NOT start with C
- const vs. readonly attributes
- const, predefined type, compile time evaluation,
static - readonly, value assigned only once (decl or
construct)
13Inheritance (C syntax)
- public class Person IComparable
-
-
- public class Employee Person, ISerializable
-
14Constructor
public Person (string name, int ssn)
public Employee(string name, int ssn, int
phone) base(name, ssn) public
Employee(string name) this (name, 999,
999) //static constructor static Employee(
)
15Overriding Methods
- Must mark all overridable methods in the
superclass with virtual - public virtual int foo()
- Must mark all overriding methods in the subclass
with override - public override int foo()
- Use base.MethodName() to call the superclass
method
16Overloading operators
- Like C, we can overload and redefine operators
like and . - public static bool operator (MyClass lhs,
MyClass rhs) - public static MyClass operator(MyClass lhs,
MyClass rhs)
17Parameter passing
- Default is by value like Java
- Can change to reference using either ref or out.
- ref pass by reference calling initializes
- out pass by reference called initializes
- double orbit
- p.ComputeOrbit(planet, fuel, out orbit)
- public int ComputeOrbit(Planet p, int fuel, out
double orbit) - double orbit 10.756
- p.ComputeOrbit(planet, fuel, ref orbit)
- public int ComputeOrbit(Planet p, int fuel, ref
double orbit) - Variable parameters params
18structs (like C/C)
- value type (on stack) (calling overhead)
- cannot subclass from (sealed)
- can implement interfaces
- are considered subclass of type Object.
- cannot initialize attributes in definition
- must initialize all member attributes on creation
(or before use). - can be more efficient for small classes, but
beware of boxing/unboxing and paramater passing
overhead
19Use structs for simple types that will be used
like built-in types
public struct Location private int x
private int y public Location
(int px, int py) x px
y py
public int X get return x
set x value Location loc new
Location(10, 20)
20Interfaces
- no access modifiers (everything is public)
- no constants, attributes, or implementation of
methods. - is operator, test if a class implements an
interface - if (e is ISerializable) .
- as operator, test a class and cast it
- ISerializable doc e as ISerializable
- if (doc ! null)
21Interfaces vs. Abstract Classes
- If you are creating libraries, or classes subject
to change (adding new methods), then abstract
classes are preferred over interfaces. (virtual
empty methods). - Classes for a single project, Interfaces can be
easier to understand.
22What if interfaces have the same method?
public interface IReadable void
Read() public interface IFile void
Read() public class Foo IReadable, IFile
void IReadable.Read() void
IFile.Read() Foo f f.Read()
//no IReadable r f as IReadable r.Read()
23Collections
- Some basic data structures are provided
- Listltintgt mylist Listltintgt()
- Dictionaryltint, Employeegt empTable
- Queueltstringgt priority
- Stackltchargt tokens