.NET interview questions: - Will the below codes create new instances?

14 0 0
                                    

string str = ".NET interview questions"; // Instance 1

string str1 = ".NET  interview questions"; // points to same Instance 1   

No, the above code will not create multiple instances but rather they will point to the same memory instance. If the compiler finds the same value in memory he points the variable to the same instance rather than creating a new one.

The below two code snippets will create multiple memory instances of the variable.

string str = ".NET interview questions"; // Instance 1

string str1 = ".NET  interview questions"; // points to different instances. 

string str = ".NET interview questions"; // Instance 1

string str1 = "c# interview questions"; // points to different instances

You can check if they point to the same memory instance or multiple instances by using “ReferenceEqual” which belongs to “object” type. It returns true if the objects point to the same instance.

object.ReferenceEquals(str, str1);

You can also visit our site for more .NET interview question with answers at www.questpond.com 

You can also see this link c# interview question video: - What kind of questions are asked in .NET interviews?

 

.NET interview questions: - Will the below codes create new instances?Where stories live. Discover now