473,503 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Several questions of C#

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!
Oct 25 '06 #1
7 1133
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Oct 25 '06 #2
Hello vicmann,

v1. When and why a string defined with or without "@" in front of it?
vstring sSample = "xxxx";
vstring 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

v2. Any difference between the following two?
vstring sSample = "";
vstring 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).

v3. Given an Interface IMyInterf and it derived class MyCls.
v>
vIMyInterf MyObj = null;
vMyObj = new MyCls();
v...
vCan someone show an example code of passing this MyObj to a function
vby
vreference?

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

Oct 25 '06 #3
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/.../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.

--
Regards,
Aditya.P
"vicmann" wrote:
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!
Oct 25 '06 #4
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";
--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 25 '06 #5
"Adityanand Pasumarthi" <Ad******************@discussions.microsoft.com>
wrote in message news:74**********************************@microsof t.com...
>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/.../22/49997.aspx
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
Oct 25 '06 #6

Mark Wilden wrote:
"Adityanand Pasumarthi" <Ad******************@discussions.microsoft.com>
wrote in message news:74**********************************@microsof t.com...
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/.../22/49997.aspx

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?

Oct 25 '06 #7
Martin Z <ma***********@gmail.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1768
by: michela rossi | last post by:
Hi, Don't know if anyone can help me. I've got the following questions: 1. Does anyone know of anywhere that offers shared webspace which has support for Java/JSP? Ideally running Tomcat? 2. ...
1
1988
by: Torsten Mohr | last post by:
Hi, i'd like to write an extension module and use PyArg_ParseTuple in that to parse the parameters. I have several questions related to this that i did not find in the documentation: 1....
0
1131
by: Victor Porton | last post by:
Suppose I create several RSS channels: http://example.com/company-news.rdf http://example.com/new-products.rdf http://example.com/product-updates.rdf etc. They are interrelated: for example,...
5
6538
by: NotGiven | last post by:
I have a form with several questions. Within each question there are several checkboxes. I need to ensure that the user checks at least one checkbox. They can check more but must check at least...
1
1307
by: Ayende Rahien | last post by:
I'm storing my data inside an XML file, the data is divided into several niches. I expect two things to happen during normal use of the application: A> Number of niches to grow. B> Amount of...
0
1473
by: Chad A. Beckner | last post by:
I am starting to work on implementing ASP.NET (using VS.NET Dev 2003) into our current ASP 3.0 intranet setup. We have several (say 15 - 20) "applications" that are run within our intranet, which...
2
1806
by: Tiago Miguel Silva | last post by:
Hi there to all. I am a .NET Crystal newbie and I hope that you don’t pick me up much :) with the following questions: 1) It’s possible to access the report model to alter the rendered...
7
2218
by: Byron | last post by:
I have several user controls that have a few methods in common, such LoadFromForm() which populates an object from controls on the form. I want to call that method from the form in which the...
9
1294
by: eitan | last post by:
Hello, I am using Microsoft Visual Studio 2003 .NET. I have several question, please. 1) I have a connection to the database, which I create it at login, by application("conMain") (I have...
1
221
by: Joe | last post by:
Dear all, I have many questions, please help! 1.) If i use the backup/restore function in the IIS, does the backup includes only the register and settings of web sites in the IIS only, or does it...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.