I am fresher in c#.sorry for asing the very basic question.I saw that single comments are like this(//) and page/xml comments are like this (///).is there any difference in this functionality?why cant we use single line comments instead of page/xml comments?
You can use whatever you want within your code. The advantage of the latter is that your comment can be used for IntelliSense or the generation of code documentation. So basically if you are using Visual Studio, whatever you type in a XML comment will appear for that class/method/property etc.
Related
Hi fellow programmers and nerds!
When creating regular expressions Visual Studio, the IDE will highlight the string if it's preceded by a verbatim identifier (for example, #"Some string). This looks something like this:
(Notice the way the string is highlighted). Most of you will have seen this by now, I'm sure.
My problem: I am using a package acquired from NuGet which deals with regular expressions, and they have a function which takes in a regular expression string, however their function doesn't have the syntax highlighting.
As you can see, this just makes reading the Regex string just a pain. I mean, it's not all-too-important, but it would make a difference if we can just have that visually-helpful highlighting to reduce the time and effort one's brain uses trying to decipher the expression, especially in a case like mine where there will be quite a quantity of these expressions.
The question
So what I'm wanting to know is, is there a way to make a function highlight the string this way*, or is it just something that's hardwired into the IDE for the specific case of the Regex c-tor? Is there some sort of annotation which can be tacked onto the function to achieve this with minimal effort, or would it be necessary to use some sort of extension?
*I have wrapped the call to AddStyle() into one of my own functions anyway, and the string will be passed as a parameter, so if any modifications need to be made to achieve the syntax-highlight, they can be made to my function. Therefore the fact that the AddStyle() function is from an external library should be irrelevant.
If it's a lot of work then it's not worth my time, somebody else is welcome to develop an extension to solve this, but if there is a way...
Important distinction
Please bear in mind I am talking about Visual Studio, NOT Visual Studio Code.
Also, if there is a way to pull the original expression string from the Regex, I might do it that way, since performance isn't a huge concern here as this is a once-on-startup thing, however I would prefer not to do it that way. I don't actually need the Regex object.
According to https://devblogs.microsoft.com/dotnet/visual-studio-2019-net-productivity/#regex-language-support and https://www.meziantou.net/visual-studio-tips-and-tricks-regex-editing.htm you can mark the string with a special comment to get syntax highlighting:
// language=regex
var str = #"[A-Z]\d+;
or
MyMethod(/* language=regex */ #"[A-Z]\d+);
(the comment may contain more than just this language=regex part)
The first linked blog talks about a preview, but this feature is also present in the final product.
.NET 7 introduces the new [StringSyntax(...)] attribute, which is used in .NET 7 on more than 350 string, string[], and ReadOnlySpan<char> parameters, properties, and fields to highlight to an interested tool what kind of syntax is expected to be passed or set.
https://devblogs.microsoft.com/dotnet/regular-expression-improvements-in-dotnet-7/?WT_mc_id=dotnet-35129-website&hmsr=joyk.com&utm_source=joyk.com&utm_medium=referral
So for a method argument you should just use:
void MyMethod([StringSyntax(StringSyntaxAttribute.Regex)] string regex);
Here is a video demonstrating the feature: https://youtu.be/Y2YOaqSAJAQ
I have multiple lines of code in my solution that I'd like to remove.
They are, in this particular scenario:
System.Diagnostics.Trace.WriteLine([STRING]);
I could've done a 'Replace All' on System.Diagnostics.Trace.WriteLine but, obviously, I want to remove the entire thing (including the string parameter passed into the method) - I want the whole line of code to be deleted wherever a call to this method is made.
Is there a way in which I can do this (any way to provide a wildcard included in the replace criteria maybe?). Also, could I do this with comments/todo's too?
I appreciate this may seem bad practice to have to get to this point but would, for now, appreciate answers that are only on topic as opposed to answers that try and propose ways of preventing this in the future (which I've already acknowledged).
You can use Regular Expression in Visual Studio Find and Replace dialog, use:
Find: ^[ \t]*(System.Diagnostics.Trace.WriteLine\(.*\)\;).*\n
Replace:
That's will delete the entire line.
You'll need to use the Regular Expression option in the Replace dialog, and something like this should do:
System.Diagnostics.Trace.WriteLine\("\w*"\);
Cheers
EDIT: Obviously, if you are passing variables to WriteLine, just remove the quotes:
System.Diagnostics.Trace.WriteLine\(\w*\);
If you mean to remove all instances of System.Diagnostics.Trace.WriteLine then you could do the following:
Go to Replace in Files dialog box
Enter System.Diagnostics.Trace.WriteLine.* in the find what box
Leave the Replace with box empty
Choose Entire Solution for Look in
Expand Find Options and tick Use and ensure Regular Expressions is shown.
I have a file thats exported with a ton of lines. I want to be able to parse the file into a neat order that I can easily use.
I have a sample string here:
deal reached on cemetery flags
| <img src="/i/redes/icon-video.gif"><br>
- obama skips out on scouts for 'the view'<br>
- <strong class="em">you decide: </strong>right call?</li>"
All of that is 1 line. I'd want it to output like the following:
http://www.foxnews.com/us/2010/07/28/cemetery-allows-father-war-veteran-fly-flag-grave|Deal reached on cemetery flags
http://www.foxnews.com/politics/2010/07/27/obama-missing-historic-boy-scout-jamboree-fundraisers-view-taping|obama skips out on scouts for 'the view
sort of www|description for every href in the line. Hope this makes sense!
I really hate to say this but. Use regular expressions. href="(.*?)" should get you all the hrefs. May need a bit of tweaking to eliminate things you don't want. Here is a decentish article on implementation http://oreilly.com/windows/archive/csharp-regular-expressions.html
Take a look at the Html Agility Pack. The first example deals with hrefs: http://htmlagilitypack.codeplex.com/wikipage?title=Examples&referringTitle=Home.
I have researched this for a few hours and I am kind of frustrated. Maybe I am just missing something as I am new to blogging.
I am not hosting my own blog, I am just using WordPress.com.
I want to include snippets of c# code and have them look like they do in Visual Studio, or at least make them look nice, certainly with line numbers and color.
The solutions I have seen for this all seem to assume you are hosting your own blog.
I cannot figure out how to install plugins.
Is there a widget that will make code snippets look nice, or some other solution I can easily use?
Thank you
EDIT: Sarfraz has outlined one way to solve my problem (thank you!), and I have tried it but there is an issue I have, namely that it does not colorize most of my code (newer keywords like var, from, where, select, etc). Is there a fix to this or is there some other solution?
Just edit your aricles in html mode and enclose your code within these tags.
[sourcecode language="css"]
[/sourcecode]
Example:
[sourcecode language="javascript"]
// javascript hello world program
alert('Hello, World !!');
[/sourcecode]
Note: You need to specify correct language identifier for the language attribute as shown above.
More Information Here :)
The [sourcecode] tag usually works fine for C#, but for me it often breaks when I post XAML code.
Instead I use this page to format my code. The result looks nice (you can see it on my blog), but it requires the "Custom CSS" option ($15/year).
EDIT: actually the [sourcecode] tag works fine, and I'm now using it in all my posts
[code language="csharp"]
//Your code here
[/code]
looks like this has been updated, now you can use
[code language="[the lang you are posting]"]
your code here
[/code]
note: you can shorthand language as lang
[code lang="[the lang you are posting]"]
your code here
[/code]
here is the list of supported languages
Ok, so i'm basically trying to load the contents of a .txt file that contains 1 word per line into a dictionary.
I had no problems doing so when the words in that file were in english, but changing the file to a language with accents, i started having problems.
Had to change the encoding while creating the stream reader, also the culture in the ToLower method while adding the word to the dictionary.
Basically i now have something similar to this:
if (!dict.ContainsKey(word.ToLower(culture)))
dict.Add(word.ToLower(culture), true);
The problem is that words like "esta" and "está" are being considered the same. So, is there any way to set the ContainsKey method to a specific language or do we need to implement something in the lines of a comparable? Either way i'm kinda new to c# so i would apreciate an example please.
Another issue submerge with the new file... after like a hundred words it stops adding the rest of the file, leaving a word incomplete... but i cant see any special chars in that word to end the execution of the method, any ideas about this problem?
Many thanks.
EDIT:
1st Problem solved using Jon Skeet sugestion.
In regards of the 2nd problem:
Ok, changed the file format to UTF8 and removed the encoding in the stream reader since it now recognizes the accents just right. Testing some stuff regarding the 2nd issue now.
2nd problem also solved, it was a bug on my part... the shame...
Thnks for the quick answers everyone, and especially Jon Skeet.
I assume you're trying to get case insensitivity for the dictionary. Instead of calling ToLower, use the constructor of Dictionary which takes an equality comparer - and use StringComparer.Create(culture, true) to construct a suitable comparer.
I don't know what your second problem is about - we'd need more detail to diagnose it, including the code you're using, ideally.
EDIT: UTF-7 is almost certainly not the correct encoding. Don't just guess at the encoding; find out what it's really meant to be. Where did this text file come from? What can you open it successfully in?
I suspect that at least some of your problems are due to using UTF-7.
The problem is with the enconding you are using when opening the file to read. Looks like you may be using ASCIIEncoding.
.NET handles strings internally as UTF-8, so this kind of issue would not happen internally.