Professional Documents
Culture Documents
L04. C# Syntax and Structure
L04. C# Syntax and Structure
C++
Visual Basic
Assembly Language
more …
Exception handling
Inheritance
Interfaces
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
{ } 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
C# is fully object-oriented
like Java but not like C++
Read-only Static
Pascal ReadOnlyValue
field (constant)
IDisposable
Interface Pascal
Note: Interface names always begin with the prefix I.
Parameterized
Constructor
Overridden ToString
method
Console output
for the program
Visual Basic
Each project has one or more C# source code files (.cs) and possibly
other files as well
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
Can
target .
NET
version
Console Application
Project Name
Location of Solution Folder
Use these
menu choices
to run.
The F-key
combinations
or buttons can
also be used.
Select Font
Select Syntax
Coloring Choices
Select all of
the Advanced
Options
Intellisense Options
Escape to cancel
More on
Intellisense later …
Property Name
Public getter/setter
combo
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:
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
Use refactoring
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)
System.Object is a base
class for everything
Approx. 30 digits
Unicode
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.
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.
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.
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.)
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.
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.
ToLowerInvariant Returns a copy of this String object converted to lowercase using the casing
rules of the invariant culture.
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.