C # basic concepts 25 Question 21-25

Source: Internet
Author: User

21. P/Invoke?
A:
A transaction is generated when the controlled code interacts with the non-controlled code. This usually occurs when Platform Invocation Services (P/Invoke) is used.
For example, if you call System APIs or deal with COM objects, use the System. Runtime. InteropServices namespace
Although Interop is very convenient, it is estimated that 10 to 40 commands will be executed for each transaction call, and there are a lot of overhead. Therefore, we should try to call as few transactions as possible.
If it is not usable, we recommend that you execute multiple actions at a time, instead of executing only a few actions at a time.

22. What is the difference between StringBuilder and String?
A:
A new instance is generated when String operations (such as value assignment and concatenation) are performed, while StringBuilder does not. Therefore, it is best to use StringBuilder when splicing a large number of strings or frequently performing operations on a String. Do not use String
In addition, we have to say a few more for the String:
1. It is a reference type and memory is allocated on the stack.
2. A new instance is generated during the operation.
3. Immutable)

3. Define equal operators (= and! =) Is used to compare the value of a String object (not a reference ).
Example:

Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example22
{
Class Program
{
Static void Main (string [] args)
{
Const int cycle= 10000;
Long vTickCount = Environment. TickCount;
String str = null;
For (int I = 0; I <cycle; I ++)
Str + = I. ToString ();
Console. WriteLine ("String: {0} MSEL", Environment. TickCount-vTickCount );
VTickCount = Environment. TickCount;
// I am angry when I see this variable name. Why do everyone make it? :)
StringBuilder sb = new StringBuilder ();
For (int I = 0; I <cycle; I ++)
Sb. Append (I );
Console. WriteLine ("StringBuilder: {0} MSEL", Environment. TickCount-vTickCount );
String tmpStr1 = "";
String tmpStr2 = tmpStr1;
Console. WriteLine (tmpStr1 );
Console. WriteLine (tmpStr2 );
// Note that the value of tmpStr1 does not affect the value of tmpStr2.
TmpStr1 = "B ";
Console. WriteLine (tmpStr1 );
Console. WriteLine (tmpStr2 );
Console. ReadLine ();
}
}
}
Result:
String: 375 MSEL
StringBuilder: 16 MSEL
A
A

A

23. What is the meaning of explicit and implicit?
A:
Explicit and implicit are conversion operators. Using these two operators allows us to exchange custom types.
Explicti indicates Explicit conversions. For example, forced conversions (B = (B) A) must be performed from A to B)
Implicit indicates implicit conversion. For example, from B-> A, you only need to assign values directly (A = B)
Implicit conversions make our code look more beautiful and more concise and easy to understand, so it is best to use the implicit operator more. However! If the object itself loses some information (such as precision) during conversion, we can only use the explicit IT operator to warn the client caller during compilation.
Example:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example23
{
Class Program
{
// This example is inspired by the classic line "fairy? Monsters ?" -- I can't think of any good examples.
Class Immortal
{
Public string name;
Public Immortal (string Name)
{
Name = Name;
}
Public static implicit operator Monster (Immortal value)
{
Return new Monster (value. name + ": How can a fairy become a Monster? You just need to steal it... ");
}
}
Class Monster
{
Public string name;
Public Monster (string Name)
{
Name = Name;
}
Public static explicit operator Immortal (Monster value)
{
Return new Immortal (value. name + ": Do monsters want to be gods? Continue to cultivate for five hundred years! ");
}
}
Static void Main (string [] args)
{
Immortal tmpImmortal = new Immortal ("Zixia fairy ");
// Implicit conversion
Monster tmpObj1 = tmpImmortal;
Console. WriteLine (tmpObj1.name );
Monster tmpMonster = new Monster ("Sun Wukong ");
// Explicit conversion
Immortal tmpObj2 = (Immortal) tmpMonster;
Console. WriteLine (tmpObj2.name );
Console. ReadLine ();
}
}
}
Result:
Zixia FAIRY: The Fairy becomes a monster? You just need to steal it...
Sun Wukong: Do monsters want to be gods? Continue to cultivate for five hundred years!

