Monday, 26 November 2012
c# for dummies: c sharp programming
c# for dummies: c sharp programming: Strings and Equality As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed heap. By d...
c sharp programming
Strings and Equality
As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed
heap. By default, when you perform a test for equality on reference types (via the C# == and !=
operators), you will be returned true if the references are pointing to the same object in memory.
However, even though the string data type is indeed a reference type, the equality operators have been
redefined to compare the values of string objects, not the object in memory to which they refer.
static void StringEquality()
{
Console.WriteLine("=> String equality:");
string s1 = "Hello!";
string s2 = "Yo!";
Console.WriteLine("s1 = {0}", s1);
Console.WriteLine("s2 = {0}", s2);
Console.WriteLine();
// Test these strings for equality.
Console.WriteLine("s1 == s2: {0}", s1 == s2);
Console.WriteLine("s1 == Hello!: {0}", s1 == "Hello!");
Console.WriteLine("s1 == HELLO!: {0}", s1 == "HELLO!");
Console.WriteLine("s1 == hello!: {0}", s1 == "hello!");
Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2));
Console.WriteLine("Yo.Equals(s2): {0}", "Yo!".Equals(s2));
Console.WriteLine();
}
The C# equality operators perform a case-sensitive, character-by-character equality test on string
objects. Therefore, "Hello!" is not equal to "HELLO!", which is different from "hello!". Also, keeping the
connection between string and System.String in mind, notice that we are able to test for equality using
the Equals() method of String as well as the baked-in equality operators. Finally, given that every string
literal (such as "Yo") is a valid System.String instance, we are able to access string-centric functionality
from a fixed sequence of characters.
As fully explained in Chapter 4, a reference type is an object allocated on the garbage-collected managed
heap. By default, when you perform a test for equality on reference types (via the C# == and !=
operators), you will be returned true if the references are pointing to the same object in memory.
However, even though the string data type is indeed a reference type, the equality operators have been
redefined to compare the values of string objects, not the object in memory to which they refer.
static void StringEquality()
{
Console.WriteLine("=> String equality:");
string s1 = "Hello!";
string s2 = "Yo!";
Console.WriteLine("s1 = {0}", s1);
Console.WriteLine("s2 = {0}", s2);
Console.WriteLine();
// Test these strings for equality.
Console.WriteLine("s1 == s2: {0}", s1 == s2);
Console.WriteLine("s1 == Hello!: {0}", s1 == "Hello!");
Console.WriteLine("s1 == HELLO!: {0}", s1 == "HELLO!");
Console.WriteLine("s1 == hello!: {0}", s1 == "hello!");
Console.WriteLine("s1.Equals(s2): {0}", s1.Equals(s2));
Console.WriteLine("Yo.Equals(s2): {0}", "Yo!".Equals(s2));
Console.WriteLine();
}
The C# equality operators perform a case-sensitive, character-by-character equality test on string
objects. Therefore, "Hello!" is not equal to "HELLO!", which is different from "hello!". Also, keeping the
connection between string and System.String in mind, notice that we are able to test for equality using
the Equals() method of String as well as the baked-in equality operators. Finally, given that every string
literal (such as "Yo") is a valid System.String instance, we are able to access string-centric functionality
from a fixed sequence of characters.
c# for dummies: c sharp programming
c# for dummies: c sharp programming: String Concatenation string variables can be connected together to build larger strings via the C# + operator. As you may know, this tec...
c sharp programming
String Concatenation
string variables can be connected together to build larger strings via the C# + operator. As you may
know, this technique is formally termed string concatenation. Consider the following new helper
function:
static void StringConcatenation()
{
Console.WriteLine("=> String concatenation:");
string s1 = "Programming the ";
string s2 = "PsychoDrill (PTP)";
string s3 = s1 + s2;
Console.WriteLine(s3);
Console.WriteLine();
}
string variables can be connected together to build larger strings via the C# + operator. As you may
know, this technique is formally termed string concatenation. Consider the following new helper
function:
static void StringConcatenation()
{
Console.WriteLine("=> String concatenation:");
string s1 = "Programming the ";
string s2 = "PsychoDrill (PTP)";
string s3 = s1 + s2;
Console.WriteLine(s3);
Console.WriteLine();
}
c# for dummies: string manipulation
c# for dummies: string manipulation: Basic String Manipulation Working with the members of System.String is as you would expect. Simply declare a strin...
string manipulation
Basic String Manipulation
Working with the members of System.String is as you would expect. Simply declare a string variable
and make use of the provided functionality via the dot operator. Be aware that a few of the members of
System.String are static members and are, therefore, called at the class (rather than the object) level.
Assume you have created a new Console Application project named FunWithStrings. Author the
following method, which should be called from within Main():
static void BasicStringFunctionality()
{
Console.WriteLine("=> Basic String functionality:");
string firstName = "Freddy";
Console.WriteLine("Value of firstName: {0}", firstName);
Console.WriteLine("firstName has {0} characters.", firstName.Length);
Console.WriteLine("firstName in uppercase: {0}", firstName.ToUpper());
Console.WriteLine("firstName in lowercase: {0}", firstName.ToLower());
Console.WriteLine("firstName contains the letter y?: {0}",
firstName.Contains("y"));
Console.WriteLine("firstName after replace: {0}", firstName.Replace("dy", ""));
Console.WriteLine();
}
Not too much to say here, as this method simply invokes various members, such as ToUpper() and
Contains(), on a local string variable to yield various formats and transformations.
Working with the members of System.String is as you would expect. Simply declare a string variable
and make use of the provided functionality via the dot operator. Be aware that a few of the members of
System.String are static members and are, therefore, called at the class (rather than the object) level.
Assume you have created a new Console Application project named FunWithStrings. Author the
following method, which should be called from within Main():
static void BasicStringFunctionality()
{
Console.WriteLine("=> Basic String functionality:");
string firstName = "Freddy";
Console.WriteLine("Value of firstName: {0}", firstName);
Console.WriteLine("firstName has {0} characters.", firstName.Length);
Console.WriteLine("firstName in uppercase: {0}", firstName.ToUpper());
Console.WriteLine("firstName in lowercase: {0}", firstName.ToLower());
Console.WriteLine("firstName contains the letter y?: {0}",
firstName.Contains("y"));
Console.WriteLine("firstName after replace: {0}", firstName.Replace("dy", ""));
Console.WriteLine();
}
Not too much to say here, as this method simply invokes various members, such as ToUpper() and
Contains(), on a local string variable to yield various formats and transformations.
Tuesday, 13 November 2012
c# for dummies: c sharp programming
c# for dummies: c sharp programming: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StringManipulationPost { class Program ...
Subscribe to:
Posts (Atom)