charles university in prague faculty of mathematics and physics c# language &.net platform 4 th...

26
CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics C# Language & .NET Platform 4 th Lecture Pavel Ježek [email protected]ff.cun i.cz Some of the slides are based on University of Linz .NET presen © University of Linz, Institute for System Software, 20 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.asp

Upload: douglas-robbins

Post on 18-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Visibility Visibility modifiers: publicAccess is not restricted. protectedAccess is limited to the containing class or types derived from the containing class. internalAccess is limited to the current assembly. protected internalAccess is limited to the current assembly or types derived from the containing class. privateAccess is limited to the containing type. Default visibility in: enumpublic classprivate interfacepublic structprivate

TRANSCRIPT

Page 1: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CHARLES UNIVERSITY IN PRAGUE

http://d3s.mff.cuni.cz/~jezek

faculty of mathematics and physics

C# Language & .NET Platform

4th Lecture

Pavel Jež[email protected]

Some of the slides are based on University of Linz .NET presentations.© University of Linz, Institute for System Software, 2004

published under the Microsoft Curriculum License(http://www.msdnaa.net/curriculum/license_curriculum.aspx)

Page 2: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type SystemAll types

Reference types(allocated on

managed heap)

PointersValue types(allocated in-place

[with exceptions])

Classes(e.g. strings)

Interfaces Arrays Delegates

Simple types(Int32, Int64,

Double, Boolean, Char, …)

Nullables

EnumerationsStructures

User defined structures

Page 3: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Visibility

Visibility modifiers:public Access is not restricted.

protected Access is limited to the containing class or types derived from the containing class.

internal Access is limited to the current assembly.

protected internal Access is limited to the current assembly or types derived from the containing class.

private Access is limited to the containing type.

Default visibility in:enum publicclass privateinterface publicstruct private

Page 4: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type SystemAll types

Reference types(allocated on

managed heap)

PointersValue types(allocated in-place

[with exceptions])

Classes(e.g. strings)

Interfaces Arrays Delegates

Simple types(Int32, Int64,

Double, Boolean, Char, …)

Nullables

EnumerationsStructures

User defined structures

Page 5: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Parameters

value parameters (input parameters)void Inc(int x) {x = x + 1;}void f() {int val = 3;Inc(val); // val == 3}

- "call by value"- formal parameter is a copy of the

actual parameter- actual parameter is an expression

- "call by reference"- formal parameter is an alias for the

actual parameter(address of actual parameter is passed)

- actual parameter must be a variable

ref parameters (transient parameters)void Inc(ref int x) { x = x + 1; }void f() {

int val = 3;Inc(ref val); // val == 4

}

Page 6: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Parameters

value parameters (input parameters)void Inc(int x) {x = x + 1;}void f() {int val = 3;Inc(val); // val == 3}

- "call by value"- formal parameter is a copy of the

actual parameter- actual parameter is an expression

- similar to ref parametersbut no value is passed by the caller.

- must not be used in the method beforeit got a value.

out parameters (output parameters)void Read (out int first, out int next) {

first = Console.Read();next = Console.Read();

}void f() {

int first, next;Read(out first, out next);

}

- "call by reference"- formal parameter is an alias for the

actual parameter(address of actual parameter is passed)

- actual parameter must be a variable

ref parameters (transient parameters)void Inc(ref int x) { x = x + 1; }void f() {

int val = 3;Inc(ref val); // val == 4

}

Page 7: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type SystemAll types

Reference types(allocated on

managed heap)

PointersValue types(allocated in-place

[with exceptions])

Classes(e.g. strings)

Interfaces Arrays Delegates

Simple types(Int32, Int64,

Double, Boolean, Char, …)

Nullables

EnumerationsStructures

User defined structures

Page 8: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Declaration of Local Variables

void foo(int a) {int b;if (...) {

int b; // error: b is already declared in the outer blockint c;int d;...

} else {int a; // error: a is already declared in the outer block (parameter)int d; // ok: no conflict with d in the if block

}for (int i = 0; ...) {...}for (int i = 0; ...) {...} // ok: no conflict with i from the previous loopint c; // error: c is already declared in a nested block

}

int e = 1, f;if (e == 1) {

f = 2;}e = f; // error: f is not initialized in every possible execution path

Page 9: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type SystemAll types

Reference types(allocated on

managed heap)

PointersValue types(allocated in-place

[with exceptions])

Classes(e.g. strings)

Interfaces Arrays Delegates

Simple types(Int32, Int64,

Double, Boolean, Char, …)

Nullables

EnumerationsStructures

User defined structures

Page 10: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

How Would You Implement List<T>.Clear()?

Option Complexity with Respect to Number of Elements in List (n)

A O(1)

B O(n)

C Something else.

Page 11: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

How Would You Implement List<T>.Clear()?

Option Complexity with Respect to Number of Elements in List (n)

A O(1)

B O(n) – set all elements to null! Mind the GC!C Something else.

Page 12: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What about String.Substring?

Page 13: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type SystemAll types

Reference types(allocated on

managed heap)

PointersValue types(allocated in-place

[with exceptions])

Classes(e.g. strings)

Interfaces Arrays Delegates

Simple types(Int32, Int64,

Double, Boolean, Char, …)

Nullables

EnumerationsStructures

User defined structures

Page 14: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type InheritanceSystem.Object(C# keyword: object)

user-defined classes(C# keyword: class)

delegates(C# keyword: delegate)

pointers(C#: Type *)

System.Delegate

System.MulticastDelegate

System.ValueType

System.Enum

System.Array

arrays(C#: Type[] or Type[,])

System.String(C# keyword: string)

interfaces(C# keyword: interface)

user-defined structures

(C# keyword: struct)

enumerations(C# keyword: enum)

System.Int32(C# keyword: int)

System.Int64(C# keyword: long)

System.Double(C# keyword: double)

System.Boolean(C# keyword: bool)

simple types

System.Nullable(C#: Type?)

Page 15: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type InheritanceSystem.Object(C# keyword: object)

user-defined classes(C# keyword: class)

delegates(C# keyword: delegate)

pointers(C#: Type *)

System.Delegate

System.MulticastDelegate

System.ValueType

System.Enum

System.Array

arrays(C#: Type[] or Type[,])

System.String(C# keyword: string)

interfaces(C# keyword: interface)

user-defined structures

(C# keyword: struct)

enumerations(C# keyword: enum)

System.Int32(C# keyword: int)

System.Int64(C# keyword: long)

System.Double(C# keyword: double)

System.Boolean(C# keyword: bool)

simple types

System.Nullable(C#: Type?)

Interfaces are not inherited from System.Object, but

System.Object members are callable/accessible via any interface

type expression.

Page 16: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Visibility

Visibility modifiers:public Access is not restricted.

protected Access is limited to the containing class or types derived from the containing class.

internal Access is limited to the current assembly.

protected internal Access is limited to the current assembly or types derived from the containing class.

private Access is limited to the containing type.

Default visibility in:enum publicclass privateinterface publicstruct private

Page 17: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Example: Inheritance of private members

Page 18: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type InheritanceSystem.Object(C# keyword: object)

user-defined classes(C# keyword: class)

delegates(C# keyword: delegate)

pointers(C#: Type *)

System.Delegate

System.MulticastDelegate

System.ValueType

System.Enum

System.Array

arrays(C#: Type[] or Type[,])

System.String(C# keyword: string)

interfaces(C# keyword: interface)

user-defined structures

(C# keyword: struct)

enumerations(C# keyword: enum)

System.Int32(C# keyword: int)

System.Int64(C# keyword: long)

System.Double(C# keyword: double)

System.Boolean(C# keyword: bool)

simple types

System.Nullable(C#: Type?)

Page 19: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

CLI Type Inheritance (Sealed Types)System.Object(C# keyword: object)

user-defined classes(C# keyword: class)

delegates(C# keyword: delegate)

pointers(C#: Type *)

System.Delegate

System.MulticastDelegate

System.ValueType

System.Enum

System.Array

arrays(C#: Type[] or Type[,])

System.String(C# keyword: string)

interfaces(C# keyword: interface)

user-defined structures

(C# keyword: struct)

enumerations(C# keyword: enum)

System.Int32(C# keyword: int)

System.Int64(C# keyword: long)

System.Double(C# keyword: double)

System.Boolean(C# keyword: bool)

simple types

System.Nullable(C#: Type?)

sealed

sealedsealed

sealed

sealed

sealed

sealed

Optionally sealed

Page 20: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Ref. Type DOES NOT Imply InstancesSystem.Object(C# keyword: object)

user-defined classes(C# keyword: class)

delegates(C# keyword: delegate)

pointers(C#: Type *)

System.Delegate

System.MulticastDelegate

System.ValueType

System.Enum

System.Array

arrays(C#: Type[] or Type[,])

System.String(C# keyword: string)

interfaces(C# keyword: interface)

user-defined structures

(C# keyword: struct)

enumerations(C# keyword: enum)

System.Int32(C# keyword: int)

System.Int64(C# keyword: long)

System.Double(C# keyword: double)

System.Boolean(C# keyword: bool)

simple types

System.Nullable(C#: Type?)

Page 21: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

Hiding

Members can be declared as new in a subclass.They hide inherited members with the same name and signature.

class A {public int x;public void F() {...}public virtual void G() {...}}

class B : A {public new int x;public new void F() {...}public new void G() {...}}

B b = new B();b.x = ...; // accesses B.xb.F(); ... b.G(); // calls B.F and B.G

((A)b).x = ...; // accesses A.x!((A)b).F(); ... ((A)b).G(); // calls A.F and A.G!

Page 22: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What is the output the following program?

class A {public string className = “A”;

}class B : A {

private string className = “B”;}class Program {

static void Main(string[] args) {Console.WriteLine(new B().className);

}}

Option Result

A It will not compile – error in class B.

B It will not compile – error in class Program.

C A

D B

E It will generate a runtime error.

Page 23: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What is the output the following program?

class A {public string className = “A”;

}class B : A {

private string className = “B”;}class Program {

static void Main(string[] args) {Console.WriteLine(new B().className);

}}

Option Result

A It will not compile – error in class B.

B It will not compile – error in class Program.

C A

D B

E It will generate a runtime error.

a compiler warning: use new keyword

new

Page 24: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What is the output the following program?

class A {public string className = “A”;

}class B : A {

private string className = “B”;}class Program {

static void Main(string[] args) {Console.WriteLine(new B().className);

}}

Option Result

A It will not compile – error in class B.

B It will not compile – error in class Program.

C A & a compiler warning: use new keyword.

D B

E It will generate a runtime error.

Page 25: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What is the output the following program?

class A {public string className = “A”;

}class B : A {

private string className = “B”;public void f() {

Console.WriteLine(className);}

}class Program {

static void Main(string[] args) {Console.Write(new B().className);new B().f();

}}

Option Result

A A A

B A B

C B A

D B B

Page 26: CHARLES UNIVERSITY IN PRAGUE  faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek

What is the output the following program?

class A {public string className = “A”;

}class B : A {

private string className = “B”;public void f() {

Console.WriteLine(className);}

}class Program {

static void Main(string[] args) {Console.Write(new B().className);new B().f();

}}

Option Result

A A A

B A B & a compiler warning: use new keyword.

C B A

D B B