A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

A linguagem C#.

Apresentações semelhantes


Apresentação em tema: "A linguagem C#."— Transcrição da apresentação:

1 A linguagem C#

2 Sobre o que iremos falar...
As características mais importantes da linguagem C# Alguma comparação c/ Java e C++ Reflexão crítica: aspectos positivos e negativos

3 Porquê C# ? .Net é uma plataforma interessante
Java e C# são semelhantes Ainda mais quando sobre .Net C# é a linguagem de eleição no .Net .NET (dot-net) is the name Microsoft gives to its general vision of the future of computing, the view being of a world in which many applications run in a distributed manner across the Internet. We can identify a number of different motivations driving this vision. Firstly, distributed computing is rather like object oriented programming, in that it encourages specialised code to be collected in one place, rather than copied redundantly in lots of places. There are thus potential efficiency gains to be made in moving to the distributed model. Secondly, by collecting specialised code in one place and opening up a generally accessible interface to it, different types of machines (phones, handhelds, desktops, etc.) can all be supported with the same code. Hence Microsoft's 'run-anywhere' aspiration. Thirdly, by controlling real-time access to some of the distributed nodes (especially those concerning authentication), companies like Microsoft can control more easily the running of its applications. It moves applications further into the area of 'services provided' rather than 'objects owned'. Interestingly, in taking on the .NET vision, Microsoft seems to have given up some of its proprietary tendencies (whereby all the technology it touched was warped towards its Windows operating system). Because it sees its future as providing software services in distributed applications, the .NET framework has been written so that applications on other platforms will be able to access these services. For example, .NET has been built upon open standard technologies like XML and SOAP. At the development end of the .NET vision is the .NET Framework. This contains the Common Language Runtime, the .NET Framework Classes, and higher-level features like ASP.NET (the next generation of Active Server Pages technologies) and WinForms (for developing desktop applications). The Common Language Runtime (CLR) manages the execution of code compiled for the .NET platform. The CLR has two interesting features. Firstly, its specification has been opened up so that it can be ported to non-Windows platforms. Secondly, any number of different languages can be used to manipulate the .NET framework classes, and the CLR will support them. This has led one commentator to claim that under .NET the language one uses is a 'lifestyle choice'. Not all of the supported languages fit entirely neatly into the .NET framework, however (in some cases the fit has been somewhat Procrustean). But the one language that is guaranteed to fit in perfectly is C#. This new language, a successor to C++, has been released in conjunction with the .NET framework, and is likely to be the language of choice for many developers working on .NET applications.

4 “Hello World” using System; public class HelloWorld {
public static void Main(string[] args) Console.WriteLine(“Hello World!”); } The first thing to note about C# is that it is case-sensitive. You will therefore get compiler errors if, for instance, you write 'console' rather than 'Console'. The second thing to note is that every statement finishes with a semicolon (;) or else takes a code block within curly braces. As C# is an object-oriented language, C# programs must be placed in classes (classes are discussed in lesson 11, but if you are new to object orientation we suggest that you first read some introductory material). Line 2 above declares the class to be named 'HelloWorld'. Line 1 of the code declares we are using the System namespace (namespaces are also covered in lesson 11). The point of this declaration is mostly to save ourselves time typing. Because the 'Console' object used in line 10 of the code actually belongs to the 'System' namespace, its fully qualified name is 'System.Console'. However, because in line 1 we declare that the code is using the System namespace, we can then leave off the 'System.' part of its name within the code. When compiled and run, the program above will automatically run the 'Main' method declared and begun in line 4. Note again C#'s case-sensitivity - the method is 'Main' rather than 'main'. The statement on line 10 calls the 'WriteLine' method of the Console class in the System namespace. It should be obvious how this works in the given example - it just prints out the given string to the 'Console' (on PC machines this will be a DOS prompt). For a more complicated use of the WriteLine method, see lesson 7. In order to run it, the program above must first be saved in a file. Unlike in Java, the name of the class and the name of the file in which it is saved do not need to match up, although it does make things easier if you use this convention. In addition, you are free to choose any extension for the file, but it is usual to use the extension '.cs'. Namespaces: The namespace declaration, using System;, indicates that you are referencing the System namespace. Namespaces contain groups of code that can be called upon by C# programs. With the using System; declaration, you are telling your program that it can reference the code in the System namespace without pre-pending the word System to every reference. If you would like to call methods without typing their fully qualified name, you can implement the using directive.  In Listing 6-5, we show two using directives.  The first, using System, is the same using directive you have seen in every program in this tutorial.  It allows you to type the method names of members of the System namespace without typing the word System every time.  In myPrint(), Console is a class member of the System namespace with the method WriteLine().  Its fully qualified name is System.Console.WriteLine(...). Similarly, the using directive using csharp_station.tutorial allows us to implement members of the csharp_station.tutorial namespace without typing the fully qualified name.  This is why we can type myExample.myPrint().  Without the using directive, we would have to type csharp_station.tutorial.myExample.myPrint() every time we wanted to implement that method.

