Excerpt-it Enterprise must read 200. NET face Questions-04 use of strings, collections, and streams

Source: Internet
Author: User

String processing

Q:system.string is a value type or a reference type

System.String is a reference type whose object cannot be modified after initialization, and any attempt to modify the string object will result in a new string object being generated.

Q: How to convert between string and byte[] objects

The conversion of strings and byte arrays depends on the use of encoding, and different encoding standards will be performed using different algorithms. The System.Text.Encoding type provides support for most common encoding algorithms for converting between strings and byte arrays. Here is the code example:

classProgram {Static voidMain (string[] args) {            strings ="I am the string, I am string"; byte[] UTF8=Stringtobyte (S, Encoding.UTF8); Byte[] gb2312= Stringtobyte (S, encoding.getencoding ("GB2312")); Byte[] Unicode=Stringtobyte (S, Encoding.unicode); Console.WriteLine (UTF8.            Length); Console.WriteLine (gb2312.            Length); Console.WriteLine (Unicode.            Length);            Console.WriteLine (bytetostring (UTF8, Encoding.UTF8)); Console.WriteLine (Bytetostring (gb2312, encoding.getencoding ("GB2312")));            Console.WriteLine (Bytetostring (Unicode, Encoding.unicode));        Console.ReadLine (); }        //Convert a string to a byte array        StaticByte[] Stringtobyte (strings, Encoding Encoding) {            returnencoding.        GetBytes (s); }        //convert a byte array to a string        Static stringbytetostring (byte[] B, Encoding Encoding) {returnencoding.        GetString (b); }    }

How instances of q:securestring are allocated and freed

The system.securestring provides an encrypted string type. Its objects are allocated in unmanaged memory and are saved in an encrypted form. For securestring operations are character-by-word, securestring is responsible for decrypting and encrypting the operation.

Q: What is the string pooling mechanism

The purpose of the string pooling mechanism is to improve the performance of the program. The CLR preserves a collection of string objects that appear in the program, and when a new string is required, first checks the existing collection and returns a reference to the existing object when the lookup succeeds.

Common collections and generics

Q:int[] is a reference type or a value type

Array types are family types, all of which inherit from System.Array, and System.Array inherit from System.Object. All array types are reference types.

Q: How to convert between arrays

An array type can be implicitly converted if the array dimension must be the same, the target project type and the source project type must have an implicit or explicit conversion relationship, and the project type of the source array is not a value type. The array type can be transformed by the Array.convertall method, which requires the user to provide a conversion algorithm that is passed into the Array.convertall method in the form of a delegate or method.

Q: Explaining the fundamentals of generics

Generics are similar to templates in C + +, allowing programmers to define more general types and algorithms, and then generate concrete enclosing types when they are used. All types with generic parameters are an open type, which cannot be instantiated, but have other characteristics of all enclosing types.

Q: What are the primary and secondary constraints of generics

Each generic parameter can have at most one primary constraint, and the primary constraint for generics is that the specified generic parameter must be or inherit from a reference type, with two special primary constraints: class and struct, representing the generic parameter as a reference type and a value type, respectively. Each generic parameter can have an infinite number of minor constraints, and the syntax for the secondary and primary constraints is essentially the same, but it specifies that a generic parameter must implement the interface specified by all secondary constraints.

Flow and serialization

Q: What is a stream,. What are the common streams in

A stream is an operation for a byte collection object: The common flow types in net are FileStream, NetworkStream, MemoryStream, and so on.

What is the effect of q:serializable characteristics

By adding the serializable attribute to a type, you can declare an object to be serializable and be serialized and deserialized by objects of a type such as BinaryFormatter.

Q: How to customize the serialization and deserialization process

You can implement custom serialization by implementing the GetObjectData method in the ISerializable interface, and you can customize the process of deserialization by adding a construction method with SerializationInfo and StreamingContext parameters.

Excerpt-it Enterprise must read 200. NET face Questions-04 use of strings, collections, and streams

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.