Windows Phone Developers

Thursday, October 30, 2008

Mathematical Functions in C# / Logarithmic Function in C# / Trigonometric Functions in C#

Mathematical Functions in .NET / Logarithmic Function in .NET / Trigonometric Functions in .NET

The common logarithmic, trigonometric functions are available in the .NET framework as part of System.Math class. Here is a list of functions that are available

Name

Description

Abs

Overloaded. Returns the absolute value of a specified number.

Acos

Returns the angle whose cosine is the specified number.

Asin

Returns the angle whose sine is the specified number.

Atan

Returns the angle whose tangent is the specified number.

Atan2

Returns the angle whose tangent is the quotient of two specified numbers.

BigMul

Produces the full product of two 32-bit numbers.

Ceiling

Overloaded. Returns the smallest integer greater than or equal to the specified number.

Cos

Returns the cosine of the specified angle.

Cosh

Returns the hyperbolic cosine of the specified angle.

DivRem

Overloaded. Calculates the quotient of two numbers and also returns the remainder in an output parameter.

Exp

Returns e raised to the specified power.

Floor

Overloaded. Returns the largest integer less than or equal to the specified number.

IEEERemainder

Returns the remainder resulting from the division of a specified number by another specified number.

Log

Overloaded. Returns the logarithm of a specified number.

Log10

Returns the base 10 logarithm of a specified number.

Max

Overloaded. Returns the larger of two specified numbers.

Min

Overloaded. Returns the smaller of two numbers.

Pow

Returns a specified number raised to the specified power.

Round

Overloaded. Rounds a value to the nearest integer or specified number of decimal places.

Sign

Overloaded. Returns a value indicating the sign of a number.

Sin

Returns the sine of the specified angle.

Sinh

Returns the hyperbolic sine of the specified angle.

Sqrt

Returns the square root of a specified number.

Tan

Returns the tangent of the specified angle.

Tanh

Returns the hyperbolic tangent of the specified angle.

Truncate

Overloaded. Calculates the integral part of a number.

How to use Mathematical Function PI in C# (.NET)

Mathematical PI, which represents the ratio of the circumference of a circle to its diameter, is specified by the constant, π and can be retrieved by

Console.WriteLine("Value of PI = " + Math.PI);

Similarly other trigonometric functions like Sin, Cos, Tan etc can be used in C# as follows:

public static void MathFunctions()

{

Console.WriteLine("Value of PI = " + Math.PI);

Console.WriteLine("Value of Tan 45 = " + Math.Tan(45*Math.PI/180));

Console.WriteLine("Value of Cos 45 = " + Math.Cos(45 * Math.PI / 180));

Console.WriteLine("Value of Sin 45 = " + Math.Sin(45 * Math.PI / 180));

}

Functions Sin, Cos, Tan etc accept angle in Radians (NOT in degrees). If you want to use degrees as input, use appropriate conversion functions

Radians to Degree Conversion using C#

public static double Radians2Degrees(double degrees)

{

try

{

return Math.PI * degrees / 180;

}

catch (Exception ex)

{

Console.WriteLine(ex.ToString());

return -1;

}

}

Console.WriteLine("Value of Tan 45 = " +

Math.Tan(Radians2Degrees(45)));

The output will be as shown below


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to create and use Static Constructors in C# (.NET)

