Escolar Documentos
Profissional Documentos
Cultura Documentos
NET (1)
They serve for teaching purposes only and only for the students that
are registered in CSE4413 and should not be published as a book
or in any form of commercial product, unless written permission is
obtained from each of the above listed names and/or organizations.
1
Building non-windows applications (programs that only
output to the command line and contain no GUI
components).
2
Or can use any text editor and type your program (good
for when new to programming in general … but not really for now)
3
The final program
4
Compile and Run
Run
Compile (and
produce .exe)
5
The output
6
The C# (and .NET in general) API
Choose …
7
…/
System is a major
namespace for C#
8
Example of a C# console application
9
Start a new console application … then Add class
Employee. (you can add the class via the wizard, or by
manually typing it).
10
Add fields (instance
variables),
methods, etc. (You
can add them via
the wizard, or by
manually typing
them).
11
Can declare using the wizard or just
manually inside the code.
12
Creating a property …
13
Class View (after writing the code..)
Main() …
execution starts
here
Ctor and
methods
properties
Instance variables 14
using System;
The code …
namespace ConsoleApplication2
{
class Class1
Read until end of
{ line.
[STAThread]
static void Main(string[] args)
{
Console.Write( "Enter name : "); Convert
string theName = Console.ReadLine();
Call Console.Write( "Enter salary : " ); string to
method string theSalary = Console.ReadLine(); double.
Console.WriteLine( "name = " + theName + " salary = " + theSalary);
raise()
double theSalaryDouble = Convert.ToDouble(theSalary);
Employee e = new Employee(theName, theSalaryDouble);
Console.WriteLine( "e:: " + e.ToString());
e.raise(10);
Console.WriteLine( "e:: " + e.ToString());
e.Name = “Another name”;
e.Salary = 25000;
Console.WriteLine( "e:: " + e.ToString());
Call
}
} ctor
}
Use
properties 15
using System;
Employee.cs
namespace ConsoleApplication2
{
/// <summary> Class Employee
/// starts here
/// </summary>
public class Employee
{
public Employee()
{ Default constructor
//
// TODO: Add constructor logic here
//
}
16
public double Salary
{
…
set
{
salary = value;
}
get
Property
{ Salary
return salary;
}
}
}
} The reserved word override is used for
every method that overrides a method.
18
C#
• Developed at Microsoft by a team led by Anders
Hejlsberg and Scott Wiltamuth (June 2000)
– Event driven, object oriented, visual programming
language.
– Based on C, C++ and Java.
– Incorporated into .NET platform
• Web based applications can be distributed
– Devices and desktop computers
• Programs that can be accessed by anyone through any
device
• Allows communicating with different computer languages
(any language that is supported by .NET).
19
Visual Studio .NET Integrated Development Environment
(IDE) Overview
location bar
navigation
buttons
hidden window
recent
projects
Start Page
links
buttons
20
Start Page in Visual Studio .NET.
.NET Framework and the
Common Language Runtime
• .NET Framework
– Heart of .NET
• Manages and executes applications and Web services
• Provides security, memory management and other programming capabilities
– Includes Framework class library (FCL)
• Pre-packaged classes ready for reuse
• Used by any .NET language (VB, C#, C++, J#, etc …)
– Common Language Specification (CLS), is a specification that all languages that
run under .NET should conform to.
• Submitted to European Computer Manufacturers Association to make the
framework easily converted to other platforms
– Executes programs by Common Language Runtime (CLR)
21
.NET Framework and the Common
Language Runtime (CLR)
9 Common Language Runtime (CLR). Central
part of framework
9 Compilation process
9 Two compilations take place
1. Programs compiled to Intermediate Language (IL). (IL is
like Java’s bytecode).
2. IL code is translated into machine code for a particular
platform
22
JVM (Java) vs CLR (.NET) : multiple platforms
vs multiple languages.
23
What you need to develop C#
programs
24
How to get and install the .NET SDK [currently version 1.1] (for C#
development without using Visual Studio. Normally you will not do
this if you have Visual Studio)
1. http://www.microsoft.com/downloads
2. search for .NET Framework SDK
3. and get
http://www.microsoft.com/downloads/details.aspx?FamilyID=9b3
a2ca6-3647-4070-9f41-a333c6b9181d&DisplayLang=en
http://www.microsoft.com/downloads/details.aspx?familyid=262D25E
3-F589-4842-8157-034D1E7CF3A3&displaylang=en
25
C# constructs
• C# has all the standard structured programming
constructs (such as if, while, for, break, continue, switch,
etc) and they all have the same syntax as in Java.
• Also C# has method overloading, the same way as in
Java.
• In a typical C# program we have several classes (in the
same sense as in Java) and if the program is a Console
Application (i.e., non-Windows) one class that contains
Main() – the method that starts execution. Each class
may have one or more constructors (same as in Java).
• C# has the this keyword, with the same meaning as in
Java. (but not for using it as this(…) inside constructors.)
26
C# constructs …/
• Arrays are defined and used as in Java
(but there are some additional variations in
C#).
– int[] x;// declare reference to an array
– x = new int[ 10 ]; // locate an array of size 10.
27
ForEach.cs
1 // ForEach.cs
2 // Demonstrating for/each structure. Use the foreach loop to examine each
3 using System; element in the array
4
5 class ForEach
If the current array
6 { element is smaller than
7 // main entry point for the application lowGrade, set lowGrade
8 static void Main( string[] args ) to contain the value of
9 { the current element
10 int[,] gradeArray = { { 77, 68, 86, 73 },
11 { 98, 87, 89, 81 }, { 70, 90, 86, 81 } };
12
13 int lowGrade = 100;
14
15 foreach ( int grade in gradeArray )
16 {
17 if ( grade < lowGrade )
18 lowGrade = grade;
19 }
20
21 Console.WriteLine( "The minimum grade is: " +
lowGrade );
22 }
23 }
The minimum grade is: 68 28
Properties
(one feature that Java does not have)
• Public properties allow clients to:
– Get (obtain the values of) private data
– Set (assign values to) private data
• Get accessor
– Controls formatting of data
• Set accessor
– Ensure that the new value is appropriate for
the data member
29
How to use properties
int x = time.Hour, where time is an object (of
a class Time, probably), is equivalent to
int x = time.getHour() of the Java getter
method.
time.Hour = 5, is equivalent to
time.setHour(5), assuming the setHour(int)
method was available.
30
Cont .. Operator overloading …
• C# contains many operators (such as + and - ) that are
defined for some primitive types
• It is often useful to use operators with user-defined types
(e.g., a complex number class)
• Operator notation may often be more intuitive than
method calls
• C# allows programmers to overload operators to make
them sensitive to the context in which they are used
31
Operator Overloading
• Methods define the actions to be taken for the overloaded operator
• They are in the form: public static ReturnType operator operator-
to-be-overloaded( arguments )
• These methods must be declared public and static
• The return type is the type returned as the result of evaluating the
operation
• The keyword operator follows the return type to specify that this
method defines an operator overload
• The last piece of information is the operator to be overloaded (such
as +, -, *, etc.)
• If the operator is unary, one argument must be specified, if the
operator is binary, then two, etc.
32
1
2
// ComplexNumber.cs
// Class that overloads operators for adding, subtracting
ComplexNumber.cs
3 // and multiplying complex numbers.
4
5 public class ComplexNumber
6 {
7 private int real;
8 private int imaginary;
9
Property Real provides access to the
10 // default constructor real part of the complex number
11 public ComplexNumber() {}
12
13 // constructor
14 public ComplexNumber( int a, int b )
15 {
16 Real = a;
17 Imaginary = b;
Class ComplexNumber definition
18 }
19
20 // return string representation of ComplexNumber
21 public override string ToString()
22 {
23 return "( " + real +
24 ( imaginary < 0 ? " - " + ( imaginary * -1 ) :
25 " + " + imaginary ) + "i )";
26 }
27
28 // property Real
29 public int Real
30 {
31 get
32 {
33 return real;
34 } 33
35
36 set ComplexNumber.cs
37 {
38 real = value;
39 }
40
41 } // end property Real
42
43 // property Imaginary Overload the addition (+)
44 public int Imaginary
operator for
45 {
ComplexNumbers.
46 get
47 {
48 return imaginary;
49 }
50
51 set
52 { Property Imaginary provides
53 imaginary = value; access to the imaginary part of a
54 } complex number
55
56 } // end property Imaginary
57
58 // overload the addition operator
59 public static ComplexNumber operator + (
60 ComplexNumber x, ComplexNumber y )
61 {
62 return new ComplexNumber(
63 x.Real + y.Real, x.Imaginary + y.Imaginary );
64 } 34
65
66 // provide alternative to overloaded + operator ComplexNumber.cs
67 // for addition
68 public static ComplexNumber Add( ComplexNumber x,
69 ComplexNumber y ) Method Subtract – provides an
70 { alternative to the subtraction
71 return x + y; operator
72 }
74 // overload the subtraction operator
Overloads the
75 public static ComplexNumber operator - ( multiplication (*)
76 ComplexNumber x, ComplexNumber y ) operator for
77 { ComplexNumbers
78 return new ComplexNumber(
79 x.Real - y.Real, x.Imaginary - y.Imaginary );
80 }
81 Method Add – provides an
82 // provide alternative to overloaded - operator alternative to the addition
83 // for subtraction
operator
84 public static ComplexNumber Subtract( ComplexNumber x,
85 ComplexNumber y )
86 {
Overload the
87 return x - y;
88 }
subtraction (-)
90 // overload the multiplication operator operator for
91 public static ComplexNumber operator * ( ComplexNumbers
92 ComplexNumber x, ComplexNumber y )
93 {
94 return new ComplexNumber(
95 x.Real * y.Real - x.Imaginary * y.Imaginary,
96 x.Real * y.Imaginary + y.Real * x.Imaginary );
35
97 }
98 ComplexNumber.cs
99 // provide alternative to overloaded * operator
100 // for multiplication
101 public static ComplexNumber Multiply( ComplexNumber x,
102 ComplexNumber y )
103 {
104 return x * y;
105 }
106
107 } // end class ComplexNumber
Method
Multiply –
provides an
alternative to
the
multiplication
operator
36