24. What is the use of params?
A:
The params keyword is used in the parameter list of the method member to provide the ability to change the number of parameters for this method.
It can appear only once and cannot be followed by parameter definitions.
Example:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace ConsoleApplication1
{
Class App
{
// The first parameter must be an integer, but the number of subsequent parameters is variable.
// Because the object array is specified, all data types can be passed as parameters.
Public static void UseParams (int id, params object [] list)
{
Console. WriteLine (id );
For (int I = 0; I <list. Length; I ++)
{
Console. WriteLine (list [I]);
}
}
Static void Main ()
{
// Variable parameters are input with three parameters, both of which are strings.
UseParams (1, "a", "B", "c ");
// Four parameters are input in the Variable Parameter section, which are string, integer, floating point, and double-precision floating point array.
UseParams (2, "d", 100, 33.33, new double [] {1.1, 2.2 });
Console. ReadLine ();
}
}
}
Result:
1
A

C
2
D
100
33.33
System. Double []

25. What is reflection?
A:
Reflection, Reflection, through which we can obtain various information at runtime, such as assembly, module, type, field, attribute, method, and event
You can perform operations on a type after it is dynamically instantiated.
In simple terms, it is something that can be done at runtime with a string. It is actually a universal factory built in. net framework.
It is generally used to implement plug-in framework programs and design patterns, of course, reflection is a way to make full use of its energy to accomplish anything you want to do (as if you have seen a senior calling a function not described in the official class library using reflection ...)
Example:
Using System;
Using System. Collections. Generic;
Using System. Text;
Namespace Example25Lib
{
Public class Class1
{
Private string name;
Private int age;
// If the parameter-free constructor is explicitly declared, the client can instantiate the class by using the CreateInstance of the Assembly.
// This is not specifically implemented here, so that the reflection Implementation of the constructor can be reflected at the client call end.
// Public Class1 ()
//{
//}
Public Class1 (string Name, int Age)
{
Name = Name;
Age = Age;
}
Public void ChangeName (string NewName)
{
Name = NewName;
}
Public void ChangeAge (int NewAge)
{
Age = NewAge;
}
Public override string ToString ()
{
Return string. Format ("Name: {0}, Age: {1}", name, age );
}
}
}
Reflection instantiates an object and calls its method. Reflection calls of attributes and events are omitted.
Using System;
Using System. Collections. Generic;
Using System. Text;
// Note that the namespace for the reflection is added.
Using System. Reflection;
Namespace Example25
{
Class Program
{
Static void Main (string [] args)
{
// Load the Assembly
Assembly tmpAss = Assembly. LoadFile (AppDomain. CurrentDomain. BaseDirectory + "Example25Lib. dll ");
// Traverse all types in the Assembly and instantiate them
Type [] tmpTypes = tmpAss. GetTypes ();
Foreach (Type tmpType in tmpTypes)
{
// Obtain the constructor information of the first type
ConstructorInfo [] tmpConsInfos = tmpType. GetConstructors ();
Foreach (ConstructorInfo tmpConsInfo in tmpConsInfos)
{
// Generate a set of called parameters for the constructor
ParameterInfo [] tmpParamInfos = tmpConsInfo. GetParameters ();
Object [] tmpParams = new object [tmpParamInfos. Length];
For (int I = 0; I <tmpParamInfos. Length; I ++)
{
TmpParams [I] = tmpAss. CreateInstance (tmpParamInfos [I]. ParameterType. FullName );
If (tmpParamInfos [I]. ParameterType. FullName = "System. String ")
{
TmpParams [I] = "Clark ";
}
}
// Instantiate the object
Object tmpObj = tmpConsInfo. Invoke (tmpParams );
Console. WriteLine (tmpObj );
// Obtain and execute all methods
Foreach (MethodInfo tmpMethod in tmpType. GetMethods ())
{
// Create a parameter set for method calls
TmpParamInfos = tmpMethod. GetParameters ();
TmpParams = new object [tmpParamInfos. Length];
For (int I = 0; I <tmpParamInfos. Length; I ++)
{
TmpParams [I] = tmpAss. CreateInstance (tmpParamInfos [I]. ParameterType. FullName );
If (tmpParamInfos [I]. ParameterType. FullName = "System. String ")
{
TmpParams [I] = "Clark Zheng ";
}
If (tmpParamInfos [I]. ParameterType. FullName = "System. Int32 ")
{
TmpParams [I] = 27;
}
}
TmpMethod. Invoke (tmpObj, tmpParams );
}
// Print the object again after calling the method and compare the result
Console. WriteLine (tmpObj );
}
}
Console. ReadLine ();
}
}
}
Result:
Name: Clark, Age: 0
Name: Clark Zheng, Age: 27
From:
Http://www.cnblogs.com/reonlyrun/archive/2007/04/05/csharp_25_question.html

Related Article

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.