Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 86

A brief overview and introduction

C# SYNTAX AND STRUCTURE1

1 Materials from text, professor, MSDN, and Joe Hummel, SIGCSE

Introduction to the Syntax and Structure of C# Programs 5/25/21 1


A Little Background on C#
C and its derivatives

Introduction to the Syntax and Structure of C# Programs 5/25/21 2


What Languages Do You Know?
 You have experience programming in one or
more programming languages such as
 Java

 C++

 Visual Basic

 Assembly Language

 more …

Introduction to the Syntax and Structure of C# Programs 5/25/21 3


A Little Background
 The C language was developed in the early 1970’s
for use in creating the Unix operating system
 Parent language was B

 It was originally designed for ease of use by


experts who knew what they were doing, not for
use by the computing world in general
 Designed to write small, efficient code that used
“coding tricks” to improve performance, code size
 Not designed for security, privacy, robustness, etc.

 Easy to hack because of the designed-in ease of


doing “coding tricks”
Introduction to the Syntax and Structure of C# Programs 5/25/21 4
Background, continued
 The same features that made it possible to write small,
efficient code made it possible for programmers to make
serious logic errors without having much of the necessary
support to catch them
 For example, arrays are contiguous blocks of memory but have no
built-in bounds checking in C or C++
 This allows arrays of different sizes to be passed to a function as
parameters in different calls without the function having to know how many
entries each can hold
 But it also allows users to trash memory by inadvertently (or intentionally)
using subscripts that are out of bounds
 In less sophisticated operating systems, it even allows the user’s program
to trash the operating system inadvertently (or purposely)
Introduction to the Syntax and Structure of C# Programs 5/25/21 5
More Background
 C did not support many of the features that are so important today. For
example, the original did not support
 Object-oriented concepts such as classes, objects, inheritance, etc.

 Boolean data types

 Exception handling

 Templates and/or Generics

 Inheritance

 Interfaces

 Features as fundamental as conversion between compatible data


types were often added by people who did not use common naming
conventions or common design principles – making it impossible for
people who understood one concept to make intelligent guesses about
similar concepts
Introduction to the Syntax and Structure of C# Programs 5/25/21 6
Background, continued
 C became a popular, widely used language
 Many compilers were created, all with some
differences, meaning code was not always
portable between machines
 Standards committee formed
 Attempt was made to be sure all compilers supported
“standard” features in a consistent way
 Backward compatibility with existing software was also
important
 Periodically, standards have been expanded and
updated
Introduction to the Syntax and Structure of C# Programs 5/25/21 7
Background, continued
 Many other languages have been derived from the original C
language.
 C++ added object-oriented features to C, but kept all of C as well

 Java
 Removed some dangerous features of C (such as arrays without
bounds-checking)
 Added some features such as object-orientation, garbage collection,
better memory management, and so forth
 C# - Microsoft’s “better Java” – but also an international open
standard
 Many others

 If you know the syntax of one C derivative, you are probably


able to understand the syntax of another reasonably quickly
Introduction to the Syntax and Structure of C# Programs 5/25/21 8
Background, continued
 The derivative languages “look like” C in that
they use similar syntax, many similar
semantics, and similar keywords
 All use { }, semicolons, // for comments, and so
forth
 All use keywords such as int, double, if, else,
return, while, for, try, catch, throw, private,
public, class, and so forth to mean similar things
 All use operators such as =, ==, +, -, *, /, %, ( ), [ ],
>, <, <=, &&, ||, ++, --, and so forth in similar ways
to mean similar things

Introduction to the Syntax and Structure of C# Programs 5/25/21 9


So, if you know Java or C++…
 You can read and understand much of C#
 Semicolons end C# statements

 Case-sensitive so COUNT, Count, and count are three different identifiers

 { } are used to designate a block of code in the same way as in Java, C++

 Primitive data types (int, float, double, char, bool, etc.) have the same
interpretations as in C++ and Java
 // marks the beginning of a single line comment

 Many keywords such as if, else, while, for, public, private, return are
used in each language with similar meanings
 Arithmetic, logical, and comparison operators are essentially the same: +,
-, *, /, =, +=, &&, ||, >, < , ==, !, etc.
 All have a main (or Main) method where execution begins for a console
application

Introduction to the Syntax and Structure of C# Programs 5/25/21 10


Popularity of C# is growing

Based on a poll of about


400 programmers at an
April 2009 developers
conference