Static Constructors in .NET (C#)

Static constructors are called automatically to initialize the class before the first instance is created or any static members are referenced. They do not have any parameters and cannot be called directly.

class StatSample

{

static StatSample ()

{

Console.WriteLine("StatSample Initialized");

// Log the process start time

Write2Log(DateTime.Now.ToString());

}

public static void SayHello()

{

Console.WriteLine("Hello");

}

private static void Write2Log(String msg)

{

Console.WriteLine(msg);

}

}

The following code invokes the static constructor without any new operator or any explicit call

StatSample.SayHello();




Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to create Default Constructors in C#

Default constructors are the ones that takes no parameters. Default constructors are invoked whenever an object is instantiated by using the new operator and no arguments are provided to new.

Constructors have the same name as the class, and usually initialize the data members of the new object.

class ClsTea : IRetail

{

//Variables

private string _locale;

private static string _brand;

public ClsTea()

{

_brand = "N/A";

}

}

The following snippet invokes the default constructor

ClsTea MyTea = new ClsTea();

Console.WriteLine("My default tea brand is " + ClsTea.Brand);

ClsTea.Brand = "Tata";

Console.WriteLine("My favourite tea brand is " + ClsTea.Brand);

Default Constructors in .NET, Default Constructors in .C#, C# Default Constructors, C# New Operator, .NET New Operator, New Operator and Default Constructors Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

C# Get Temporary File / Store Values in Temporary files using C# (.NET)

C# Get Temporary File / Store Values in Temporary files using C# (.NET)

Many times in VB we would have used Open...Print...Close to open and write to a text file for temporary storage and use a unique file number to work on those files (Print #1, “Hello” etc). If we use the same number that is already in use, we would get File Open exceptions. To avoid, VB and VBA we used FreeFile to get the free buffer.

Similar way we can use GetTempFileName method to get a temporary file. This method creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file, which can be used to write temporary strings/vars.

// DotNetDud GetTempFileName Example

try

{

String sFile;

sFile = Path.GetTempFileName();

Console.WriteLine("Temporary File " + sFile);

//Store the Values Temporarily in Temp file

StoreVar(sFile);

}

catch (IOException ex1)

{

Console.WriteLine ("IOException " + ex1.Message);

}

catch (Exception ex2)

{

Console.WriteLine("Unhandled Exception " + ex2.Message);

}

If you want to have cryptographically strong, random string that can be used as either a folder name or a file name use the following:

sFile = Path.GetRandomFileName();

This does not create a file/folder, you need to write separate code to create the file. You can use GetTempFolder method () to get the temporary folder and create the file there.

Freefile in .NET (C#), .NET Create Temporary Files, Temporary Files in .NET (C#), C# Path Class, C# GetTempFileName method, .Net GetTempFileName method Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to get Temporary Folder using .NET (C#)

How to get path of the current system's temporary folder using .NET (C#)

Temporary folders are useful to store simple logs during execution of the program. It is also used for storing the decompressed files before any operation. The folder location varies from OS to OS. The following C# snippet would help in retrieving the Temporary folder path

string sPath;

sPath = Path.GetTempPath();

Console.WriteLine("Temporary Path := " + sPath );

Temporary Folder using .NET (C#), C# Temporary Folder, C# Temp Folder, .NET Temporary Folder, .NET Temp Folder

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Check if Path is Absolute or Relative Path using C# (.NET)

IsPathRooted method of Path class returns a value indicating whether the specified path string contains absolute or relative path information.

if (Path.IsPathRooted(@"c:\temp\") == true)

{

Console.WriteLine("Path is absolute");

}

else

{

Console.WriteLine("Path is relative");

}

How to find absolute path using C# (.NET), How to find relative path using C# (.NET), C# (.NET) Path Class, C# (.NET) IsPathRooted method Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to retrieve MyDocuments Folder using C# (.NET)

To retrieve the logical MyDocuments use the following

Console.WriteLine("MyDocuments := " + Environment.GetFolderPath( Environment.SpecialFolder.MyDocuments));

See also:

Retrieving Special Folders using FileSystemObject (VBA / VB)

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to retrieve Desktop Folder using C# (.NET)

To retrieve the logical desktop use the following

Console.WriteLine("Desktop := " + Environment.GetFolderPath( Environment.SpecialFolder.Desktop));

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to get the full path of cookies folder using C# (.NET)

Cookies folder (FullName) can be retrieved using the following snippet

Console.WriteLine("Cookies := " + Environment.GetFolderPath( Environment.SpecialFolder.Cookies ));

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to retrieve Application Data Folder using C# (.NET)

ApplicationData is the directory that serves as a common repository for application-specific data for the current roaming user. It can be retrieved using

Console.WriteLine("ApplicationData := " + Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData));

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to get the network domain name associated with the current user using C# (.NET)

How to get the network domain name associated with the current user using C# (.NET)

Console.WriteLine("Domain Name := " + Environment.UserDomainName );

Get Domain Name Function in C#, UserDomainName Function in C#, .NET Get User’s Domain Name, GetDomainName in .NET

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to get the number of processors in the machine using C# (.NET)

No of processors in the given machine can be retrieved by

Console.WriteLine("ProcessorCount := " + Environment.ProcessorCount );

C# Processor Count, number of processors in the machine using C# (.NET), .NET Processor Count Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Retrieve user name using .NET / How to get user name using C#

Retrieve user name using .NET / How to get user name using C#

The following code will get the user name of the computer

Console.WriteLine("User Name := " + Environment.UserName);

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

C# Get Computer Name – Name of the Computer using C#

Get Computer Name using .NET (C#)

The following code will get the name of the computer (NetBIOS name of the local computer)

Console.WriteLine("Machine Name := " + Environment.MachineName);

See also

Get Computer Name (VBA)

Get Computer Name in .Net

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Default Values Table (C# Reference)

C# Default Values Table

The following table shows the default values of value types returned by the default constructors.

Value type

Default value

bool

false

byte

0

char

'\0'

decimal

0.0M

double

0.0D

enum

The value produced by the expression (E)0, where E is the enum identifier.

float

0.0F

int

0

long

0L

sbyte

0

short

0

struct

The value produced by setting all value-type fields to their default values and all reference-type fields to null.

uint

0

ulong

0

ushort

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

How to Create Interfaces in .NET / Interfaces in .NET

How to Create Interfaces in C# / Interfaces in C#

An interface defines a set of members, such as properties and procedures that classes and structures can implement. The interface defines only the signatures of the members and not their internal workings.

Interface definitions are enclosed within the Interface and End Interface statements in Visual Basic, they are enclosed within parenthesis in C# (as shown below). Following the Interface statement, you can add an optional inherited interface name.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace CSharpSample

{

interface IRetail

{

string BrandName();

}

}

A class or structure implements the interface by supplying code for every member defined by the interface. Finally, when the application creates an instance from that class or structure, an object exists and runs in memory.

Interface can be used only at namespace or module level. That is, the declaration context for an interface must be a source file, namespace, class, structure, module, or interface. Interface cannot be a declared in a procedure or block.

To implement the interface in a class, suffix class name by a colon and the interface name as shown below

class ClsTea : IRetail

The class ClsTea should implement the methods described in the Interface. Otherwise Compiler Error CS0535 will be thrown -

'class' does not implement interface member 'member'

class ClsTea : IRetail

{

//Variables

private string _locale;

private static string _brand;

// Property

public static string Brand

{

get

{

return _brand;

}

set

{

_brand = value;

}

}

//property

public string Locale

{

get { return _locale; }

set { _locale = value; }

}

private string[] brands = new string[12];

public string this[int index]

{

get { return brands[index]; }

set { brands[index] = value; }

}

public string BrandName()

{

return "N/A";

}

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon