String Data Type in C# Programming

lecture set 4 n.w
1 / 22
Embed
Share

Explore the string data type in C# programming, including value types vs. reference types, creating and manipulating string variables, using expressions and constants, working with numeric data types, and converting between different data types. Learn about the special treatment of strings in .NET, indexing, and storing Unicode characters. Discover the power of strings in displaying output and their various methods.

  • C#
  • Programming
  • String Data Type
  • Value Types
  • Reference Types

Uploaded on | 2 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Lecture Set 4 Data Types and Variables Part C The String Data Type Value Reference Types More on Type Conversion

  2. Objectives Describe different value types and reference types Declare and use string variables Create user-defined constants (literals) and use expressions Work with numeric data types and convert data among these types and between numeric and string data types 3/20/2025 9:36 AM Slide 2

  3. Introduction to the String Data Type The CTS provides a System.String class This is a reference type, not a value type When you declare a string you are creating a reference on the call stack and NOT a value variable Data is represented as a string of characters Lots of powerful methods, too Strings can be used to display output Strings get special treatment in .NET 3/20/2025 9:36 AM Slide 3

  4. Special Treatment for Strings Don t need to use new to create a string reference just assign the string string s1 = hello ; // String literal string s2 = ; // Empty string So string looks like a value type, but it isn t Indexing is zero-based char1 = s.s1[0]; // Gets first character char4 = s.s1[3]; // Gets fourth character charLast = s.s1[s.length 1]; 3/20/2025 9:36 AM Slide 4

  5. Walking Through a String char c; string s = My Best Student ; for each (c in s) { System.Console.Writeline(c); } // end for loop 3/20/2025 9:36 AM Slide 5

  6. Storage of the String Data Type Strings are stored as Unicode characters in two bytes as an unsigned short (UShort) The first 127 values store letters, digits, and special characters The values 128 to 255 are for special characters such as keyboard arrows The values 256 to 65,536 are for international characters and diacritical marks (such as those often found in other languages) All characters have a numeric value you can use Google for example to get these values, but you really need to know (for the most part) that a < b , A < B < < Z , 1 < 9 etc 3/20/2025 9:36 AM Slide 6

  7. Representation of a Unicode char (optional) 3/20/2025 9:36 AM Slide 7

  8. Value Types and Reference Types A data type can be characterized as a value type or a reference type Value types store data in the memory allocated to the variable Value types include primitive types (seen earlier) as well as structures and enumerators Reference types store a memory address which references a value or a collection of values 3/20/2025 9:36 AM Slide 8

  9. Value Types All value types derive from the System.ValueType class Integral and floating-point data types are value types and so are all the other primitive types we examined earlier There are other value types: structs and enumerations Value variables represent an allocation of memory that holds values Reference variables hold references Any type not inheriting from a value type is a reference type (arrays, collections, strings, etc) 3/20/2025 9:36 AM Slide 9

  10. Reference Variables 1 // User defined classes public class human { private string name; private string email; public void sendEmail() { // *** Send email to a Human } // end sendEmail } // end class human public class student : human { ... private int TuID; } // end class student 3/20/2025 9:36 AM Slide 10

  11. Reference Variables 2 (for class Student) public class MyApp { public void mine() { int Age = 18; double GPA; student SRef1 = new student(); student SRef2; } // end function mine } // end class MyApp Lots of things to note in the corresponding picture. -- A reference variable never holds the instance itself (or a value). It just holds a reference to an instance of an object. -- On the other hand, a variable based on a value type always holds an instance of a value that is, a value of the given type -- IF YOU DO NOT EXPLICITLY CREATE AN OBJECT AND STORE ITS REFERENCE IN THE REFERENCE VARIABLE YOU will have TROUBLEs when you try to reference the object. (What kind of trouble?) 3/20/2025 9:36 AM Slide 11

  12. Reference Variables 3 String Object Main Student Object Age 18 GPA 0.0 Name SRef1 Email nothing SRef2 TuID 902456782 String Object Managed Heap 3/20/2025 9:36 AM

  13. Reference Variables - 4 What does this code do? Draw picture!! string s1 = piggybank ; string s2 = s1; string s3; Use of the managed heap ? 3/20/2025 9:36 AM Slide 13

  14. Reference Types The value stored in a reference type variable is a memory address This memory address references (points to) the memory allocated to the actual data Thus, the term pointer is often used to describe a reference type we prefer the term reference In a sense a reference is a managed pointer 3/20/2025 9:36 AM Slide 14

  15. The Object Class The object keyword maps directly onto the System.Object class The object class lies at the root of the CTS hierarchy Any type (value or reference) may be converted to the object type (but not vice versa when strict type checking is enabled) The object class and hence all other classes are reference types 3/20/2025 9:36 AM Slide 15

  16. Reference Types (objects) Examples (VB) Example 1: Private Dictionary As New DictionaryClass ... newWord = Dictionary.getWord() Example 2: Friend WithEvents btnYes As System.Windows.Forms.Button ... Me.btnYes = New System.Windows.Forms.Button ... Me.btnYes.Location = New System.Drawing.Point(24, 56) Me.btnYes.Name = "btnYes" Me.btnYes.TabIndex = 6 Me.btnYes.Text = "Yes" or you could write WithMe.btnYes .Location = New System.Drawing.Point(24, 56) .Name = "btnYes" .TabIndex = 6 .Text = "Yes" End With 3/20/2025 9:36 AM Slide 16

  17. Storage of Reference Types (VB) 3/20/2025 9:36 AM Slide 17

  18. Boxing (optional but take a peek it will come up later) Boxing commonly occurs when you assign a value type to an object type. This triggers the CLR to box the value. Simple example: // *** create value on the stack int val1 = 42; // *** assign value to object variable object ref1 = var1; Here val1 is a value type variable created on the call stack. When you assign this value to the object variable ref1, the CLR boxes the value by copying it into a newly created wrapper object on the managed heap, as shown in next . A reference to this wrapper object is then assigned to the object variable ref1. In the end, two distinct instances of the value 42 exist. This will become important, especially later when we attempt to understand inheritance as it pertains to primitive types 3/20/2025 9:36 AM Slide 18

  19. Boxing (the picture optional VB) Programmers do not have to write code to explicitly perform a boxing operation. Instead, the Visual Basic .NET compiler automatically determines when a boxing operation is required and generates the appropriate IL. From the perspective of the programmer, boxing occurs transparently behind the scenes. 3/20/2025 9:36 AM Slide 19

  20. The Convert Class Provides additional mechanisms for type conversion one shared methods for each of the primitive types, for example ToDouble, ToDecimal, ToInt32, ToChar, ToBool These are shared methods that you can use to convert a value of any data type to a value of another provided the source type is convertible to the target type Can lead to exceptions if not properly used For example Convert.ToInt32( 123 ) is OK Convert.ToInt32( 1B3 ) is not OK. Why not? 3/20/2025 9:36 AM Slide 20

  21. The Convert Class ToString Method Can be used to convert any argument to a string for example, to then be written to a Text Box named txtPrice Dim price as decimal = 432.11 txtPrice.Text = _ Convert.ToString(Price) The result is the entry of the string 432.11 into the text box txtPrice Slide 21 3/20/2025 9:36 AM

  22. Built-In Functions versus Methds Built-In or intrinsic functions cannot be qualified using a namespace and class reference They are simply called (the old fashioned way), passed argument(s), and return a value 3/20/2025 9:36 AM Slide 22

More Related Content