Click here to Skip to main content
15,914,386 members
Home / Discussions / C#
   

C#

 
GeneralRe: Client 2 Client File Encryption Pin
The Junior28-Feb-18 3:39
The Junior28-Feb-18 3:39 
GeneralRe: Client 2 Client File Encryption Pin
Eddy Vluggen28-Feb-18 3:53
professionalEddy Vluggen28-Feb-18 3:53 
GeneralRe: Client 2 Client File Encryption Pin
The Junior28-Feb-18 4:08
The Junior28-Feb-18 4:08 
GeneralRe: Client 2 Client File Encryption Pin
Eddy Vluggen28-Feb-18 5:33
professionalEddy Vluggen28-Feb-18 5:33 
GeneralRe: Client 2 Client File Encryption Pin
OriginalGriff28-Feb-18 1:43
mveOriginalGriff28-Feb-18 1:43 
GeneralRe: Client 2 Client File Encryption Pin
The Junior28-Feb-18 1:51
The Junior28-Feb-18 1:51 
AnswerRe: Client 2 Client File Encryption Pin
Gerry Schmitz28-Feb-18 7:15
mveGerry Schmitz28-Feb-18 7:15 
QuestionRemove duplicates Pin
Shankar M27-Feb-18 20:14
Shankar M27-Feb-18 20:14 
AnswerRe: Remove duplicates Pin
OriginalGriff27-Feb-18 21:04
mveOriginalGriff27-Feb-18 21:04 
GeneralRe: Remove duplicates Pin
Shankar M27-Feb-18 22:14
Shankar M27-Feb-18 22:14 
GeneralRe: Remove duplicates Pin
OriginalGriff27-Feb-18 22:26
mveOriginalGriff27-Feb-18 22:26 
AnswerRe: Remove duplicates Pin
Bernhard Hiller27-Feb-18 21:22
Bernhard Hiller27-Feb-18 21:22 
GeneralRe: Remove duplicates Pin
Shankar M27-Feb-18 21:55
Shankar M27-Feb-18 21:55 
GeneralRe: Remove duplicates Pin
Pete O'Hanlon28-Feb-18 0:25
mvePete O'Hanlon28-Feb-18 0:25 
GeneralRe: Remove duplicates Pin
Shankar M27-Feb-18 22:14
Shankar M27-Feb-18 22:14 
QuestionAutomatic unit tests Pin
Hila Berger27-Feb-18 0:01
Hila Berger27-Feb-18 0:01 
AnswerRe: Automatic unit tests Pin
Pete O'Hanlon27-Feb-18 1:27
mvePete O'Hanlon27-Feb-18 1:27 
GeneralRe: Automatic unit tests Pin
Hila Berger1-Mar-18 0:10
Hila Berger1-Mar-18 0:10 
GeneralRe: Automatic unit tests Pin
Pete O'Hanlon1-Mar-18 0:32
mvePete O'Hanlon1-Mar-18 0:32 
AnswerRe: Automatic unit tests Pin
Eddy Vluggen27-Feb-18 2:02
professionalEddy Vluggen27-Feb-18 2:02 
GeneralRe: Automatic unit tests Pin
Hila Berger1-Mar-18 0:12
Hila Berger1-Mar-18 0:12 
GeneralRe: Automatic unit tests Pin
Eddy Vluggen1-Mar-18 0:28
professionalEddy Vluggen1-Mar-18 0:28 
AnswerRe: Automatic unit tests Pin
Bohdan Stupak27-Feb-18 4:37
professionalBohdan Stupak27-Feb-18 4:37 
QuestionFinally! I found a valid use for the ref keyword Pin
Foothill26-Feb-18 10:57
professionalFoothill26-Feb-18 10:57 
Not really a question but I would like some feedback.

I have been programming in C# for years now and until today I never could find a way to use the ref keyword in a syntax-correct way. I'm writing code to build XML based on data classes for what must be the thousandth time. The simplest way to generate the data is in plain old strings but I needed a fast way to check for reserved XML characters and update the string only if necessary. Strings are immutable and I don't want to have to copy the string if necessary as this just adds overhead so I came up with this:
C#
public static bool ContainsReservedXmlCharacters(string value)
{
 // This array can be stored in a static class so it's only created once
 char[] reservedCharacters = new char[] { '<', '>', '&', '%' };  

 for (int i = 0; i < value.Length; ++i)
 {
  if (Array.Exists(reservedCharacters, delegate (char c) { return c.Equals(value[i]); }))
    return true;
 }

 return false;
}

public static void ValidateForXml(ref string value)
{
 if (ContainsReservedXmlCharacters(value))
 {
  value = String.Format("<![CDATA[{0}]]>", value);
 }
}

This way, I can check the string if contains characters that it shouldn't and only replace the string if necessary. Experience has taught that reserved characters do sneak into XML from time to time but not that often. This should save some parsing time as the string is copied infrequently. I could even write the function to escape the reserved characters if needed.

If there is still a use for ref, it is with handling immutable types such as strings in functions where they may or may not be replaced with a new value.

What does the community think about this?
if (Object.DividedByZero == true) { Universe.Implode(); }
Meus ratio ex fortis machina. Simplicitatis de formae ac munus. -Foothill, 2016

AnswerRe: Finally! I found a valid use for the ref keyword Pin
Richard Deeming26-Feb-18 11:57
mveRichard Deeming26-Feb-18 11:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.