Sponsored Ad

Tuesday, July 26, 2011

Calculate Maximum of two Int – Decimal - long Numbers in C# | Math.Max()

C# provide a math function to calculate max of two numbers, This function have overloaded functions for long, int, float, decimal, short etc.

if you face any problem in finding the max value can use the below function.

Calculate Maximum of two int – Decimal - long Numbers in C# | Math.Max()

C# Code to Find Maximum of Two Numbers :

using System;

namespace DemoConsoleApplication
{
    class Max_of_Numbers
    {
        static void Main(string[] args)
        {
           Console.WriteLine(" Int Number Max: " +  Math.Max(3,4));
           Console.WriteLine(" Double Number Max: " + Math.Max(3.333, 4.444));
           Console.WriteLine(" Long Number Max: " + Math.Max(3L, 4L));
           Console.WriteLine(" Decimal Number Max: " + Math.Max(3.33M, 4.44M));

            Console.ReadLine();
        }
    }
}

Calculate Truncated Number from a Decimal Number in C#

If you want to truncate a given decimal number of double number, C# provide a function Math.Truncate() to return corresponding number.

The below code provide sample demo application for the same.

Calculate Truncated Number from a Decimal Number in C#

C# Example Code to truncate a given number:

using System;

namespace DemoConsoleApplication
{
    class Math_Truncate
    {
        static void Main(string[] args)
        {
            decimal dec_Number = 400.2222M;

            decimal Result = Math.Truncate(dec_Number);
            Console.WriteLine(" Truncated Number: " + Result);

            Console.ReadLine();
        }
    }
}

Sunday, July 24, 2011

How to execute batch files in Hidden Mode using C#

C# provide a way to execute batch files even without displaying the execution window.

You have to set WindowStyle property to hidden. the sample program is given below.

C# Sample code to Hide Batch file execution. 

using System;
using System.Diagnostics;

namespace DemoConsoleApplication
{

    class Batch_File
    {
        static void Main(string[] args)
        {
            ProcessStartInfo PI = new ProcessStartInfo("c:\\Test_File.bat");
            PI.WindowStyle = ProcessWindowStyle.Hidden;
            Process BP = new Process();
            BP.StartInfo = PI;
            BP.Start();      

            Console.ReadLine();
        }
    }
}

Change Character into String using C#

The below code will help C# learner to convert a given Char to String and use it in program. You just need a cast by applying toString() function.

Please comment if you have any questions.

Change Character into String using C#

C# Sample program to convert Char into String:

using System;

namespace DemoConsoleApplication
{

    class Char_to_String
    {
        static void Main(string[] args)
        {
            string str_Var ;
            char ch_Var = 'A';
            str_Var = ch_Var.ToString();
            Console.WriteLine("Given Char: " + ch_Var);
            Console.WriteLine("Converted String: " + str_Var);           

            Console.ReadLine();
        }
    }
}

Convert String to Char using C#

You can not directly cast string to char, if you want to convert string to char the first char of string and assign to char variable. The below sample will help you to understand.

Another option is to convert string to array of char.

C# Example to Convert String to Char

C# Sample Code to Convert String to Char:

using System;

namespace DemoConsoleApplication
{

    class String_to_Char
    {
        static void Main(string[] args)
        {
            string str = "S";
            char ch = str[0];
            Console.WriteLine("Given string: " + str);
            Console.WriteLine("Converted Char: " + ch);           

            Console.ReadLine();
        }
    }
}

How to Free a Object Memory in C#

C# provide a automatic memory management by garbage collector while if you want to ensure that object should destroyed you can choose any of the below methods.

1. Method 1: use the using block to initialize the object and implement dispose method. same time suppress the finalize method.

//method 1
         using(object obj1 = new object())
         {
             //Do operations

        }
    

2. Method 2: Initialize the object and mark null in finalize block.   

//method 2

         object obj2 ;
         try
         {
             obj2 = new object();
         }
         catch
         {
         }
         finally
         {
             obj2 = null;
         }

How to Exit the Loop before End in C#

I got a question from lot of newbie's that how to exit from a loop before reaching to end of loop. This post is dedicated to those learner by giving a sample code.

C# provide a break statement to exit from a loop at any point of time. it can be conditional or for experiment we can write it without a condition.

How to Exit the Loop before End in C#

Sample Example to Exit from a loop:

using System;

namespace DemoConsoleApplication
{

    class Exit_Loop
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("Number #:" + i.ToString());
                if (i == 5)
                    break;
            }
            Console.WriteLine("Out of loop.");

            Console.ReadLine();
        }
    }
}

Change Look and Feel of Visual Studio Editor

While working with visual studio, it may be that you don't like the default look and feel of the editor, so visual studio provide a option to change the fornt and color of elements.

The below steps will help you to change the font and color of various elements like plain text, selected text, break point , background etc.

Open the visual studio

  1. Go to Tools
  2. Go to Options
  3. Go to Environments
  4. Go to Fonts & Colors

Now from this tab you can change the setting as you like.

Change Look and Feel of Visual Studio Editor

Output of above change:

Change Look and Feel of Visual Studio Editor

Sponsored Ad

Website Update

Followers