Introduction to the Syntax and Structure of C# Programs 5/25/21 11


C# - a first example
The obligatory Hello World program

Introduction to the Syntax and Structure of C# Programs 5/25/21 12


Hello World
Somewhat fewer syntactic and … For example, the name of the
structural requirements than Java … class need not be the same as the
name of the file that contains it

C# is fully object-oriented
like Java but not like C++

Main is Pascal case and may or may


not have arguments

Use this syntax to write a line of text on


the console window instead of Java’s
System.out.println or C++’s cout

Introduction to the Syntax and Structure of C# Programs 5/25/21 13


Namespaces and Using
 Related classes are grouped into namespaces
 Like Java packages and like C++ namespaces

 Namespaces may be designated for use in C#


programs with a using command
 Similar to imports in Java and #include in C++
If a “using” is specified for the namespace,
classes from the namespace do not have to be
qualified with the namespace name unless
there is an ambiguity

Introduction to the Syntax and Structure of C# Programs 5/25/21 14


Example with Keyboard Input

ReadLine inputs a string


The same representing the entire
Console class line of input
can be used
for input

Introduction to the Syntax and Structure of C# Programs 5/25/21 15


Identifier Naming Conventions in C#
Identifier Type Case Example
Class Name Pascal AppDomain
Enum type Pascal ErrorLevel
Enum values Pascal FatalError
Event Pascal ValueChange

Exception class WebException


Pascal
name Note:   Name always ends with the suffix Exception.

Read-only Static
Pascal ReadOnlyValue
field (constant)

IDisposable
Interface Pascal
Note:  Interface names always begin with the prefix I.

Method Pascal ToString


Namespace Pascal System.Drawing
Parameter Camel typeName
Property Pascal BackColor
btnOK, lblName, txtTitle, Count, k
Local and Private Private fields and local variables are “for internal use only”, so
Either
variables their names are a matter of programmer or organizational
preference.
Introduction to the Syntax and Structure of C# Programs 5/25/21 16
C# - Example with separate class
Driver and another class

Introduction to the Syntax and Structure of C# Programs 5/25/21 17


Example with Another Class
Ordinary C# Class
Attributes

Parameterized
Constructor

Overridden ToString
method

Introduction to the Syntax and Structure of C# Programs 5/25/21 18


Driver Program

Console output
for the program

ToString method of MilesPerGallon


invoked implicitly here to convert object
into a string that can be displayed

Introduction to the Syntax and Structure of C# Programs 5/25/21 19


Visual Studio
Creating, editing, running, and debugging C# code

Introduction to the Syntax and Structure of C# Programs 5/25/21 20


Using Visual Studio with C#
 Visual Studio with C# uses the same IDE (Interactive
Development Environment) as
 Visual C++

 Visual Basic

 Visual F# and IronPython (added in VS 2010)

 VS has many features in common with the Eclipse IDE


for Java
 The IDE for VS provides a code editor, project
manager, interactive debugger, UML diagramming tool,
and many other features, some of which we shall see

Introduction to the Syntax and Structure of C# Programs 5/25/21 21


Visual Studio Solution
 Visual studio organizes C# programs into
 Solutions (.sln)

 Made up of one or more projects (.csproj)

 Each project has one or more C# source code files (.cs) and possibly
other files as well

 The solution explorer tab in Visual Studio shows the


solution structure:

Project

Source
code files
Introduction to the Syntax and Structure of C# Programs 5/25/21 22
Creating a Console Application in VS2010
 On the File menu, choose New/Project

Introduction to the Syntax and Structure of C# Programs 5/25/21 23


Creating a Console App, cont.

Can
target .
NET
version

Console Application

Project Name
Location of Solution Folder

Solution name same as project by default


Introduction to the Syntax and Structure of C# Programs 5/25/21 24
Creating a Console App, cont.
Code editor
window

File name may


Namespace and class also be
names default as shown changed here
– but the names may be
changed

Introduction to the Syntax and Structure of C# Programs 5/25/21 25


Creating a Console App, cont.

Use these
menu choices
to run.
The F-key
combinations
or buttons can
also be used.

Introduction to the Syntax and Structure of C# Programs 5/25/21 26


Add a New Class to the Project

Use this to add


a new class or
other project
item

Introduction to the Syntax and Structure of C# Programs 5/25/21 27


Adding a New Class

Name the class


file

Introduction to the Syntax and Structure of C# Programs 5/25/21 28


