methodological_instructions (5)

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

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

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

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

Methodological Instructions

Theme: Use of strings

Aim: Use of procedures and functions for processing strings

Assessment criteria

Knowledge

·     Know how to display Date in a string

 

Understanding

·     Describes Dynamic Strings In C#

 

Application

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures

 

Analysis

·     Analyzes given program to complete it

 

Language objectives

Student is able to describe that memory, which is allocated for the string, cannot change. So, strings are immutable.

For instance, while concatenating strings the old string exists in memory until garbage collection occur, that’s resource consuming.

Thus, student has to work out (conclude) the implementation of StringBuilder  Class and its methods.

Also, student can justify the purpose of function or procedure used for processing string.

Vocabulary and terminology specific to the subject:

string, string operators, comparing stings, substring, append, index , length of the substring, StringBuilder  Class, date

Useful expressions for dialogs and letters:

Well, to work with dynamic strings … .

For example, AppendFormat method specifies a format for the … .

As a matter of fact, the stings are used in the wide variety of cases to  …

 

 

Theory

 

StringBuilder In C#

As we all know that the strings are immutable, because after a string is stored in the memory, the memory, which is allocated for the string, cannot change.

If the string changed, a new memory location is needed to store the changed string. In these circumstances, we use StringBuilder class with a string type.

For example, let’s look at the example given below for clear understanding 

1.   Int amount=26;  

2.   String s1=”My age is ”;  

3.   S1=string.concat (s1,amount.ToString());   

In the code given above, s1 is created and it changed, so old and new s1 will be stored temporarily in memory. The old s1 will be cleared from the memory by a garbage collection process. If our Application frequently changes the string, then obviously we may hold large memory in use, while waiting for the next garbage collection to clean up the memory.

Concatenating strings

The string.Concat method creates a new string and concatenates with the result of the ToString method and then stores the result in the new memory location, which is then linked to s.

This means, we have two strings when we need only one. Also, in real time scenario, we may work with multiple strings. Let’s say that we had a situation to concatenate the strings in a loop. This situation can be both a performance and memory problem

Thus, to overcome this issue, we need to implement String.Builder class in System.Text namespace.

When to use StringBuilder Class

If we concatenate the string in for loop, where a large number of strings are stored in the memory, then it is recommended to use StringBuilder Class.

StringBuilder also acts like the collection classes. It allocates an initial value of 16 characters and if your string becomes larger than this, it automatically grows to accommodate the string size.

We can implement string builder, as shown below. 

1.   int amount =21;  

2.   StringBuilder sb= new StringBuilder("My Balance is ");  

3.   sb.append(amount);   

The code given above contains only one string, which is referenced by sb.

Methods of StringBuilder class

There are 5 important methods in stringbuilder class, as shown below.

  1. Append
  2. AppendFormat
  3. Insert
  4. Remove
  5. Replace

Append

This method places an item at the end of current StringBuilder.

AppendFormat

This method specifies a format for the object.

Insert

Places the object at the specific index.

Remove

As the name indicates, it removes the characters.

Replace

As the name indicates, it replaces the characters.

Examples of Each method

Let us create an object, as shown below.

  1. StringBuilder sb= new StringBuilder(“ABCD”);  

APPEND

  1. Way of Appending
    s.Append(“EF”);

  2. Output
    ABCDEF

APPEND FORMAT

  1. Way to apply appendformat
    s.AppendFormat (“{0:n}”,1100);

  2. Output
    ABCDEF1,100.00

INSERT

  1. Way to insert an object
    s.Insert(2,”Z”);

  2. Output
    ABZCDEF1,100.00

REMOVE

  1. Way to Remove the object
    s.Remove(7,6);

  2. Output
    ABZCDEF00

REPLACE

  1. Way to Replace an object
    s.Replace(“0”,”x”);

  2. Output
    ABZCDEFXX

Interview Question

After executing the code given below, how can you preserve computer memory?
 

1.   for(int i=0; i<1000;i++)  

2.   {  

3.       s=s.Concat(s,i.ToString());  

4.   }   

Ans 

1.   StringBuilder= new StringBuilder();  

2.   for(int i=0; i<1000;i++)  

3.   {  

4.       s.Append(i);  

5.   }   

 

 

 

 

П. Questions for self-assessment.

How do you understand the phrase: ‘strings are immutable’?

What happens if the string has changed?

Explain the principle of memory allocation by StringBuilder class (how the memory  is allocated by StringBuilder in case when string has changed)

 

Visual Aids and Materials links.

1.      Slides

2.      https://www.c-sharpcorner.com/article/how-to-create-dynamic-strings-in-c-sharp/

 

Students' Practical Activities:

Students must be able:

·     Write code for processing the strings using functions

·     Write code for processing the strings using procedures


Скачано с www.znanio.ru

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