if ( variable )
{
}
else
{
Console.WriteLine("Wrong")
}
He doesn't know what "Not" is it :|
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var token = new CancellationTokenSource().Token;
Task.Factory.StartNew (() => {
while (!token.IsCancellationRequested) {
try {
if (/*Condition*/)
this.Invoke(new Action(() => label.Text = " =)"));
else
this.Invoke(new Action(() => label.Text = " =("));
Thread.Sleep(10);
}
catch (Exception) {
throw;
}
finally {
throw new Exception();
}
}
}) ;
}
}
My collegue's way of using multithreading features (and exceptions handling) :)
foreach (Byte bajt in data ) {
BufferIndex = i++;
if (!char.IsControl((char)bajt))
MyCharacter = Convert.ToChar(bajt).ToString();//Encoding.ASCII.GetString(new[]{bajt});
else
MyCharacter = " ";
if (BufferIndex == 200)
{
int a = BufferIndex;
}
if (BufferIndex < 200)
{
sw.WriteLine("PrintBuffer1[" + BufferIndex + "] :=\t" + bajt + "\t; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + "\t" + MyCharacter );
}
if (BufferIndex >= 200 && BufferIndex < 400)
{
BufferIndex = BufferIndex - 200;
sw.WriteLine("PrintBuffer2[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 400 && BufferIndex < 600)
{
BufferIndex = BufferIndex - 400;
sw.WriteLine("PrintBuffer3[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 600 && BufferIndex < 800)
{
BufferIndex = BufferIndex - 600;
sw.WriteLine("PrintBuffer4[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 800 && BufferIndex < 1000)
{
BufferIndex = BufferIndex - 800;
sw.WriteLine("PrintBuffer5[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 1000 && BufferIndex < 1200)
{
BufferIndex = BufferIndex - 1000;
sw.WriteLine("PrintBuffer6[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 1200 && BufferIndex < 1400)
{
BufferIndex = BufferIndex - 1200;
sw.WriteLine("PrintBuffer7[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 1400 && BufferIndex < 1600)
{
BufferIndex = BufferIndex - 1400;
sw.WriteLine("PrintBuffer8[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 1600 && BufferIndex < 1800)
{
BufferIndex = BufferIndex - 1600;
sw.WriteLine("PrintBuffer9[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 1800 && BufferIndex < 2000)
{
BufferIndex = BufferIndex - 1800;
sw.WriteLine("PrintBuffer10[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 2000 && BufferIndex < 2200)
{
BufferIndex = BufferIndex - 2000;
sw.WriteLine("PrintBuffer11[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
if (BufferIndex >= 2200 && BufferIndex < 2400)
{
BufferIndex = BufferIndex - 2200;
sw.WriteLine("PrintBuffer12[" + BufferIndex + "] :=" + bajt + "; // " + String.Format(@"0x{0:x2}", (ushort)bajt) + " " + MyCharacter);
}
}
don't repeat yourself.. whyy we don't have PrintBufeer[i] ?
string month = DateTime.Today.Month.ToString();
if (DateTime.Today.Month < 10)
{
month = "0" + month;
}
string day = DateTime.Today.Day.ToString();
if (DateTime.Today.Day < 10)
{
day = "0" + day;
}
string dateCorrect = String.Format("{0}.{1}.{2}", DateTime.Today.Year, month, day);
string dateDue = "";
if (transferFields.DueDate.SelectedDate.HasValue)
{
var dateDuearr = transferFields.DueDate.Value.Split(' ')[0].Split('.');
dateDue = dateDuearr[2] + '.' + dateDuearr[1] + '.' + dateDuearr[0];
}
else {
dateDue = dateCorrect;
}
Real developers don't use built in parsing and formatting methods.
public static class DecimalHelpers
{
/// <summary>
/// Format a decimal XX.XX to XX,XX%.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string FormatToStringPercentageValue(this decimal value)
{
return value.ToString().Replace(".", ",").FormatToPercentageValue();
}
}
public static class StringHelpers
{
/// <summary>
/// Add % at the end of the string.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string FormatToPercentageValue(this string value)
{
return string.Concat(value, "%");
}
}
if (model.env.Trim().ToUpper() == "Release")
{
retVal = false;
if (!string.IsNullOrEmpty(model.Server_Core))
{
// Perform a trim in case user specified just whitespace...
string tmpStr = model.Server_Core.Trim();
if (!string.IsNullOrEmpty(tmpStr))
{
retVal = true;
}
}
}
public static bool ValidateEmailAddress(string emailAddress)
{
string pattern = "^((?>[a-zA-Z\\d!#$%&'*+\\-/=?^_`{|}~]+\x20*|\"((?=[\x01-\x7f])[^\"\\]|\\[\x01-\x7f])*\"\x20*)*(?<angle><))?((?!\\.)(?>\\.?[a-zA-Z\\d!#$%&'*+\\-/=?^_`{|}~]+)+|\"((?=[\x01-\x7f])[^\"\\]|\\[\x01-\x7f])*\")@(((?!-)[a-zA-Z\\d\\-]+(?<!-)\\.)+[a-zA-Z]{2,}|\\[(((?(?<!\\[)\\.)(25[0-5]|2[0-4]\\d|[01]?\\d?\\d)){4}|[a-zA-Z\\d\\-]*[a-zA-Z\\d]:((?=[\x01-\x7f])[^\\\\[\\]]|\\[\x01-\x7f])+)\\])(?(angle)>)$";
if (!String.IsNullOrEmpty(emailAddress) && !(Regex.Match(emailAddress.Trim(), pattern, RegexOptions.IgnoreCase)).Success)
{
return false;
}
return true;
}
public class NullableFloatToNullableDoubleConverter : ITypeConverter<float?, double?>
{
public double? Convert(float? source, double? destination, ResolutionContext context)
{
if (source == null)
{
return null;
}
var floatAsString = source.Value.ToString(CultureInfo.InvariantCulture);
return double.Parse(floatAsString);
}
}
/// <summary>
/// Builds the suffixs for the request sequence number
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static string GetSequenceNumber(int i)
{
string strCount = string.Empty;
string increase = string.Empty;
int count;
strCount = i.ToString();
count = strCount.Length;
//Add 0 infront of the index to make a 4 digit number
if (count < 5)
{
for (int j = 0; j < (4 - count); j++)
{
increase = increase + "0";
}
strCount = increase + strCount;
}
return strCount;
}
StartCoroutine(Patrol());
public IEnumerator Patrol() {
while(true) {
dt.Walk();
yield return new WaitForSeconds(reactionTime);
}
}
if (date.Training != null)
{
var training = date.Training;
var status = new TrainingStatus();
var refresher = new Training();
if (training.Trainings != null)
refresher = training.Trainings;
bool hasTakenRefresher = false;
status.Value = date.Date.AddDays(
TrainingHelper.CalculateValidityDays(training.Validity ?? 0, training.ValidityType ?? 0));
status.Name = training.TrainingName;
status.ID = training.TrainingID;
status.Category = training.CategoryTraining != null ? training.CategoryTraining.Name : "Other";
status.IsTrained = _validityUtilsService.IsValid(training, date.Date);
if (status.IsTrained)
{
status.IsExpiring = _validityUtilsService.IsExpiring(training, date.Date,
TrainingHelper.CalculateValidityDays(
training.ExpirationWarningValidity ?? 0,
training.ExpirationWarningValidityType ?? 0));
}
if (refresher.ID != 0)
{
hasTakenRefresher = employee.Dates
.Count(x => x.Training != null
&& x.TrainingID == refresher.ID) > 1;
if (hasTakenRefresher && (status.IsExpiring || !status.IsTrained))
{
status.IsExpiring = false;
var trainingWithNewValidity = new Training();
status.Value = date.Date.AddDays(
TrainingHelper.CalculateValidityDays(training.ValidityRefresher ?? 0,
training.ValidityRefresherType ?? 0));
trainingWithNewValidity.Validity = training.ValidityRefresher;
trainingWithNewValidity.ValidityType = training.ValidityRefresherType;
status.IsTrained = _validityUtilsService.IsValid(trainingWithNewValidity, date.Date);
if (status.IsTrained)
{
status.IsExpiring = _validityUtilsService.IsExpiring(trainingWithNewValidity, date.Date,
TrainingHelper.CalculateValidityDays(
refresher.ExpirationWarningValidity ?? 0,
refresher.ExpirationWarningValidityType ?? 0));
}
}
}
if (status.IsTrained && !status.IsExpiring)
status.Status = TrainingStatuses.Trained;
else if (status.IsTrained && status.IsExpiring)
status.Status = TrainingStatuses.Expiring;
else if (!status.IsTrained && !status.IsExpiring)
status.Status = TrainingStatuses.Expired;
status.EmpID = employee.ID;
return status;
}
test
class ExampleClass {
public void DoSomething()
{
if (this != null) {
//omitted
}
}
public void DoSomething2()
{
if (this != null) {
//omitted
}
}
}
Explanation: This can't be null in C# virtual instance methods - I was so confused by the widespread use of this check that I asked this question at SO: https://stackoverflow.com/questions/31747718/can-this-be-null-in-c-sharp-virtual-methods-what-happens-with-the-rest-of-ins
protected override void OnStartup(StartupEventArgs e)
{
Current.DispatcherUnhandledException += ApplicationUnhandledException;
base.OnStartup(e);
#if (DEBUG)
RunInDebugMode(e.Args);
#else
RunInReleaseMode(e.Args);
#endif
}
#endregion
private static void RunInReleaseMode(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
try
{
SplashScreen screen = new SplashScreen(@"Images\splashscreen.png");
screen.Show(true, true);
RunInDebugMode(args);
}
catch (Exception ex)
{
HandleException(ex);
}
}
private static void RunInDebugMode(string[] args)
{
var bootstrapper = new Bootstrapper();
bootstrapper.Run();
}
var list = new List<string>() { // Assume some items in the list };
for (var i = 0; i < list.Count; i++)
{
var item = list[i];
list.Remove(item);
i--;
}
Simple alternative to List.Clear()
public static class time
{
public const int minutes_per_hour = 60;
public const int seconds_per_minute = 60;
public const int hours_per_day = 24;
public const int minutes_per_day = minutes_per_hour * hours_per_day;
public const int seconds_per_day = seconds_per_minute * minutes_per_day;
public const int days_per_year = 365;
}