https://medium.com/@EdwhardT/covariant-and-contravariant-types-made-simple-4ab6effb031b
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Monday, September 21, 2015
Monday, September 14, 2015
How to swap two numbers without third variable?
int first,second ;
first = 1;
second = 2;
first = first + second;
second = first - second;
first = first - second;
first = 1;
second = 2;
first = first + second;
second = first - second;
first = first - second;
Tuesday, July 28, 2015
Friday, May 29, 2015
Monday, May 11, 2015
C# Safe Navigation Operator ?.
Read code to understand:
var g1 = parent?.child?.child?.child;
if (g1 != null) // TODO
http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx
var g1 = parent?.child?.child?.child;
if (g1 != null) // TODO
http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx
Wednesday, May 6, 2015
How to solve problem "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at"
To my case:
The password is wrong.
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
The password is wrong.
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("myusername@gmail.com", "mypwd"),
EnableSsl = true
};
client.Send("myusername@gmail.com", "myusername@gmail.com", "test", "testbody");
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Tuesday, April 28, 2015
Wednesday, April 22, 2015
How to create a SFTP server on the fly for unit test?\
Usage:
NuaneSFTPServer server = new NuaneSFTPServer("username", "Passw0rd!", SftpRootFolder, 990);
server.Start();
server.Stop();
public class NuaneSFTPServer
{
volatile SftpServer sFTPserver = null;
public string UserName;
public string Password;
public string FtpPath;
public int PortNumber;
private static Thread workerThread;
public NuaneSFTPServer(string userName, string password, string ftpPath, int portNumber)
{
UserName = userName;
Password = password;
FtpPath = ftpPath;
PortNumber = portNumber;
SshKey rsaKey = SshKey.Generate(SshKeyAlgorithm.RSA, 1024);
SshKey dssKey = SshKey.Generate(SshKeyAlgorithm.DSS, 1024);
// add keys, bindings and users
sFTPserver = new SftpServer();
sFTPserver.Log = Console.Out;
sFTPserver.Keys.Add(rsaKey);
sFTPserver.Keys.Add(dssKey);
sFTPserver.Bindings.Add(IPAddress.Any, portNumber);
sFTPserver.Users.Add(new SshUser(userName, password, ftpPath));
}
public void Start()
{
workerThread = new Thread(sFTPserver.Start);
workerThread.Start();
while (!workerThread.IsAlive) ;
}
public void Stop()
{
sFTPserver.Stop();
workerThread.Join();
}
}
How to fix The thread xxx has exited with code 259 (0x103)?
When try to wait for a thread to be alive, but not call thread Start method, will get a lot of errors like this::The thread xxx has exited with code 259 (0x103)
workerThread = new Thread(StartMethod);
// workerThread.Start();
while (!workerThread.IsAlive) ;
:)
workerThread = new Thread(StartMethod);
// workerThread.Start();
while (!workerThread.IsAlive) ;
:)
Thursday, February 12, 2015
How to check TcpListener is listening?
In TcpListener class, there is a property called Active but it a proctected one.
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.active(v=vs.110).aspx
Solution:
Create a sub class from TcpListener, and expose this property by yourself.
Code example:
public class Listener : TcpListener
{
public Listener(int portNumber): base(IPAddress.Any, portNumber) {}
public bool IsAcitve
{
get { return this.Active; }
}
}
https://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.active(v=vs.110).aspx
Solution:
Create a sub class from TcpListener, and expose this property by yourself.
Code example:
public class Listener : TcpListener
{
public Listener(int portNumber): base(IPAddress.Any, portNumber) {}
public bool IsAcitve
{
get { return this.Active; }
}
}
Wednesday, January 28, 2015
Why readonly in C#?
Make summary form following post:
http://stackoverflow.com/questions/277010/what-are-the-benefits-to-marking-a-field-as-readonly-in-c
1. Improve readability: tell how to not use in other way.
2. Nothing to do with performance
3. Immutable data structures in threading
http://stackoverflow.com/questions/277010/what-are-the-benefits-to-marking-a-field-as-readonly-in-c
1. Improve readability: tell how to not use in other way.
2. Nothing to do with performance
3. Immutable data structures in threading
Monday, January 26, 2015
What is real purpose of Enable Visual Studio Hosting Process?
According to MSDN:
"The Visual Studio hosting process improves debugger performance and enables new debugger features, such as partial-trust debugging and design-time expression evaluation"
In real world:
Sometimes, vshost.exe always running, and you can't even kill it from Task Manager. (After killed it, it restores automatically, need to quit Visual Studio)
Need to disable this feature/bug by following steps:
Properties of project =>
"Debug" tab =>
uncheck "Enable the Visual Studio hosting process"
"The Visual Studio hosting process improves debugger performance and enables new debugger features, such as partial-trust debugging and design-time expression evaluation"
In real world:
Sometimes, vshost.exe always running, and you can't even kill it from Task Manager. (After killed it, it restores automatically, need to quit Visual Studio)
Need to disable this feature/bug by following steps:
Properties of project =>
"Debug" tab =>
uncheck "Enable the Visual Studio hosting process"
Wednesday, January 14, 2015
Readonly vs const in C#
A const field can only be initialized at the declaration of the field.
A readonly field can be initialized either at the declaration or in a constructor
http://msdn.microsoft.com/en-us/library/vstudio/acdd6hb7(v=vs.100).aspx
A readonly field can be initialized either at the declaration or in a constructor
http://msdn.microsoft.com/en-us/library/vstudio/acdd6hb7(v=vs.100).aspx
Thursday, December 18, 2014
How to solve problem "Cannot find the requested object." while create an X509Certificate2 object?
It throws this exception for following line:
X509Certificate2 certificate = new X509Certificate2(@"c:\temp\server.pvk", "password");
It means: the certificate file is not valid.
The most common reason is:
Need pvkimprt tool to generate PFX files
Reference:
http://msdn.microsoft.com/en-us/library/ms148420(v=vs.110).aspx
This constructor creates a new X509Certificate2 object using a certificate file name and a password needed to access the certificate. It is used with PKCS12 (PFX) files that contain the certificate's private key. Calling this constructor with the correct password decrypts the private key and saves it to a key container.
Following are the steps to generate X509Certificate2
"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\makecert" -r -pe -n "CN=Test" -sky exchange c:\temp\server.cer -sv c:\temp\server.pvk -sr currentuser
"C:\Program Files (x86)\Windows Kits\8.1\bin\x64\cert2spc" c:\temp\server.cer server.spc
pvkimprt -pfx server.spc server.pfx
Thursday, December 11, 2014
What is the simplest way to solve "Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on"
Code Example
Replace
list.Items.Add(status);
with:
this.Invoke((MethodInvoker)delegate
{
//list.Items.Add(status);
});
Replace
list.Items.Add(status);
with:
this.Invoke((MethodInvoker)delegate
{
//list.Items.Add(status);
});
C# Naming Conventions with example
use PascalCasing for class names and method names.
use camelCasing for method arguments and local variables
use noun or noun phrases to name a class.
prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
References:
http://msdn.microsoft.com/en-us/library/ms229043.aspx
http://www.dofactory.com/reference/csharp-coding-standards
use camelCasing for method arguments and local variables
use noun or noun phrases to name a class.
prefix interfaces with the letter I. Interface names are noun (phrases) or adjectives.
References:
http://msdn.microsoft.com/en-us/library/ms229043.aspx
http://www.dofactory.com/reference/csharp-coding-standards
Friday, December 5, 2014
How to solve Invalid Xml syntax error after updated an older Visual Studio project
Error message:
This application has failed to start because the application configuration is incorrect. Review the manifest file for possible errors.
Reinstalling the application may fix this problem. For more details, please see the application event log.
In EventView, the error message is:
Activation context generation failed for XXXXX
XXXX on line 1. Invalid Xml syntax.
The first line in config file is
<?xml version="1.0" encoding="Windows-1252"?>
Solution:
Change to
<?xml version="1.0" encoding="utf-8"?>
Wednesday, November 12, 2014
Wednesday, November 5, 2014
Thursday, October 23, 2014
Subscribe to:
Posts (Atom)