Customize VS
Suit your preferences for fonts, colors, spacing, and more

Introduction to the Syntax and Structure of C# Programs 5/25/21 38


Setting Options
 Use Tools/Options to get to Options Dialog
May also set options for error
messages, output, debugger, etc.

Select Font

Select Syntax
Coloring Choices

Preview your selection

Introduction to the Syntax and Structure of C# Programs 5/25/21 39


More Options
Set Default Location for
new Projects – like a
Java workspace

Introduction to the Syntax and Structure of C# Programs 5/25/21 40


More Options

Show line numbers in


code editor

Introduction to the Syntax and Structure of C# Programs 5/25/21 41


More Options

Set size of tabs for


indenting things – 4 is
common

Introduction to the Syntax and Structure of C# Programs 5/25/21 42


More Options

Select all of
the Advanced
Options

Introduction to the Syntax and Structure of C# Programs 5/25/21 43


More Options

Set your preferences for


automatic indenting –
example shows the effect
for the selected item

Introduction to the Syntax and Structure of C# Programs 5/25/21 44


More Options

Preferences for vertical


spacing – makes program
more readable. Example
shows results.

Introduction to the Syntax and Structure of C# Programs 5/25/21 45


More Options

Set preferences for


horizontal spacing to
improve readability. See
results in example to left.

Introduction to the Syntax and Structure of C# Programs 5/25/21 46


More Options

Intellisense Options

Introduction to the Syntax and Structure of C# Programs 5/25/21 47


Save/Restore Options

This can be used to set options on


one machine and use them on
other machines as well.

Introduction to the Syntax and Structure of C# Programs 5/25/21 48


Intellisense
On-the-fly coding hints and more

Introduction to the Syntax and Structure of C# Programs 5/25/21 49


Intellisense
 After a period is typed following the name of a namespace,
class, object, etc., a list of what may come next appears
 Use tab to accept

 Escape to cancel

More on
Intellisense later …

Introduction to the Syntax and Structure of C# Programs 5/25/21 50


C# Properties
Looks and acts like a variable but built like two functions

Introduction to the Syntax and Structure of C# Programs 5/25/21 51


Properties in .NET
 In Java, classes often have
 Private attributes (fields) that are defined as
variables
 Public getter and/or setter methods that allow a
user of the class to assign a value to an attribute or
retrieve the current value of the attribute
 In .NET, the attributes (fields) and their
getter/setter methods are combined as
Properties

Introduction to the Syntax and Structure of C# Programs 5/25/21 52


Properties in .NET
Private field

Property Name

Public getter/setter
combo

The term “value” is a keyword used as


a parameter if none is given explicitly

Introduction to the Syntax and Structure of C# Programs 5/25/21 53


Properties in .NET
 Properties are used in a much simpler fashion
in .NET than getters/setters are in Java
 The name of the property is used as if it were a variable
 The getter is invoked if semantics call for retrieving the
property value
 The setter is invoked if semantics call for assigning a new
property value

Setter invoked

Getter invoked
Introduction to the Syntax and Structure of C# Programs 5/25/21 54
Properties in .NET – even better with VS
 It is extremely easy in VS to create a C# property
 Simply type the term prop and press tab-key, tab-key

 This generates:

 Replace int with correct data type

 Replace MyProperty with the name your property is to have

 No other code is required

 No need to declare/define the private attribute


 No need to fill in code for the getter and setter if the typical
standard code will do

Introduction to the Syntax and Structure of C# Programs 5/25/21 55


Properties in .NET
 In .NET, it is considered to be good practice to replace
essentially all private attributes (fields) by public properties, if
getters and/or setters are desired
 The underlying private attribute (field) is preserved

 The public getters and setters allow controlled access to the private attribute

 A property may have a public getter with a private setter to permit the outside
world to retrieve but not change the value

 The get or set may be omitted if the property is a write-only or a read-only


property

 Is there an easy way to convert an existing private attribute into a


public property?
 Yes!!

 Use refactoring

Introduction to the Syntax and Structure of C# Programs 5/25/21 56


Refactoring
Improving the readability and the structure of code without changing how
the code works or what it does

Introduction to the Syntax and Structure of C# Programs 5/25/21 57


Code Refactoring in Visual Studio
 Sometimes one has code that “does the job”, but it is
poorly structured, overly long or complex, difficult to
read and understand, difficult to maintain, and needs
improvement
 Refactoring is the process of modifying the structure of
