Programming C#

Florian Rappl, Department of Theoretical Physics, University of Regensburg

Programming C#

Day 1: Basic concepts, types, arrays, properties and Console projects

Content

  • The Microsoft Visual Studio
  • Basic concepts and syntax
  • The C# type system
  • Arrays, Lists, Dictionaries
  • Constants and properties
  • C# projects and debugging
  • Writing and running console projects

Micosoft Visual Studio (VS)

  • Multiple ways to write C#
  • The most elegant and efficient is probably using VS
  • CIP Pool has version 2010 (2012 would be better)
  • Integrated project management, debugging, profiling, ...
  • Most important: File, New Project, C#, Console project

Basic concepts of C# (pt. 1)

  • C# (out-of-the-box) is not native and not interpreted
  • Programs are saved in an intermediate language
  • That makes C# universal (same concept as Java)
  • C# is also managed code, i.e. no control over memory
  • C# is a strongly-typed language with a static type system
  • This means that types must be identified at compilation
  • Encapsulation required, i.e. every function is assigned to a class

How Garbage Collection works

Basic concepts of C# (pt. 2)

  • Just two datatypes: struct and class
  • Structures are value types (only copies are passed)
  • Classes are reference types (references are passed)
  • The concept of a pointer is embedded in the language
  • Passing structures by reference is possible with the ref keyword
  • Another way is the out keyword (enforces assignment)
  • Every object inherits at least from another object called object

Basic concepts of C# (pt. 3)

  • The syntax is quite close to C++ / Java
  • Datatypes have to be specified
  • Example: Due to encapsulation all console related functions can be found in the static class Console, which is located in the System namespace
  • Why static class? Closest to C printf(), scanf(), ...
  • Namespaces guarantee overview and unique naming

What are namespaces?

→ Example - Hello World!

HelloWorld.cs

Syntax of C# (pt. 1)

  • Example is quite close to C++
  • Differences: using for embedding namespaces
  • The main method has to be specified differently
  • We have static void Main(string[] args)
  • Array datatype has already a field to specify the length
  • No header files and macros
  • if, else, for, ... as in C++
  • Additionally we have foreach and lock

Syntax of C# (pt. 2)

  • The dot operator lets use access the members of an object
  • Constants need to be defined using const and a type
  • Only structures can be constants, otherwise use readonly
  • Arrays are defined different than in C/C++
  • However, accessing arrays is the same as in C/C++
  • Several literals for creating types, like "hello" is a string type, 'C' is a new char, 4 a new integer and 4.0 a new double

→ Example - Control flow

ControlFlow.cs Foreach.cs

Datatypes

  • Some basic structures (size in bytes): char (2), short (2), int (4), long (8), float (4), double (8), decimal (16), bool (1)
  • Some important classes: string, Array, ArrayList, Stack, Queue
  • We have enum, struct, class, interface, delegate to create new data types
  • An enumeration is a collection of constants
  • A delegate is a managed function pointer

Operators

  • C# supports the same standard operators as C++
  • Overloading is only possible for certain operators
  • The execution hierachy is determined by the operator
  • Changing the hierachy is possible by using brackets
  • A selection of operators: +, -, *, /, %, !, <, >, ==, !=, <<, >>, ...

→ Example - Datatypes and operators

DataTypes.cs

Arrays

  • A simple array of integers: int[] myarray = new int[5];
  • Initialization char[] arr = new char[] { 'a', 'b', 'c' }
  • Modification arr[2] = 'd' (0-based !)
  • More-dimensional: double[,] matrix = new double[5,5];
  • Access Console.WriteLine("M_33 = " + matrix[2,2])
  • It is also possible to use jagged arrays like so:
    double[][] matrix = new double[5][];
    matrix[0] = new double[5]; // ...
    

Collections

  • The .NET-Framework contains a huge number of collections
  • Collections go beyond the fixed limits of arrays
  • With OOP it is easily possible to create own implementations
  • Some collections are obvious (Dictionaries, Hashtables, Lists) others more specialized (Stack, Queue)
  • The ArrayList object is one of the simplest implementations
  • It takes only types of type object

→ Example - Arrays and collections

Arrays.cs

Properties (pt. 1)

  • Variables should usually be only modified within a class
  • However, what if we want read access from outside?
  • Solution C++: Create method with a name starting with Get
  • Solution C#: (other methods see it like a variable)
    int myVariable;
    public int MyVariable { get { return myVariable; } }
  • This is called a property (possible: get and set)

Properties (pt. 2)

public int MyVariable { 
	get { return myVariable; } 
	set { myVariable = value; /* More code ? */ } 
}
  • The big advantage in the set part - we can validate values, invoke events or function calls and control the state of our classes
  • Less code maintenance required

→ Example - Properties

Properties.cs

Debugging

  • Debugging is great with the VS
  • Remember: F5 to start debugging
  • CTRL+F5 to start the project
  • Insert breakpoints to see what's going on
  • While debugging: F10 to step over, F11 to step into
  • Step out of the current function with SHIFT+F11
  • More possibilites (watches, locals, interaction, ...)

Console projects

  • No GUI, just the plain Windows command line
  • All possibilities given in the Console class
  • Possible to pass arbitrary objects to Console.Write()
  • Reason: Every class inherits from object and has ToString()
  • Possibility of formatting strings with placeholders {0} etc.
  • Use ReadLine() to read string until RETURN

All available presentations

More questions? Just mail!

MVP

Florian Rappl, MVP Visual C#