473,657 Members | 2,707 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 1143
>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.WriteLi ne("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************ ******@discussi ons.microsoft.c om>
wrote in message news:74******** *************** ***********@mic rosoft.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************ ******@discussi ons.microsoft.c om>
wrote in message news:74******** *************** ***********@mic rosoft.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.WriteLi ne (object.Referen ceEquals(string .Empty, ""));
}
}

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

--
Jon Skeet - <sk***@pobox.co m>
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
1784
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. Is there any driver/support in JSP for writing to a text file? E.g. a website that receives many thousand submissions/day via a feedback form - rather than simply send emails, desire is to put them all into one text file per day then email that...
1
1994
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. I'd like to check for a list of "int"s and i don't know how many int's are in this array, it could be
0
1136
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, new-products would be a subset of company-news. So the questions:
5
6550
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 one. How would I do this in Javascript? Many thanks.
1
1319
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 information in niche would grow. This would result in one big file, it's not too much trouble to change it now to mutliply files, the question is what would be better from design and performance perspective? Several other questions:
0
1478
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 leads me to the following questions: 1. I currently use an ISAPI filter to "force" all pages to run through a page called site_template.asp. This file "templates" the pages with common code and a page "skin". This forces all pages (whether...
2
1813
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 data. I’m talked about a process like .NET data grid, where we can iterate thru the rows and change data. 2) I have a report with a linked sub report. The data targets the same data
7
2228
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 control is contained regardless of the type currently displayed without having to use a huge switch somthing like: switch(curentUserType.GetType()) { case "Person": ((person)currentUserControl).LoadFromForm();
9
1305
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 some problems by using session("conMain"), see question 2). I don't know if it's a good thing to do, and not oppenning at each page the
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 include the file together? 2.) If i create a web sites have several hundred of static html pages, however all the pages need to check whether the user is login or not , if not, it will redirect to login page. For this, I think to create a aspx...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.