existing code to improve its readability and
maintainability without changing its functionality in any
way
 Makes the code look better and be more readable without
breaking what the code does
 Visual Studio and CodeRush XPress automate much of the
process of refactoring
Introduction to the Syntax and Structure of C# Programs 5/25/21 58
Code Refactoring
 One example of refactoring is the conversion of
an existing private attribute to a public property
automatically by VS Could also
Right-click on rename
item to refactor

Expand the Refactor list


and choose
Encapsulate Field

Introduction to the Syntax and Structure of C# Programs 5/25/21 59


Property Generation
 Pressing the Enter key generates code that is
similar to the following

Introduction to the Syntax and Structure of C# Programs 5/25/21 60


Other Refactorings
 There are many possible refactorings. Here are some others

 Rename a method, variable, class, property, etc.

 Add parameter or reorder parameters

 Create an overload of a method

 If a change is made in one place, all references to that item in the entire
project are changed to match (even if they are in other files of the project)

Introduction to the Syntax and Structure of C# Programs 5/25/21 61


Extract Method Refactoring
 Suppose a method has become too complex or
too lengthy
 One can simplify it by turning a logical section of it
into a separate method

Introduction to the Syntax and Structure of C# Programs 5/25/21 62


Extracting a New Method From Existing Code
 First select the code to be extracted

Right click the


selected text and
choose Refactor /
Extract Method

Introduction to the Syntax and Structure of C# Programs 5/25/21 63


Shows proposed call to new
method that will replace the Extracting Method
extracted code

It figures out what


parameters are
needed and from
where they come

Click glyph Gives information

Introduction to the Syntax and Structure of C# Programs 5/25/21 64


Extracting a Method
 Allows us to set the location of the new method
in the existing code and accept or reject the
proposed refactoring operation

Introduction to the Syntax and Structure of C# Programs 5/25/21 65


Extracting a Method

Call to new method replaces the


Can extracted code
change
name if New
desired method
inserted

Introduction to the Syntax and Structure of C# Programs 5/25/21 66


.NET Data Types
Resembles Java but with additional types

Introduction to the Syntax and Structure of C# Programs 5/25/21 69


.NET Common Type System

System.Object is a base
class for everything

Introduction to the Syntax and Structure of C# Programs 5/25/21 70


Types in C# mapped to .NET Types

C# type names .NET type names

Approx. 30 digits

Introduction to the Syntax and Structure of C# Programs 5/25/21 71


Nullable Types in C#
 The name of an instance (object) of a class may not refer
to anything at a given time just as in Java.
 Student John = null;
 Employee Jill = null;
 There is an analog for value types such as int, double,
and decimal in C#.
 Important to be able to have no value for a value type because
databases allow numeric values to be “empty”.
 What if one enters data via a form and leaves blank the box for
age? This may be OK if age is optional for the application, but
how do we show that in a program?
 int age = null ; // syntax error for value types!

Introduction to the Syntax and Structure of C# Programs 5/25/21 72


Nullable Types, cont.
 Nullable types are C#’s solution to value types with no
values
 Uses ? syntax like this:
 valuetype? valuename = null;
 Examples
 int? age = null;
 double? GPA = null;

 Of course, variables defined as nullable types may also have


values of their underlying type
 age = 21;
 GPA = 4.00;

Introduction to the Syntax and Structure of C# Programs 5/25/21 73


More on Nullable Types
 All nullable types have two public properties
 HasValue is a boolean property that indicates
whether a nullable variable has a non-null value
or not
 Value returns the current value of a variable of
nullable type, but throws an exception if
HasValue is false
 See example on next slide …

Introduction to the Syntax and Structure of C# Programs 5/25/21 74


Example

Introduction to the Syntax and Structure of C# Programs 5/25/21 75


Null Coalescing Operator
 The null coalescing operator (??) evaluates a
nullable variable, and it returns either the
variable’s value if it has one or it returns a
specified value if the variable is null
 The following example illustrates this where bal is
of type decimal?
decimal balance = bal ?? 0.0M;
The constant 0.00 is of type double. Adding a suffix of
“M” designates the constant as type decimal
 The above assigns the value of bal to balance if
bal is not null. Otherwise, it assigns 0 to balance.
Introduction to the Syntax and Structure of C# Programs 5/25/21 76
String Class
 The string class is C#’s implementation of
.NET’s System.String class
 Similar to String class in Java

 Unicode

 Supports == and != operators

 Length property is set to the number of characters in


