Several questions of C#

  • Thread starter Thread starter vicmann
  • Start date Start date
V

vicmann

1. When and why a string defined with or without "@" in front of it?
string sSample = "xxxx";
string sSample = @"xxxx";

2. Any difference between the following two?
string sSample = "";
string sSample = string.Empty;

3. Given an Interface IMyInterf and it derived class MyCls.

IMyInterf MyObj = null;
MyObj = new MyCls();
....
Can someone show an example code of passing this MyObj to a function by
reference?

Thanks for your help!
 
1. When and why a string defined with or without "@" in front of it?
string sSample = "xxxx";
string sSample = @"xxxx";

@ is used to create a verbatim string literal where \ isn't treated as
an excape character. It's often useful when storing file paths and
things like that in a string.

@"""foo\bar""" == "\"foo\\bar\""

2. Any difference between the following two?
string sSample = "";
string sSample = string.Empty;

The end result is the same, it's mostly a matter of personal
preference.

3. Given an Interface IMyInterf and it derived class MyCls.

IMyInterf MyObj = null;
MyObj = new MyCls();
...
Can someone show an example code of passing this MyObj to a function by
reference?

YourMethod(ref MyObj);


Mattias
 
Hello vicmann,

v> 1. When and why a string defined with or without "@" in front of it?
v> string sSample = "xxxx";
v> string sSample = @"xxxx";

When u need to use "\\" symb in string, for example specify the path.

if u just point "\\" in string u get the single "\", because \ is the service
digit pointed than the next symb should be kept - like \"

If u need to have both \\ in string the easiest way to set @ before your
string (u even can use "\\\" without @ but readability is terrible

v> 2. Any difference between the following two?
v> string sSample = "";
v> string sSample = string.Empty;

For reading convenience - u not mix it up with sSample = "'" ( ' in the string)
and to have the unique property for the empty string (because maybe in some
..net platforms "" couldn't be the emply string).

v> 3. Given an Interface IMyInterf and it derived class MyCls.
v>
v> IMyInterf MyObj = null;
v> MyObj = new MyCls();
v> ...
v> Can someone show an example code of passing this MyObj to a function
v> by
v> reference?

just use ref keyword in the signature Method(ref MyObl)

---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hi,
1. When and why a string defined with or without "@" in front of it?
string sSample = "xxxx";
string sSample = @"xxxx";

The @ character tells the C# compiler to treat any escape characters in the
string literal as real characters. For instance...

string sSample = @"C:\Test"

In the above string literal \ will _not be_ treated as escape character.
Without the use of @ you should declare the above string as follows.

string sSample = "C:\\Test"
2. Any difference between the following two?
string sSample = "";
string sSample = string.Empty;

There is no difference. Both will result in a string object with length of
0. But there is an efficientcy difference in the above two approaches. Check
this blog by Brad Abrams.
http://blogs.msdn.com/brada/archive/2003/04/22/49997.aspx

3. Given an Interface IMyInterf and it derived class MyCls.

IMyInterf MyObj = null;
MyObj = new MyCls();
....
Can someone show an example code of passing this MyObj to a function by
reference?

public interface IX
{
void Display();
}
public class CX : IX
{
public void Display() { Console.WriteLine("Display"); }
}

public static Test(ref IX x)
{
x = new CX();
}

public static void MyFunc()
{
IX x = null;
Test(ref x);
x.Display(); // x now points to the newly created CX object in the Test()
method
}

Hope this helps.
 
Hi vicmann,

In addition to the other posts there is another function of @. Setting @
in front of a string lets you write on several lines without having to do
string concatenation

string s = @"Line1
Line2
Line3";
 
Mark said:
However, Brad didn't address the comment by Thong Nguyen in which Thong felt
that the JIT would optimize the constant "" into the same CLR emission as
String.Empty.

///ark

Are we even sure the String Intern pool isn't already pre-constructed
String objects, or a reasonable approximation thereof? After all,
considering that Strings are immutable, then the interned strings could
actually be statics that the garbage collector completely ignores. In
that case, fetching the interned string object would very likely
actually get you the same instance as String.Empty... hmm, that can
probably be checked, can't it?
 
Martin Z said:
Are we even sure the String Intern pool isn't already pre-constructed
String objects, or a reasonable approximation thereof? After all,
considering that Strings are immutable, then the interned strings could
actually be statics that the garbage collector completely ignores. In
that case, fetching the interned string object would very likely
actually get you the same instance as String.Empty... hmm, that can
probably be checked, can't it?

Yes, but the answer is that it depends on the framework:

using System;

class Test
{
static void Main()
{
Console.WriteLine (object.ReferenceEquals(string.Empty, ""));
}
}

prints "False" on 2.0 for me, but "True" for 1.1...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top