A C or C++ string is nothing more than an array of characters, so the applicant programmer has to do a lot of plan just to copy one cord to addition or to concatenate two strings. In fact, for a bearing of C++ programmers, implementing a cord chic that wraps up the blowzy data of these operations was a rite of passage requiring abounding hours of teeth gnashing and arch scratching. Visual Basic programmers have a somewhat easier life, with a string type, and Java people have it even better, with a String class that is in many ways very similar to a C# string. C# recognizes the cord keyword, which beneath the awning is translated to the .NET class, System String . With it, operations like cord chain and cord artful are a snap.
.
string str1 = “Hello “;
string str2 = “World”;
string str3 = str1 + str2; // string concatenation
In C#, the string keyword is an alias for String. So, String and string are equivalent, and one can use whichever naming convention he prefer. The String class provides many methods for safely creating, manipulating, and comparing strings. In addition, the C# language also provide overloads of operators to simplify common string operations.
Despite this accomplishment of assignment, band is a inadvertence type. Behind the scenes, a string is allocated on the heap, not the stack, and if you accredit one band approximate to accretion string, you get two references to the aloft band in memory. However, with band there are some differences from the accustomed behavior for advertence types. For example, should you accomplish changes to one of these strings, this will accomplish an in fact new band object, abolishment the added band unchanged. Consider the afterwards code.
using System;
class StringExample
{
public static int Main()
{
string s1 = “a string”;
string s2 = s1;
Console.WriteLine(“s1 is “ + s1);
Console.WriteLine(“s2 is “ + s2);
s1 = “another string”;
Console.WriteLine(“s1 is now “ + s1);
Console.WriteLine(“s2 is now “ + s2);
return 0;
}
}
The output from this is:
s1 is a string
s2 is a string
s1 is now another string
s2 is now a string
Changing the bulk of s1 had no aftereffect on s2 , adverse to what you apprehend with a advertence type! What`s blow achievement is that if s1 is initialized with the bulk a bond , a new bond commodity is allocated on the heap. If s2 is initialized, the advertence believability to this above object, so s2 aswell has the bulk a bond . However, if you now change the bulk of s1 , instead of replacing the ancient value, a new commodity will be allocated on the affluence for the new value. The s2 arbitrary will still point to the ancient object, so its bulk is unchanged.
Interview Questions: Implement Same Name Method of two Interfaces in a Class using C#
-
This is most famous question in OOPs programming. if you have two
interfaces and driving a class and these two interfaces have the same name
method then ...
0 comments:
Post a Comment