5 Semelhante a Java e C++ public class Pessoa { private string Nome;
private int Idade; public Pessoa(string nome, int idade) { this.Nome = nome; this.Idade = idade; } public void MostraInfo() { Console.WriteLine(“{0}, tem {1} anos”, Nome, Idade); C# versus Java C# and Java are both new-generation languages descended from a line including C and C++. Each includes advanced features, like garbage collection, which remove some of the low level maintenance tasks from the programmer. In a lot of areas they are syntactically similar. Both C# and Java compile initially to an intermediate language: C# to Microsoft Intermediate Language (MSIL), and Java to Java bytecode. In each case the intermediate language can be run - by interpretation or just-in-time compilation - on an appropriate 'virtual machine'. In C#, however, more support is given for the further compilation of the intermediate language code into native code. C# contains more primitive data types than Java (lesson 4), and also allows more extension to the value types. For example, C# supports 'enumerations', type-safe value types which are limited to a defined set of constant variables (lesson 7), and 'structs', which are user-defined value types (lesson 11). (Note: Java doesn't have enumerations, but there is a standard way of emulating them Unlike Java, C# has the useful feature that we can overload various operators. Like Java, C# gives up on multiple class inheritance in favour of a single inheritance model extended by the multiple inheritance of interfaces (lesson 11). However, polymorphism (lesson 14) is handled in a more complicated fashion, with base class methods either 'overriding' or 'hiding' super class methods C# also uses 'delegates' - type-safe method pointers (see lesson 16). These are used to implement event-handling. In Java, multi-dimensional arrays are implemented solely with single-dimensional arrays (where arrays can be members of other arrays. In addition to jagged arrays, however, C# also implements genuine rectangular arrays (lesson 6). C# versus C++ Although it has some elements derived from Visual Basic and Java, C++ is C#'s closest relative. In an important change from C++, C# code does not require header files. All code is written inline. As touched on above, the .NET runtime in which C# runs performs memory management, taking care of tasks like garbage collection. Because of this, the use of pointers in C# is much less important than in C++. Pointers can be used in C#, where the code is marked as 'unsafe' (lesson 5), but they are only really useful in situations where performance gains are at an absolute premium. Speaking generally, the 'plumbing' of C# types is different from that of C++ types, with all C# types being ultimately derived from the 'object' type (lesson 4). There are also specific differences in the way that certain common types can be used. For instance, C# arrays are bounds checked unlike in C++, and it is therefore not possible to write past the end of a C# array. C# statements are quite similar to C++ statements. To note just one example of a difference: the 'switch' statements has been changed so that 'fall-through' behaviour is disallowed (lesson 10). As mentioned above, C# gives up on the idea of multiple class inheritance. Other differences relating to the use of classes are: there is support for class 'properties' of the kind found in Visual Basic, and class methods are called using the . operator rather than the :: operator. Pessoa Cliente = new Pessoa(“Carlos”, 25); Cliente.MostraInfo();

6 C# - Características Linguagem OO: Sistema unificado de tipos
Muito semelhante ao C++ / Java É fácil começar a programar em C# Sistema unificado de tipos Tudo pode ser visto como um objecto Suporte directo à Programação Baseada em Componentes Propriedades, Eventos e Atributos Assemblies

7 } } Tudo é um objecto! Boxing Unboxing 10 val obj 10 k 10
int val = 10; object obj = val; int k = (int) obj; // k fica c/ valor 10 val 10 } Boxing obj System.Int32 10 C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type. Variables can hold either value types or reference types, or they can be pointers. This lesson covers the first two options; pointers are discussed in lesson 5. Here's a quick recap of the difference between value types and reference types. where a variable v contains a value type, it directly contains an object with some value. No other variable v' can directly contain the object contained by v (although v' might contain an object with the same value). where a variable v contains a reference type, what it directly contains is something which refers to an object. Another variable v' can contain a reference to the same object refered to by v. Reference Types The pre-defined reference types are object and string, where object - as we have mentioned above - is the ultimate base class of all other types. New reference types can be defined using 'class', 'interface', and 'delegate' declarations (covered in lesson 12). Reference types actually hold the value of a memory address occupied by the object they reference. Consider the following piece of code, in which two variables are given a reference to the same object (for the sake of the example, this object is taken to contain the numeric property 'myValue'). object x = new object(); x.myValue = 10; object y = x; y.myValue = 20; // after this statement both x.myValue and y.myValue equal 20 This code illustrates how changing a property of an object using a particular reference to it is reflected in all other references to it. Note, however, that although strings are reference types, they work rather more like value types. When one string is set to the value of another, eg string s1 = "hello"; string s2 = s1; Then s2 does at this point reference the same string object as s1. However, when the value of s1 is changed, for instance with s1 = "goodbye"; what happens is that a new string object is created for s1 to point to. Hence, following this piece of code, s1 equals "goodbye", whereas s2 still equals "hello". The reason for this behaviour is that string objects are 'immutable'. That is, the properties of these objects can't themselves change. So in order to change what a string variable references, a new string object must be created. Boxing C# allows you convert any value type to a corresponding reference type, and to convert the resultant 'boxed' type back again. The following piece of code demonstrates boxing. When the second line executes, an object is initiated as the value of 'box', and the value held by i is copied across to this object. It is interesting to note that the runtime type of box is returned as the boxed value type; the 'is' operator thus returns the type of box below as 'int'. int i = 123; object box = i; if (box is int) {Console.Write("Box contains an int");} // this line is printed } Unboxing k 10