the value currently
 String.Empty is an empty string

Introduction to the Syntax and Structure of C# Programs 5/25/21 77


Visual Studio Help
 Many methods in the class; use VS Help to find String
class’s methods for more detail

Introduction to the Syntax and Structure of C# Programs 5/25/21 78


Some String Methods
Name Description

Clone Returns a reference to this instance of String.

Compare Overloaded. Compares two specified String objects and returns an integer that
indicates their relationship to one another in the sort order.

CompareOrdinal Overloaded. Compares two String objects by evaluating the numeric values of the
corresponding Char objects in each string.

CompareTo Overloaded. Compares this instance with a specified object or String and returns an
integer that indicates whether this instance precedes, follows, or appears in the same
position in the sort order as the specified object or String.

Concat Overloaded. Concatenates one or more instances of String, or the String


representations of the values of one or more instances of Object.

Contains Returns a value indicating whether the specified String object occurs within this string.

Copy Creates a new instance of String with the same value as a specified String.

CopyTo Copies a specified number of characters from a specified position in this instance to a
specified position in an array of Unicode characters.

EndsWith Overloaded. Determines whether the end of an instance of String matches a specified
string.

Equals Overloaded. Determines whether two String objects have the same value.

Finalize Allows an Object to attempt to free resources and perform other cleanup operations
before the Object is reclaimed by garbage collection. (Inherited from Object.)

Format Overloaded. Replaces each format item in a specified String with the text equivalent of
a corresponding object's value.
Introduction to the Syntax and Structure of C# Programs 5/25/21 79
Some String Methods
IndexOf(Char) Reports the index of the first occurrence of the specified Unicode
character in this string.

IndexOf(String) Reports the index of the first occurrence of the specified string in this
instance.

IndexOf(Char, Int32) Reports the index of the first occurrence of the specified Unicode
character in this string. The search starts at a specified character
position.
IndexOf(String, Int32) Reports the index of the first occurrence of the specified string in this
instance. The search starts at a specified character position.

IndexOf(String, Reports the index of the first occurrence of the specified string in the
StringComparison) current String object. A parameter specifies the type of search to use for
the specified string.

IndexOf(Char, Int32, Int32) Reports the index of the first occurrence of the specified character in this
instance. The search starts at a specified character position and
examines a specified number of character positions.

IndexOf(String, Int32, Int32) Reports the index of the first occurrence of the specified string in this
instance. The search starts at a specified character position and
examines a specified number of character positions.

Introduction to the Syntax and Structure of C# Programs 5/25/21 80


Some String Methods
IndexOf(String, Int32, StringComparison) Reports the index of first occurrence of the specified string in
the current String object. Parameters specify the starting
search position in the current string and the type of search to
use.
IndexOf(String, Int32, Int32, Reports the index of first occurrence of the specified string in
StringComparison) the current String object. Parameters specify the starting
search position in the current string, the number of
characters in the current string to search, and the type of
search to use.
IndexOfAny(array<Char []()>[]) Reports the index of the first occurrence in this instance of
any character in a specified array of Unicode characters.

IndexOfAny( array<Char [],()>[] Int32) Reports the index of the first occurrence in this instance of
any character in a specified array of Unicode characters. The
search starts at a specified character position.

IndexOfAny(array<Char []()>[], Int32, Int32) Reports the index of the first occurrence in this instance of
any character in a specified array of Unicode characters. The
search starts at a specified character position and examines
a specified number of character positions.

Insert Inserts a specified instance of String at a specified index


position in this instance.
Introduction to the Syntax and Structure of C# Programs 5/25/21 81
More String Methods
IsNullOrEmpty Indicates whether the specified String object is a null reference (Nothing in
Visual Basic) or an Empty string.

Join Overloaded. Concatenates a specified separator String between each element


of a specified String array, yielding a single concatenated string.

LastIndexOf Overloaded. Reports the index position of the last occurrence of a specified
Unicode character or String within this instance.

LastIndexOfAny Overloaded. Reports the index position of the last occurrence in this instance
of one or more characters specified in a Unicode array.

MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)

Normalize Overloaded. Returns a new string whose binary representation is in a


particular Unicode normalization form.

PadLeft Overloaded. Right-aligns the characters in this instance, padding on the left
with spaces or a specified Unicode character for a specified total length.

PadRight Overloaded. Left-aligns the characters in this string, padding on the right with
spaces or a specified Unicode character, for a specified total length.

