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
Consider
the program
each line of
do what it is
code
doing?
What parts
What is it
Advice
are
doing?
confusing?
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
EXPLORER
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-guide/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
DECLARATION
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
DOCUMENTATION
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
COMPARING
CHARACTER
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
METHODS
From String to Char
• A string (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(<args>)” “typename.FunctionName(<args>)”
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.