methodological_instructions (9)

  • doc
  • 02.05.2020
Публикация на сайте для учителей

Публикация педагогических разработок

Бесплатное участие. Свидетельство автора сразу.
Мгновенные 10 документов в портфолио.

Иконка файла материала methodological_instructions (9).doc

Methodological Instructions

Theme: Use of strings

Aim: Use of procedures and functions for processing strings

Assessment criteria

Knowledge

·     Know what is string

 

Understanding

·     Describes declaration and initialization of strings

 

Application

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures

 

Language objectives

Student is able to describe the String type used in C# programming language.

For instance, to work with strings one have to include System namespace reference to program’s code .

Also, student can justify the purpose of function and procedure

Vocabulary and terminology specific to the subject:

string, string operators, comparing stings, symbols of string

Useful expressions for dialogs and letters:

Well, the variable of string type is created by … .

For example, assignment of some value to string variable   … .

Obviously, the stings used for the wide variety of cases like  … .

 

Theory

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0'). The Length property of a string represents the number of Char objects it contains, not the number of Unicode characters. To access the individual Unicode code points in a string, use the StringInfo object.

Declaring and Initializing Strings

You can declare and initialize strings in various ways, as shown in the following example:

 

C#

 

Copy

// Declare without initializing.

string message1;

 

// Initialize to null.

string message2 = null;

 

// Initialize as an empty string.

// Use the Empty constant instead of the literal "".

string message3 = System.String.Empty;

 

//Initialize with a regular string literal.

string oldPath = "c:\\Program Files\\Microsoft Visual Studio 8.0";

 

// Initialize with a verbatim string literal.

string newPath = @"c:\Program Files\Microsoft Visual Studio 9.0";

 

// Use System.String if you prefer.

System.String greeting = "Hello World!";

 

// In local variables (i.e. within a method body)

// you can use implicit typing.

var temp = "I'm still a strongly-typed System.String!";

 

// Use a const string to prevent 'message4' from

// being used to store another string value.

const string message4 = "You can't get rid of me!";

 

// Use the String constructor only when creating

// a string from a char*, char[], or sbyte*. See

// System.String documentation for details.

char[] letters = { 'A', 'B', 'C' };

string alphabet = new string(letters);

Note that you do not use the new operator to create a string object except when initializing the string with an array of chars.

 

Initialize a string with the Empty constant value to create a new String object whose string is of zero length. The string literal representation of a zero-length string is "". By initializing strings with the Empty value instead of null, you can reduce the chances of a NullReferenceException occurring. Use the static IsNullOrEmpty(String) method to verify the value of a string before you try to access it.

 

Immutability of String Objects

String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

 

C#

 

Copy

string s1 = "A string is more ";

string s2 = "than the sum of its chars.";

 

// Concatenate s1 and s2. This actually creates a new

// string object and stores it in s1, releasing the

// reference to the original object.

s1 += s2;

 

System.Console.WriteLine(s1);

// Output: A string is more than the sum of its chars.

Because a string "modification" is actually a new string creation, you must use caution when you create references to strings. If you create a reference to a string, and then "modify" the original string, the reference will continue to point to the original object instead of the new object that was created when the string was modified. The following code illustrates this behavior:

 

C#

 

Copy

string s1 = "Hello ";

string s2 = s1;

s1 += "World";

 

System.Console.WriteLine(s2);

//Output: Hello

 

П. Questions for self-assessment.

What is the string in C#?

How to declare string variable?

How to initialize string variable?

 

Visual Aids and Materials links.

1.      Slides

2.      https://books.google.kz/books?id=_AyCDwAAQBAJ&pg=PT152&lpg=PT152&dq=Use+of+procedures+and+functions+for+processing+the+strings+in+c+sharp&source=bl&ots=_8UzPgU0XS&sig=ACfU3U14ahT5Gh0nOiWZRdAv3GnJZTysKA&hl=ru&sa=X&ved=2ahUKEwiSmZDorYPgAhVLFCwKHfC-ApQQ6AEwBXoECAQQAQ#v=onepage&q&f=false

3.      https://www.c-sharpcorner.com/UploadFile/736ca4/strings-in-C-Sharp/

4.      https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/

 

Students' Practical Activities:

Students must be able:

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures


Посмотрите также