0% found this document useful (0 votes)
3 views

Cs321 Winter 2023 Lecture 3 Strings

The document outlines a learning goal to deepen understanding of C# strings and characters, emphasizing syntax, semantics, and the use of unit tests. It explains the nature of strings as reference types and chars as primitive value types, along with their representations in memory. Additionally, it covers concepts like Unicode, encoding, and character equality, providing examples and documentation references for further exploration.

Uploaded by

mbielawski21
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Cs321 Winter 2023 Lecture 3 Strings

The document outlines a learning goal to deepen understanding of C# strings and characters, emphasizing syntax, semantics, and the use of unit tests. It explains the nature of strings as reference types and chars as primitive value types, along with their representations in memory. Additionally, it covers concepts like Unicode, encoding, and character equality, providing examples and documentation references for further exploration.

Uploaded by

mbielawski21
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

TODAY’S GOAL

Deepen our understanding of C# by diving into strings


and characters
Goal Breakdown

Become more familiar with C# syntax and semantics

Understand the concepts introduced using real examples

How using Unit tests can help your understanding

Learn about how text and characters are represented


Why does
the Consider
program each line
do what it of code
is doing?

What
parts are What is it
Advice
confusing doing?
?

Why is
each
symbol or
element
there?
Test Projects: A special kind of
library
Test Projects

They provide tool support for running tests in the IDE

We will be creating test projects in the lab

Useful for exploratory coding and … well … testing


THE
TEST
EXPLORE
R
WINDOW
What is a String?
•A representation of text values as sequences of characters
Consider the Following
• Are strings value types (“structs”) and allocated on the stack?
• Or are they reference types (“classes”) and allocated on the heap?
• What about chars?
• Why?
Strings
• https://learn.microsoft.com/en-us/dotnet/csharp/programming-guid
e/strings/

•A string is an object of type System.String whose value represents


text.
• Internally,
the text is stored as a sequential read-only collection of
char objects
• The keyword “string” is an alias for the type “System.String”
A Helper Function for Today
STRING
VARIABLE
DECLARATIO
N
The Take-away
• No observable difference between “string” and “System.String”
• Local variable declarations can (and should) use var
• Use the “string” alias in your code
• See the Microsoft coding guidelines
System.String Documentation
Reading the documentation
• It said that it was a “class” (so it is a reference type)
• It said that it derives from “System.Object”
• This means that it shares the methods (functions) of System.Object
• We already know that everything derives from System.Object
• What is a UTF-16 Code Unit?
• What does it mean that it “implements interfaces”?
WHY IS STRING A CLASS?
So What’s a Char
•A Char is primitive value type
• It has two bytes, and represents a Unicode UTF-16 character.
• It is classified as an integral type by the specification:
• The char keywords is an alias for the type System.Char
• See the language reference and the type documentation
THE
SYSTEM.CHAR
DOCUMENTATIO
N
Salient Points from Documentation
• It’s a struct
• It derives from ValueType
• All structs derive from ValueType automatically
• It implements a number of interfaces like IComparable
What “Implementing Interfaces”
means
• An interface is a kind of contract
• It
states a set of methods and properties that a class or struct may
implement
• An interface is also a type
INTERFACE EXAMPLE
Code Example of IComparable
The Interface Methods are on the
Type
Glyph
•A graphic symbol that represents a character
• An element of a typeface (aka font fontamily)
• Letters (e.g., ‘e’) and diacritics (e.g., accents) are separate glyphys
Char Literals
WHAT’S UNICODE?
What’s Utf-16
• One
of multiple Unicode encodings (e.g., Utf-8, Utf-16, Utf-32,
GB18030)
• It
is variable length: Characters use either one or two 16-bit code
units
• Not as widely used on the web as Utf-8
What’s an Encoding
• An assignment of numbers to characters
Did you see that!
• Two“Chars” in a row are sometimes needed to properly display
characters
• This is why we disambiguate code units and code points
• Code points are made up of code units
• In C# a char instance is a code unit in the Utf-16 encoding
ASCII
• ASCII is a character encoding first published as a standard in 1963
• Only 128 Characters
• 95 are printable
• 33 are control codes
• One byte per code point
COMPARIN
G
CHARACTE
R
EQUALITY
Escape Sequences
• How do you write the character used to delimit char literals?
• Or the character that represents a newline or tab?
• Or an unprintable control code like the bell
• Answer: use an escape sequence (backslash followed by code).
SOME
CHAR
METHOD
S
From String to Char
•Astring (s) with one character can be converted to a char in two
ways:
• One is to use “String.Parse”
• The other is to use the indexing property of the string “string[0]”
Invoking Instance versus Static
Methods

Some methods have the form While others have the form
“expression.FunctionName(<a “typename.FunctionName(<ar
rgs>)” gs>)”
Char is not an expression
• The word “Char” is the name of a type
• More specifically a class within the namespace
• You cannot use it where you would an expression:
But you can use it to call static
methods
• Char.IsControl

• Char.IsLetterOrDigit

• Char.IsUpper

• Char.IsWhitespace

• Etc.

You might also like