Remove Overloaded. Deletes a specified number of characters from this instance.

Replace Overloaded. Replaces all occurrences of a specified Unicode character or String


in this instance, with another specified Unicode character or String.

Split Overloaded. Returns a string array that contains the substrings in this instance
that are delimited by elements of a specified string or Unicode character array.

StartsWith Overloaded. Determines whether the beginning of an instance of String


matches a specified string.
Introduction to the Syntax and Structure of C# Programs 5/25/21 82
Even More String Methods
Substring Overloaded. Retrieves a substring from this instance.

ToCharArray Overloaded. Copies the characters in this instance to a Unicode character


array.

ToLower Overloaded. Returns a copy of this String converted to lowercase.

ToLowerInvariant Returns a copy of this String object converted to lowercase using the casing
rules of the invariant culture.

ToString Overloaded. Converts the value of this instance to a String.

ToUpper Overloaded. Returns a copy of this String converted to uppercase.

ToUpperInvariant Returns a copy of this String object converted to uppercase using the casing
rules of the invariant culture.

Trim Overloaded. Removes all leading and trailing occurrences of a set of specified
characters from the current String object.

TrimEnd Removes all trailing occurrences of a set of characters specified in an array


from the current String object.

TrimStart Removes all leading occurrences of a set of characters specified in an array


from the current String object.

Introduction to the Syntax and Structure of C# Programs 5/25/21 83


Some String Examples

Introduction to the Syntax and Structure of C# Programs 5/25/21 84


Example, continued

Introduction to the Syntax and Structure of C# Programs 5/25/21 85


Example, continued

Introduction to the Syntax and Structure of C# Programs 5/25/21 86


Output of Example

Introduction to the Syntax and Structure of C# Programs 5/25/21 87


Overloads
 Many of the String methods have one or more
overloads
 Some common methods (Equals, IndexOf,
EndsWith, StartsWith, . . .) have at least one
overload that allows one to specify culture-
specific information
 For example:
if (str1.Equals (str2, StringComparison.CurrentCultureIgnoreCase)) …

Introduction to the Syntax and Structure of C# Programs 5/25/21 88


Example

Introduction to the Syntax and Structure of C# Programs 5/25/21 89


Verbatim String Literals
 Verbatim string literals start with @ and are enclosed in double
quotation marks. For example:
@"good morning" // a verbatim string literal
 The advantage of verbatim strings is that escape sequences are
not processed:
@"c:\terry\files\nuts.txt"
// rather than
"c:\\terry\\files\\nuts.txt"
 To include a double quotation mark in an @-quoted string, double
it:
@"""Ahoy!"" cried the captain."
// "Ahoy!" cried the captain.
Introduction to the Syntax and Structure of C# Programs 5/25/21 90
System.Object
The ultimate base class in .NET

Introduction to the Syntax and Structure of C# Programs 5/25/21 91


The Object Base Class

Introduction to the Syntax and Structure of C# Programs 5/25/21 92


System.Object Members
Name Description
Determines whether the specified Object is equal
Equals(Object)
to the current Object
(Static) Determines whether the specified Object
Equals(Object, Object)
instances are considered equal

Allows an Object to attempt to free resources and


Finalize perform other cleanup operations before the
Object is reclaimed by garbage collection

GetHashCode Serves as a hash function for a particular type


GetType Gets the Type of the current instance
MemberwiseClone Creates a shallow copy of the current Object
(Static) Determines whether the specified Object
ReferenceEquals
instances are the same instance

ToString Returns a String that represents current Object


Introduction to the Syntax and Structure of C# Programs 5/25/21 93
GetHashCode
 The purpose of System.Object’s GetHashCode
method is to compute an integer value that
somehow “represents” the object in question
 The resulting integer is not necessarily unique,
though a unique integer is “desirable” if that can be
done
 Can be thought of as an “encryption” of the object
into an integer
 This integer “hash code” has many purposes

Introduction to the Syntax and Structure of C# Programs 5/25/21 94


Overriding a Method

Must designate an override

This ToString method overrides


the virtual ToString method
defined in Object
Introduction to the Syntax and Structure of C# Programs 5/25/21 95
Interfaces

Use a colon instead of “implements”

Introduction to the Syntax and Structure of C# Programs 5/25/21 96


Inheritance

Use “base” instead of “super”

Introduction to the Syntax and Structure of C# Programs 5/25/21 97

You might also like