8 Tudo é um objecto (2) C# O fim das classes de wrapping! JAVA
Hashtable ht = new Hashtable(); ht.Add(1, “Xpto”); ht.Add(2, “Xpti”); JAVA Hashtable ht = new Hashtable(); ht.put(new Integer(1), “Xpto”); ht.put(new Integer(2), “Xpti”); O fim das classes de wrapping!

9 Tudo é um objecto (3) Tudo deriva de System.Object Métodos: Equals
GetType ToString Finalize MemberwiseClone GetHashCode

10 C# base Controlo de execução if, for, do, while, switch, foreach...
switch sem fall-through: foreach: switch a { case 2: x = 4; goto case 3 // fall-through explícito case 3: ... Foreach #1 The 'foreach' loop is used to iterate through the values contained by any object which implements the IEnumerable interface. When a 'foreach' loop runs, the given variable1 is set in turn to each value exposed by the object named by variable2. As seen above, such loops can be used to access array values. The main drawback of 'foreach' loops is that each value extracted (held in the given example by the variable 'b') is read-only. Foreach. #2 A foreach loop is used to iterate through the items in a list. It operates on arrays or collections such as ArrayList, which can be found in the System.Collections namespace. The syntax of a foreach loop is foreach (<type> <item name> in <list>) { <statements> }. The type is the type of item contained in the list. For example, if the type of the list was int[] then the type would be int. The item name is an identifier that you choose, which could be anything but should be meaningful. For example, if the list contained an array of people's ages, then a meaningful name for item name would be age. The in keyword is required. As mentioned earlier, the list could be either an array or a collection, as defined by types found in the System.Collections namespace. You learned about arrays in Lesson 02: Operators, Types, and Variables. If you aren't familiar with collections, open the .NET Framework SDK documentation and look for the System.Collections namespace to see what types are available. While iterating through the items of a list with a foreach loop, the list is read-only. This means that you can't change the value of any items in the list within a foreach loop. On each iteration through a foreach loop the list is queried for a new value. As long as the list can return a value, this value will be put into the read-only item name variable, causing the statements in the foreach block to be executed. When the collection has been fully traversed, control will transfer to the first executable statement following the end of the foreach block. Listing 4-4 demonstrates how to use a foreach loop. int[] tabela = {1, 2, 3}; foreach (int i in tabela) Console.WriteLine(“{0}”, i);

11 Sistema de Tipos string Nome; int[] tabela; class Pessoa { ... }
decimal conta; enum Estado { Ligado, Desligado} struct Ponto { int x; int y; } Array Object Class Enum ValueType Struct Em C++ uma struct é um reference type com herança acesso público Em C# são um value type A struct allows you to create new value-type objects that are similar to the built-in types (int, float, bool, etc.).  When would you use a struct instead of a class?  Think about how the built-in types are used.  They have values and distinct operations to manipulate those values.  If you have a need to create an object that behaves in this manner, consider implementing it as a struct.  It does not need to be instantiated with a new operator, like a class.  When this occurs, the struct is implicitly instantiated with its default (or parameterless) constructor.  The parameterless constructor initializes all struct fields to default values.  i.e. integrals are 0, floating points are 0.0, and booleans are false.  It's illegal to define a parameterless constructor for a struct. Another difference between structs and classes is that structs can not have destructors.  Also, structs cannot inherit another class or struct or be inherited from.  However, a struct may inherit multiple interfaces.  An interface is a C# reference type with members that do not have implementations.  Any class or struct inheriting an interface must implement every one of that interface's methods.  Interfaces are a subject for a later lesson. Reference Types The pre-defined reference types are object and string, where object - as we have mentioned above - is the ultimate base class of all other types. New reference types can be defined using 'class', 'interface', and 'delegate' declarations (covered in lesson 12). int short byte char float decimal long uint ulong sbyte ushort bool double

12 Passagem de parâmetros
ref – passagem de tipos-valor por referência {... char c=‘c’; g(ref c); } out - passagem de tipos-valor não inicializados por referência: int x; f(out x); void g(ref char pc) { pc =‘x’; } The addAddress() method takes a ref parameter.  This means that a reference to the parameter is copied to the method.  This reference still refers to the same object on the heap as the original reference used in the caller's argument.  This means any changes to the local reference's object also changes the caller reference's object.  The code can't change the reference, but it can make changes to the object being referenced.  You can think of this as a way to have an input/output parameter.  out parameters are for those parameters that are only returned from a method.  This is more efficient because the program doesn't have the overhead of copying the parameter in to the method.  On a normal program, this overhead is minimal.  However, in distributed processing scenarios, where network communication is the bottleneck, out parameters make a larger difference.  out parameters are only passed back to the calling function.  Because of definite assignment rules, you cannot use this variable until it has a valid value assigned.  Once assigned and the program returns, the value of the out parameter will be copied into the caller's argument variable.  You must assign a value to an out parameter before your method returns. Where a method parameter is defined (and invoked) using the 'out' modifier, it is passed by reference. The difference between the 'out' and the 'ref' modifier is this: a parameter modified by the 'out' keyword need not be assigned a value before being passed into the method, but must be assigned a value in the method. The reason that one might use output parameters is to return multiple values from a method. For instance, in the following code an integer and a boolean is passed to the 'change' method. This method sets the boolean to indicate whether or not the integer is greater than 0, and returns the value of the integer doubled. void f(out int x) { x=2; }

13 Passagem de parâmetros
params – passagem de nº variável de parâmetros public static void Main() { UseParams(1, 'a', "test"); int[] myarray = new int[3] {10,11,12}; UseParams(myarray); } public static void UseParams(params object[] list) { for ( int i = 0 ; i < list.Length ; i++ ) Console.WriteLine(list[i]); Console.WriteLine(); }

14 Herança Por omissão, os métodos não são virtuais! public class Pessoa
{ private string Nome; private int Idade; public Pessoa(string nome, int idade) { this.Nome = nome; this.Idade = idade; } public virtual void MostraInfo() { Console.WriteLine(“{0}, tem {1} anos”, Nome, Idade); Por omissão, os métodos não são virtuais! public class Pessoa { private string Nome; private int Idade; public Pessoa(string nome, int idade) { this.Nome = nome; this.Idade = idade; } public void MostraInfo() { Console.WriteLine(“{0}, tem {1} anos”, Nome, Idade);

15 Herança Por omissão, os métodos não são virtuais!
public class Empregado : Pessoa { private string Empresa; public Empregado(string nome, int idade, int empresa) : base(nome, idade) this.Empresa = empresa; } public override void MostraInfo() { base.MostraInfo(); Console.WriteLine(“Empresa: {0}”, Empresa); Por omissão, os métodos não são virtuais!

16 Binding estático vs. dinâmico
public class Base { void Xpto() { Console.WriteLine(“Base.Xpto()”); } virtual void Xpti() { Console.WriteLine(“Base.Xpti()”); } } public class Derivada : Base { new void Xpto() { Console.WriteLine(“Deriv.Xpto()”); } override void Xpti() { Console.WriteLine(“Deriv.Xpti()”); } } Derivada d = new Derivada(); d.Xpto(); // Resulta em “Deriv.Xpto()” d.Xpti(); // Resulta em “Deriv.Xpti()” Base dref = d; dref.Xpto(); // Resulta em “Base.Xpto()” dref.Xpti(); // resulta em “Deriv.Xpti()” The override modifier allows a method to override the virtual method of its base class at run-time.  The override will happen only if the class is referenced through a base class reference.  Overriding methods must have the same signature, name and parameters, as the virtual base class method it is overriding. Notice the new modifier on the Child class print() method.  This enables this method to hide the Parent class print() method, thus explicitly preventing polymorphism.  Without the new modifier, the compiler will produce a warning to draw your attention to this.  See the next lesson for a detailed discussion of polymorphism. As we have seen previously, classes take on the methods of the classes from which they inherit. In some cases, however, a class may want to 'overwrite' such a method. C# supports two different ways of method overwriting - 'hiding' or 'overriding'. Method overwriting makes use of the following three method-head keywords (see lesson 13): new, virtual, override The main difference between hiding and overriding relates to the choice of which method to call where the declared class of a variable is different to the run-time class of the object it references. This point is explained further below. Method Overriding Suppose that we define a Square class which inherits from a Rectangle class (a square being a special case of a rectangle). Each of these classes also specifies a 'getArea' instance method, returning the area of the given instance. For the Square class to 'override' the Rectangle class' getArea method, the Rectangle class' method must have first declared that it is happy to be overridden. One way in which it can do this is with the 'virtual' keyword. To override this method the Square class would then specify the overriding method with the 'override' keyword. Note that for one method to override another, the overridden method must not be static, and it must be declared as either 'virtual', 'abstract' or 'override'. Furthermore, the access modifiers for each method must be the same. The major implication of the specifications above is that if we construct a new Square instance and then call its 'getArea' method, the method actually called will be the Square instance's getArea method. There is, however, a more subtle point. To show this, suppose that we declare two variables in the following way: Square sq = new Square(4); Rectangle r = sq; Here variable r refers to sq as a Rectangle instance (possible because the Square class derives from the Rectangle class). We can now raise the question: if we run the following code double area = r.getArea(); then which getArea method is actually called - the Square class method or the Rectangle class method? The answer in this case is that the Square class method would still be called. Because the Square class' getArea method 'overrides' the corresponding method in the Rectangle class, calls to this method on a Square instance always 'slide through' to the overriding method. Method Hiding Where one method 'hides' another, the hidden method does not need to be declared with any special keyword. Instead, the hiding method just declares itself as 'new'. So, where the Square class hides the Rectangle class' getArea method, the two methods might just be written thus: public double getArea() // in Rectangle {    return length * width; } public new double getArea() // in Square {  return length * length; } Note that a method can 'hide' another one without the access modifiers of these methods being the same. So, for instance, the Square's getArea method could be declared as private, viz: private new double getArea() {   return length * length;} This leads us to an important point. A 'new' method only hides a super-class method with a scope defined by its access modifier. Specifically, where the access level of the hiding method is 'private', as in the method just described, this method only hides the super-class method for the particular class in which it is defined. To make this point more concrete, suppose that we introduced a further class, SpecialSquare, which inherits from Square. Suppose further that SpecialSquare does not overwrite the getArea method. In this case, because Square's getArea method is defined as private, SpecialSquare inherits its getArea method directly from the Rectangle class (where the getArea method is public). The final point to note about method hiding is that method calls do not always 'slide through' in the way that they do with virtual methods. So, if we declare two variables thus: Square sq = new Square(4); Rectangle r = sq; then run the code the getArea method run will be that defined in the Rectangle class, not the Square class.

17 Interfaces interface ILeitorCD { void TocaCD(CD cd); }
public class Aparelhagem : ILeitorCD { void TocaCD(CD cd) { ... } Aparelhagem sony = new Aparelhagem(); Computador pc = new Computador(); ILeitorCD leitor; leitor = sony; leitor.tocaCD(GreatestHits); leitor = pc; public class Computador : ILeitorCD { void TocaCD(CD cd) { ... } An interface looks like a class, but has no implementation.  The only thing it contains are definitions of events, indexers, methods and/or properties.  The reason interfaces only provide definitions is because they are inherited by classes and structs, which must provide an implementation for each interface member defined. So, what are interfaces good for if they don't implement functionality?  They're great for putting together plug-n-play like architectures where components can be interchanged at will.  Since all interchangeable components implement the same interface, they can be used without any extra programming.  The interface forces each component to expose specific public members that will be used in a certain way. Because interfaces must be defined by inheriting classes and structs, they define a contract.  For instance, if class foo inherits from the IDisposable interface, it is making a statement that it guarantees it has the Dispose() method, which is the only member of the IDisposable interface.  Any code that wishes to use class foo may check to see if class foo inherits IDisposable.  When the answer is true, then the code knows that it can call foo.Dispose(). 

18 Redefinição de Operadores
Em C# é possível redefinir os operadores existentes. Lista A = new Lista(); Lista B = new Lista(); A.Add(1); A.Add(2); B.Add(3); B.Add(4); Lista C = A + B; // Junta ambas as listas Overloading operators To overload an operator in a class, one defines a method using the 'operator' keyword. For instance, the following code overloads the + operator

19 Redefinição de Operadores (2)
Lista A = new Lista(); Lista B = new Lista(); ... Lista C = A + B; public class Lista { private object[] Elementos; ... public static Lista operator+(Lista a, Lista b) Lista resultado = new Lista(); // Copia os elementos de <a> e <b> para // a lista <resultado> return resultado; }

20 Unsafe Code O C# suporta elementos avançados, como a utilização de ponteiros Sempre que são utilizados estes elementos, o código tem de ser colocado dentro de um contexto unsafe int total = 0; unsafe { int* ptr = &total; *ptr = 10; } public unsafe void FastCopy(byte* dst, byte* src, int count) { for (int i=0; i<count; i++) *dst++ = *src++; } Unsafe Code A major problem with using pointers in C# is that C# operates a background garbage collection process. In freeing up memory, this garbage collection is liable to change the memory location of a current object without warning. So any pointer which previously pointed to that object will no longer do so. Such a scenario leads to two potential problems. Firstly, it could compromise the running of the C# program itself. Secondly, it could affect the integrity of other programs. Because of these problems, the use of pointers is restricted to code which is explicitly marked by the programmer as 'unsafe'. Because of the potential for malicious use of unsafe code, programs which contain unsafe code will only run if they have been given full trust. To address the problem of garbage collection, one can declare a pointer within a 'fixed' expression. This 'pins' the location of the type pointed to - the memory location of the type therefore remains static, safe from garbage collection. Note that the fixed statement can only be used within the context of unsafe code. There is a further quirk to learn. Any value types declared within unsafe code are automatically 'fixed', and will generate compile-time errors if used within fixed expressions. The same is not true of reference types, however (for the difference between value and reference types see lesson 4).

21 C# vs. C++ GC destrói objectos inacessíveis
Tipos de referência e tipos-valor Boxing, unboxing Redefinição de métodos tem de ser explícita

22 C# vs. C++ (2) boolean não são inteiros switch sem fall-through
Não se podem usar variáveis sem atribuição (out) Não há métodos globais

23 C# vs Java Só há um Main Várias classes num ficheiro
namespaces em vez de packages

24 C# vs Java (2) Goto foreach Redefinição de operadores Enumerações
Tipos-valor e Boxing Código inseguro (unsafe) ref serve para passar por referência

25 Documentação em XML /// <summary>
/// Este método calcula o ordenado de uma pessoa, /// baseado nos seus dias de trabalho. /// </summary> /// /// <param name=“diasTrabalho"> /// O número de dias que trabalhou. /// </param> /// <returns> O salário da pessoa. </returns> public int CalculaOrdenado(int diasTrabalho) { ... } The C# compiler supports the automatic creation of class documentation. Where the equivalent functionality for Java produces HTML, the C# documenter produces XML. This means that the C# documentation is not as immediately ready to use as the Java documentation. However, it does allow there to be different applications which can import and use the C# documentation in different ways. (Note: using Visual Studio you can also create HTML documentation, but we will not be covering this here). Sadly, Microsoft did not bundle a basic documentation reader with C#.. Even worse, however, the documentation automatically produced by C# is rather less extensive than that produced by Java’s javadoc tool. C# Documentation Comments To document any element in a C# script, you precede the element with XML elements. Each of the lines comprising this documentary code should be marked off as comments using the following special comment indicator (you can compare this with the standard comment indicators in Lesson 3) /// The above code gives an example of how one can provide overview information about a class. You are at liberty to use any XML tags you wish to document the code – as long as they follow the XML syntax then the compiler will happily write them into the documentation. But Microsoft does provide a list of recommended XML elements for you to use. Some of these elements indicate the type of documentary information that is being given, and the compiler will validate certain aspects of these. Other elements are just used to give layout or formating information. The following lists describe the main documentation elements provided. Note that the content of each element should be written between its opening and closing tags, and some of the tags also take further attributes. In particular, the ‘cref’ attribute can supposedly be used in any element, but we have just used it in the cases where it seems particularly appropriate. Tag(s)Description<summary>- holds overview information about any documentable element.<remarks>- allows for expanded comments about any documentable element, following summary information.Note: we still aren’t sure if the descriptions of the tags above are correct. The following points describe the problem. In favour of using the 'summary' and 'remarks' in the suggested way is the fact that Gunnarson, who helped create C#, sets things out in this way. It also correlates with the default behaviour of Visual Studio.NET, where ‘summary’ tags are given by default whenever you start documenting any element. On the other hand, in the help files of the (production) version of .NET – v – it explicitly states that the use of ‘summary’ is to hold overview information about a class member, whereas the use of ‘remarks’ is to hold overview information about a class. However, some of the examples given throughout these same help files conflicts with this advice - for example, in the description of the ‘paramref’ element, a class method is documented only with ‘remarks’. Unfortunately, of course, this example also conflicts with what we say, since the example contains no ‘summary’ tag. Basically, it’s all been knocked together by a committee of rather befuddled monkeys. But the way we suggest is as good as any. Tag(s)Description<param name="name">- describes a parameter passed to a method. The compiler checks that the ‘name’ value matches an actual parameter in the code. Also, if you give documentation for one parameter value, the compiler will expect you to give documentation for them all.<paramref name="name">- identifies the mention of a parameter name within some other descriptive element, for instance within ‘summary’ tags. The idea is that this mentioned name can be styled differently from the surrounding text. The compiler checks that the ‘name’ value matches an actual parameter in the code.<returns>- describes the return value for a method. As the descriptive field is just free text there is no compiler checking. <exceptions cref="type">- describes an exception that may be thrown by a method. The ‘cref’ attribute refers to a type or field (such as System.Exception), and the compiler checks that this reference is available from the current compilation environment.<permission cref="type">- describes a permission requirement for a type or member. The cref attribute refers to a type or field (such as System.Security.PermissionSet), and the compiler checks that this reference is available from the current compilation environment.<value>- describes a class property.<example>- gives an example of the use of the referenced object (for example a class method). The ‘example’ element is often used with the following two elements.<c>- marks up a single phrase or line as a code example. Generally used in conjuction with the ‘example’ element.<code>- marks up multiple lines as code examples. Generally used in conjuction with the ‘example’ element.<see cref ="type">- used to identify a cross-reference in the documentation; designed to be used inline within a description element. The cref attribute refers to a type or field, and the compiler checks that this reference is available from the current compilation environment. and the see also tag is used in a separate section. This allows the documentation to create cross-references.<seealso cref ="type">- used to identify a cross-reference in the documentation; different from ‘see’ in that it is designed to stand alone. The cref attribute refers to a type or field, and the compiler checks that this reference is available from the current compilation environment.The following elements are just used to provide layout information: <para>- used within descriptive tags like ‘remarks’, ‘summary’, etc. to wrap a single paragraph.<list type = ”bullet” | “number” | “table”>- top level tags for a list, where this may be one of the three types shown.There are more elements associated with the list tag. Generating C# Documentation You tell the compiler to produce documentation when compiling by invoking it with the switch: /doc:file In this switch, file represents the name of the file that you want the documentation written to. As the documentation is generated in xml format, the file should have the extension .xml. So, for instance, to produce the documentation for a program in sys.cs file in a file named my.xml, we would use the command: csc sys.cs /doc:my.xml Those working with Visual Studio .NET should set the ‘XML Documentation File’ property in the ‘Build’ directory of the project’s Configuration Properties.

26 Exercício 1 Abra o VisualStudio.Net
Construa a aplicação “Hello World” como Console Application Utilize o método WriteLine da classe Console Construa uma aplicação semelhante, agora como Windows Application Utilize o método Show da classe MessageBox

27 Exercício 2 Crie um novo projecto C# de Windows Application
Adicione um novo ficheiro de classes (.cs) Defina a interface IListaNomes, que deverá incluir interfaces para: Adicionar novo nome à lista de nomes Retornar a listagem de todos os nomes presentes na lista de nomes, sob a forma de uma cadeia de caracteres Limpar o conteúdo da lista de nomes

28 Exercício 2 (continuação)
Crie uma classe que implemente IListaNomes e crie uma instância dessa classe na sua Windows Form. (Utilize a classe ArrayList para guardar os nomes) Construa a Windows Form por forma a utilizar todas as funcionalidades fornecidas pela interface IListaNomes Coloque breakpoints em cada método da classe e verifique que cada método da mesma é chamado e executado correctamente


Carregar ppt "A linguagem C#."

Apresentações semelhantes


Anúncios Google