Windows Phone Developers

Saturday, November 15, 2008

What are the differences between Structs and Classes in C# (.NET)?

The struct type is suitable for representing lightweight. A struct would be less expensive. It is declared as

public struct Rect

{

public double height;

public double width;

public Rect(double d1, double d2)

{

height = d1;

width = d2;

}

public override string ToString()

{

return "(" + height +"," + width + ")";

}

}

It is used as

Rect r1 ;

r1.height = 10;

r1.width = 20;

Console.WriteLine(r1.ToString());

Rect r2 = new Rect(12, 23);

Console.WriteLine(r2.ToString());

  • Structs are value types and classes are reference types. That is when you pass the struct to a method you pass the value and not the reference.

Rect r2 = new Rect(12, 23);

Console.WriteLine("Rect Dimensions before passing:" + r2.ToString());

change_the_struct(r2);

Console.WriteLine("Rect Dimensions after passing:" + r2.ToString());

private static void change_the_struct(Rect Rectangle1)

{

Rectangle1.height = 100;

Rectangle1.width = 100;

Console.WriteLine("Rectangle Dimensions " + Rectangle1.ToString());

}

It will give the following output, which shows the struct is passed by value.

  • Unlike classes, structs can be instantiated without using a new operator. Static classes are an exception ()

Rect r1 ;

r1.height = 10;

r1.width = 20;

Console.WriteLine(r1.ToString());

  • Structs can declare constructors, but they must take parameters.

public struct Rect

{

public double height;

public double width;

public Rect(double d1, double d2)

{

height = d1;

width = d2;

}

}

  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.
  • Structs cannot have destructors, whereas classes can

.NET Structs, What are Structs, Example of Struct in .NET



See also :

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

How to Create Classes in C#

Properties in .NET / Get and Set Methods 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

Converting Date to British Date Format using C# (.NET)

Get Today’s date in British Format

Formatting dates are easy in C#. The following snippet gets the current date in British format.

private static string ConvertDate2British()

{

return String.Format("{0:dd-MM-yy}", DateTime.Now);

}

How to convert dates to British format using .NET, Date Conversion using .NET, .NET String.Format Method, Get Current Date using .NET, .NET Today Method

See also:

Formatting Date Input - DateFOrmat (SQL)

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

Get Size of a Particular Directory using C# (.NET)

Get Directory Size using C#

One way to find the size of entire directory (including all sub-directories) is to sum up the size of all files in those directories

The following code snippet adds the size of all the files to get the directory size

private static void GetDirSize(string rootdir)

{

try

{

long DirSize = 0;

DirectoryInfo[] DI = new DirectoryInfo(rootdir).GetDirectories("*.*", SearchOption.AllDirectories);

FileInfo[] FI = new DirectoryInfo(rootdir).GetFiles("*.*", SearchOption.AllDirectories);

foreach (FileInfo F1 in FI)

{

DirSize += F1.Length;

}

Console.WriteLine("Total Size of {0} is {1} bytes", rootdir, DirSize);

}

catch (DirectoryNotFoundException dEX)

{

Console.WriteLine("Directory Not Found " + dEX.Message);

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

Get Directory Size using C#, Get SubDirectory Size using C#, SearchOption.AllDirectories in C#, How to Search All Directories using C#, FileSize using C#, How to get directory size using C#

Get Directory Size using .NET, Get SubDirectory Size using .NET, SearchOption.AllDirectories in .NET, How to Search All Directories using .NET, FileSize using .NET, How to get directory size using .NET



Directory Size using C#





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

Formatted Output using C# (Integer to Hexadecimal) / (Integer to Decimal) / (Integer to Exponential notations)

How to Display an Integer in Hexadecimal, Decimal and Scientific notations using C# (.NET)

Composite formatting can be used to format the output into various forms as shown below

private static void ListNumbersAsHexOctSciFormat()

{

try

{

Console.WriteLine("Decimal Hexadecimal Exponential (Scientific)");

for (int i = 1; i <>

{

Console.WriteLine("{0:D} \t{0:X} \t{0:E}",i );

}

}

catch (FormatException dEX)

{

Console.WriteLine("Formatting Exception" + dEX.Message);

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

To convert a string use String.Format method

string S1 = String.Format("{0:X}", i);

Formatting Strings in C#, Formatted Output in C#, C# Convert Decimal to Hexadecimal, Decimal to Hexadecimal Conversion using C#, Integer to Scientific notation using C#, C# Scientific notations, C# Integer to Hexadecimal, C# Integer to Decimal, C# Integer to Exponential notations, .NET Integer to Decimal, .NET Integer to Exponential notations

Formatting Strings in .NET, Formatted Output in .NET, .NET Convert Decimal to Hexadecimal, Decimal to Hexadecimal Conversion using .NET, Integer to Scientific notation using .NET, .NET Scientific notations, .NET Integer to Hexadecimal

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

Get all Sub Directories under a folder using C# (.NET)

Get Sub Directories using C# (.NET)

Listing all subdirectories under a root directory using .NET (C#) is simple. You can use the DirectoryInfo..::.GetDirectories Method to list the directories

private static void ListDir(string rootdir)

{

try

{

DirectoryInfo[] DI = new DirectoryInfo(rootdir).GetDirectories("*.*", SearchOption.AllDirectories ) ;

foreach (DirectoryInfo D1 in DI)

{

Console.WriteLine(D1.FullName);

}

}

catch (DirectoryNotFoundException dEX)

{

Console.WriteLine("Directory Not Found " + dEX.Message);

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

Here is the overload list of GetDirectories method

Name

Description

GetDirectories()()()

Returns the subdirectories of the current directory.

GetDirectories(String)

Returns an array of directories in the current DirectoryInfo matching the given search criteria.

GetDirectories(String, SearchOption)

Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

C# GetDirectories()()() Method, C# GetSubDirectories Method, Dir Function in VB.NET, Dir Function in C#, List All Directories using C#, List Sub Directories using C#, How to get all subdirectories using C#

.NET GetDirectories()()() Method, .NET GetSubDirectories Method, Dir Function in VB.NET, Dir Function in .NET, List All Directories using .NET, List Sub Directories using .NET, How to get all subdirectories using .NET

See also :

VBA Dir Function to Get Sub Directories

C# Get All Files from a Folder

OpenFileDialog in Visual Basic .Net

Directory Size using VB.Net

Get Size of a Particular Directory using C# (.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

Saturday, November 1, 2008

List all ReadOnly files in a directory using C# (.NET)

C# Get Read Only Files

The following code will retrieve the read-only files using LINQ and C#

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.IsReadOnly == true

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

The above can (and mostly is) done by the following way

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

foreach (FileInfo F1 in FileList)

{

if (F1.IsReadOnly == true)

{

Console.WriteLine(F1.FullName );

}

}

List ReadOnly files using C#, Retrieve Read-Only files using C#, Get Read Only Files using C#, List ReadOnly files using .NET, Retrieve Read-Only files using .NET, Get Read Only Files using .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 Filter files that are modified in a specific day using C#

C# GetFiles with Date Filter

Here is a snippet using C# and LINQ, which will retrieve the files from a directory for a specific date

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.LastWriteTime.Date == DateTime.Now.Date

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

Filter files by Date using C# GetFiles, DirectoryInfo.GetFiles method in C#, Filter files by date using DirectoryInfo, C# Get Files modified within a week, Filter files by Date using .NET GetFiles, DirectoryInfo.GetFiles method in .NET, Filter files by date using DirectoryInfo, .NET Get Files modified within a week 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