Kurt Nørmark ©
Department of Computer Science, Aalborg University, Denmark
Abstract Previous lecture Next lecture Index References Contents | In this lecture we will introduce the programming language C#. Recall that we assume that the reader has experience with C, or at least a similar imperative language. In the central part of this lecture we will address how C# differs from C. This is done by 'a tour through C' in which we explain the major similarities and differences between the two languages. |
The C# Language and System |
C# seen in a historic perspective Slide Annotated slide Contents Index References Textbook |
|
|
|
The Common Language Infrastructure Slide Annotated slide Contents Index References Textbook |
|
|
|
CLI Overview from Wikipedia Slide Annotated slide Contents Index References Textbook |
Figure. Wikipedia's overview diagram of the CLI |
C# Compilation and Execution Slide Annotated slide Contents Index References Textbook |
|
|
|
C# in relation to C |
Simple types Slide Annotated slide Contents Index References Textbook | This page is about integers, real numbers, characters, and booleans. |
|
Differences compared to C. |
Program: Demonstrations of the simple type bool in C#. Shows boolean constants and how to deal with the default boolean value (false). |
|
Program: Demonstrations of the simple type char in C#. Illustrates character constants, hexadecimal escape notation, and character methods. |
|
Program: Demonstrations of numeric types in C#. Illustrates all numeric types in C#. Exercises minimum and maximum values of the numeric types. |
|
Program: Output from the numeric demo program. Output from the program above. Reveals minimum and maximum values of all the numeric types. |
|
Exercise 2.3. Exploring the type Char | The type System.Char (a struct) contains a number of useful methods, and a couple of constants. Locate the type System.Char in your C# documentation and take a look at the methods available on characters. You may ask where you find the C# documentation. There are several possibilities. You can find it at the Microsoft MSDN web site at msdn.microsoft.com. It is also integrated in Visual Studio and - to some degree - in Visual C# express. It comes with the C# SDK, as a separate browser. It is also part of the documentation web pages that comes with Mono. If you are a Windows user I will recommend the Windows SDK Documentation Browser which is bundled with the C# SDK. Along the line of the character demo program above, write a small C# program that uses the char predicates IsDigit, IsPunctuation, and IsSeparator. It may be useful to find the code position - also known as the code point - of a character. As an example, the code position of 'A' is 65. Is there a method in System.Char which gives access to this information? If not, can you find another way to find the code position of a character? Be sure to understand the semantics (meaning) of the method GetNumericValue in type Char. |
Exercise 2.3. Hexadecimal numbers | In this exercise we will write a program that can convert between decimal and hexadecimal notation of numbers. Please consult the focus boxes about hexadecimal numbers in the text book version if you need to. You might expect that this functionality is already present in the C# libraries. And to some degree, it is. The static method ToInt32(string, Int32) in class Convert converts the string representation of a number (the first parameter) to an arbitrary number system (the second parameter). Similar methods exist for other integer types. The method ToString(string) in the struct Int32, can be used for conversion from an integer to a hexadecimal number, represented as a string. The parameter of ToString is a format string. If you pass the string "X" you get a hexadecimal number. The program below shows examples:
Now, write a method which converts a list (or array) of digits in base 16 (or more generally, base b, b >= 2) to a decimal number. The other way around, write a method which converts a positive decimal integer to a list (or array) of digits in base 16 (or more generally, base b). Here is an example where the requested methods are used:
|
Enumerations types Slide Annotated slide Contents Index References Textbook | Enumeration types provide for symbolic names of selected integer values. Use of enumeration types improves the readability of your programs. |
|
Program: Two examples of enumeration types in C#. |
|
|
Program: Demonstration of enumeration types in C#. Programming with Ranking and OnOff. |
|
Program: Output from the program that demonstrates enumeration types. |
|
Exercise 2.5. ECTS Grades | Define an enumeration type ECTSGrade of the grades A, B, C, D, E, Fx and F and associate the Danish 7-step grades 12, 10, 7, 4, 2, 0, and -3 to the symbolic ECTS grades. What is the most natural underlying type of ECTSGrade? Write a small program which illustrates how to use the new enumeration type. |
Exercise 2.5. Use of Enumeration types | Consult the documentation of type type System.Enum, and get a general overview of the methods in this struct. Be sure that you are able to find the documentation of System.Enum Test drive the example EnumTest, which is part of MicroSoft's documentation. Be sure to understand the program relative to its output. Write your own program with a simple enumeration type. Use the Enum.CompareTo method to compare two of the values of your enumeration type. |
Non-simple types Slide Annotated slide Contents Index References Textbook | This is about arrays, strings, structs. All the classes and structs that we program ourselves also count as non-simple types. |
|
|
|
|
|
|
Arrays and Strings Slide Annotated slide Contents Index References Textbook | Both arrays and strings are classical types, supported by almost any programming language. Both arrays and strings are reference types. It means that arrays and strings are accessed via references. |
|
|
|
Program: Demonstrations of array types in C#. |
|
Program: Output from the array demonstration program. |
|
Program: A demonstration of strings in C#. |
|
Program: Output from the string demonstration program. |
|
Exercise 2.7. Use of array types | Based on the inspiration from the accompanying example, you are in this exercise supposed to experiment with some simple C# arrays. First, consult the documentation of the class System.Array. Please notice the properties and methods that are available on arrays in C#. Declare, initialize, and print an array of names (e.g. array of strings) of all members of your group. Sort the array, and search for a given name using System.Array.BinarySearch method. Reverse the array, and make sure that the reversing works. |
Exercise 2.7. Use of string types | Based on the inspiration from the accompanying example, you are in this exercise supposed to experiment with some simple C# strings. First, consult the documentation of the class System.String - either in your documentation browser or at msdn.microsoft.com. Read the introduction (remarks) to string which contains useful information! There exists a large variety of operations on strings. Please make sure that you are aware of these. Many of them will help you a lot in the future! Make a string of your own first name, written with escaped Unicode characters (like we did for "OOP" in the accompanying example). If necessary, consult the unicode code charts (Basic Latin and Latin-1) to find the appropriate characters. Take a look at the System.String.Insert method. Use this method to insert your last name in the first name string. Make your own observations about Insert relative to the fact that strings in C# are immutable. |
|
|
Pointers and references Slide Annotated slide Contents Index References Textbook | References are important in C#. A reference is similar to a point in C, but references are much easier to work with. References can be passed around (via parameters). Objects are always accessed via references, but in contrast to C, the following of a references happens implicitly, and automatically. |
|
|
Program: Demonstrations of references in C#. |
|
Program: Output from the reference demo program. |
|
|
Structs Slide Annotated slide Contents Index References Textbook | Structs in C and C# are similar to each other. But structs in C# are extended in several ways compared to C. |
|
|
Program: Demonstrations of structs in C#. |
|
Program: Output from the struct demo program. |
|
Program: An extended demonstration of structs in C#. |
|
Operators Slide Annotated slide Contents Index References Textbook | This pages shows an overview of all operators in C#. |
|
Table. The operator priority table of C#. Operators with high level numbers have high priorities. In a given expression, operators of high priority are evaluated before operators with lower priority. The associativity tells if operators at the same level are evaluated from left to right or from right to left. |
|
|
|
Commands and Control Structures Slide Annotated slide Contents Index References Textbook | On this page we discuss control structures for selection and iteration. As usual, we concentrate on the differences between C an C#. |
|
|
|
Program: Demonstrations of definite assignment. |
|
Program: Demonstrations of if. |
|
Program: Demonstrations of switch. |
|
Program: Demonstrations of foreach. |
|
Program: Demonstrations of try catch. |
|
Functions Slide Annotated slide Contents Index References Textbook | On this page we will look at parameter passing techniques. C only supports call by value. Call by reference in C is in reality call by value passing of pointers. C# offers a variety of different parameter passing modes. We also discuss overloaded functions - functions of the same name distinguished by parameters of different types. |
|
|
|
Program: Demonstration of simple functions in C#. |
|
Program: Demonstration of parameter passing in C#. |
|
Program: Demonstration of overloaded methods in C#. |
|
Input and output Slide Annotated slide Contents Index References Textbook | Input and output (IO) is handled by a number of different classes in C#. In both C and C# there very few traces of IO in the languages as such. |
|
|
|
Program: Demonstrations of Console output in C#. |
|
Program: Output from the output demo program. |
|
Program: Demonstrations of Console input in C#. |
|
Program: A sample dialog with the Console IO demo program. Input is shown in bold style. Boldface items represent the input entered by the user of the program. |
|
|
Comments Slide Annotated slide Contents Index References Textbook |
|
|
|
C# in relation to Java |
C# versus Java Slide Annotated slide Contents Index References Textbook |
|
|
|
Types Slide Annotated slide Contents Index References Textbook |
|
Operations Slide Annotated slide Contents Index References Textbook |
|
Other substantial differences Slide Annotated slide Contents Index References Textbook |
|
C# in relation to Visual Basic |
The Overall Picture Slide Annotated slide Contents Index References Textbook |
Program: Typical overall program structure of a Visual Basic Program. |
|
Program: Typical overall program structure of a C# Program. |
|
The Overall Picture Slide Annotated slide Contents Index References Textbook |
|
Declarations and Types Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with a number of variable declarations. |
|
Program: The similar C# program with a number of variable declarations. |
|
Declaration and Types Slide Annotated slide Contents Index References Textbook |
|
|
Expressions and Operators Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with expressions and operators. |
|
Program: The similar C# program with a number of expressions and operators. |
|
Program: A Visual Basic Program with expressions and operators. |
|
Program: The similar C# program with a number of expressions and operators. |
|
Program: A Visual Basic Program with expressions and operators. |
|
Program: The similar C# program with a number of expressions and operators. |
|
Expressions and Operators Slide Annotated slide Contents Index References Textbook |
|
|
|
Control Structures for Selection Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with an If Then Else control structure. |
|
Program: The similar C# program with an if else control structure. |
|
Program: A Visual Basic Program with a Select control structure. |
|
Program: The similar C# program with a switch control structure. |
|
Control structures for Selection Slide Annotated slide Contents Index References Textbook |
|
Control Structures for Iteration Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with a while loop. |
|
Program: The similar C# program with a while loop. |
|
Program: A Visual Basic Program with a for loop. |
|
Program: The similar C# program with a for loop. |
|
Program: A Visual Basic Program with a Do Loop. |
|
Program: The similar C# program with a similar for and a break. |
|
Control structures for iteration Slide Annotated slide Contents Index References Textbook |
|
Arrays Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with an array of 10 elements. |
|
Program: The similar C# program with an array of 10 elements. |
|
Arrays Slide Annotated slide Contents Index References Textbook |
|
Procedures and Functions Slide Annotated slide Contents Index References Textbook |
Program: A Visual Basic Program with a procedure - Subprogram. |
|
Program: The similar C# program with void method. |
|
Program: A Visual Basic Program with a function. |
|
Program: The similar C# program with int method. |
|
Procedures and Functions Slide Annotated slide Contents Index References Textbook |
|
Combined C# and Visual Basic Programming Slide Annotated slide Contents Index References |
|
Program: A class Die programmed in C#. |
|
Program: A client of class Die programmed in Visual Basic. |
|
Program: Compilation and execution. |
|
Object-oriented programming in Visual Basic Slide Annotated slide Contents Index References Textbook |
|
C# Tools and IDEs |
C# Tools on Windows Slide Annotated slide Contents Index References Textbook |
|
|
|
|
C# Tools on Unix Slide Annotated slide Contents Index References Textbook |
|
|
|
Documentation Tools Slide Annotated slide Contents Index References Textbook |
|
|
Collected references Contents Index |
|
Chapter 2: Introduction to C#
Course home Author home About producing this web Previous lecture (top) Next lecture (top) Previous lecture (bund) Next lecture (bund)
Generated: February 7, 2011, 12:12:17