In C# 3 and Visual Basic 9, you can declare variables without having to specify the type of the variable. This isn't the same as the variant type in Visual Basic, but rather the compiler's ability to determine the type of the variable from the initialization value.
Here's how it works:
var i = 3; //C#
Dim i = 3 'VB
Using implicit declaration of variables requires you to specify a non-null value since a null only represents the generic object type.
EDIT: You can get the implicit declarations feature in LINQ projects (CTP) with Visual Studio 2005.
Showing posts with label .NET. Show all posts
Showing posts with label .NET. Show all posts
Saturday, December 29, 2007
Sunday, December 23, 2007
VS2008 Express
The Visual Studio 2008 Express offline installer is a corrupt image being offered for download from the Microsoft website. A lot of people have complained about the "Program too big to fit in memory" error so it's yet another bug from infamous Microsoft. It's a free product so no one really cares at this stage, unless they are paying for bandwidth.
Tuesday, December 18, 2007
Microsoft's 2008 Platform
Microsoft is releasing Microsoft Windows 2008, Microsoft Visual Studio 2008 and Microsoft SQL Server 2008 at an event on February 27, 2008 7am. It's a pretty major event since it marks the next leap since the 2005 release.
The best feature of the new platform is LINQ, which enables developers to treat data as entities rather than relational tables. It's like taking NHibernate to the next level and integrating it with the programming language.
The best feature of the new platform is LINQ, which enables developers to treat data as entities rather than relational tables. It's like taking NHibernate to the next level and integrating it with the programming language.
Wednesday, December 12, 2007
NCover
NCover is a pretty good code coverage tool for .NET. You can use it to determine which parts of your code aren't tested by unit tests, thus letting you know if you need to write more tests.
A common problem identified by code coverage tools is that you have blocks of code that you wouldn't encounter under regular circumstances, such as Exception blocks that are intended to catch unforseen errors or error conditions that occur rarely. Due to insufficient testing for those blocks, you could be releasing some untested code that could result in a bug.
NCover can be run with pretty much any kind of tests - you just need to provide them as parameters to the command line NCover tool or browse to find the executable for the graphical tool so you aren't limited to any single testing framework, such as NUnit.
A common problem identified by code coverage tools is that you have blocks of code that you wouldn't encounter under regular circumstances, such as Exception blocks that are intended to catch unforseen errors or error conditions that occur rarely. Due to insufficient testing for those blocks, you could be releasing some untested code that could result in a bug.
NCover can be run with pretty much any kind of tests - you just need to provide them as parameters to the command line NCover tool or browse to find the executable for the graphical tool so you aren't limited to any single testing framework, such as NUnit.
Friday, November 2, 2007
Method Extensions in C# 3.0
One of the new features in C#.NET 3.0 is method extensions. You can add additional methods, for example, to the String class so you can have a ToEmployee method in it. The only difference between a regular static method and a method extension is the addition of the "this" keyword before the parameter declaration.
Monday, October 22, 2007
.NET Remoting over TCP
Remoting in .NET is quite similar to RMI in Java. To begin with, we need an object that has the methods we would be executing on the server-side. This object inherits from MarshalByRefObject. This indicates that the client does not get a copy of the object but rather a reference to the remote object. We then have a hosting process and a client process.
In our example, the hosting process creates a new instance of the TcpChannel class (pass the TCP port number to the constructor of TcpChannel for the server hosting process) and registers the object using ChannelServices.RegisterChannel. Then, the class we are hosting has to be registered using the RemotingConfiguration.RegisterWellKnownServiceType method - it takes the type of our class as the first parameter, the registration name as the second parameter and the object mode (singleton & single call modes) as the third parameter. When creating a console application, we would usually add a Console.ReadLine statement at the end of the application to prevent it from exiting.
The client application would create and register an instance of TcpChannel. Then, obtain an instance of the class hosted on the server using Activator.GetObject - with the type of the class as the first parameter and the URL (Example: tcp://localhost:8080/my_service) as the second parameter. You can now call methods on the object as you would for a normal object.
In our example, the hosting process creates a new instance of the TcpChannel class (pass the TCP port number to the constructor of TcpChannel for the server hosting process) and registers the object using ChannelServices.RegisterChannel. Then, the class we are hosting has to be registered using the RemotingConfiguration.RegisterWellKnownServiceType method - it takes the type of our class as the first parameter, the registration name as the second parameter and the object mode (singleton & single call modes) as the third parameter. When creating a console application, we would usually add a Console.ReadLine statement at the end of the application to prevent it from exiting.
The client application would create and register an instance of TcpChannel. Then, obtain an instance of the class hosted on the server using Activator.GetObject - with the type of the class as the first parameter and the URL (Example: tcp://localhost:8080/my_service) as the second parameter. You can now call methods on the object as you would for a normal object.
Sunday, October 21, 2007
Type Forwarding
Type Forwarding enables client applications to refer to a library at compile time for a type that is later moved to another library.
Example:
using System.Runtime.CompilerServices;
[assembly:TypeForwardedTo(typeof(DestinationLib.TheType))]
Example:
using System.Runtime.CompilerServices;
[assembly:TypeForwardedTo(typeof(DestinationLib.TheType))]
The IFormattable interface
Implementing the IFormattable interface let's you do much more with the ToString() method if you're developing text-based applications or are trying to create strings out of object fields.
To implement the IFormattable interface, you have to create a ToString method that accepts a format string and an IFormatProvider object. The IFormatProvider object isn't relevant but the format string can be used, such as to return only one field of an object. Here's a sample implementation:
class TimeInterval : IFormattable {
...
public override string ToString() {
return ToString(null, null);
}
public string ToString (string format, IFormatProvider formatProvider) {
if (format == null) {
return this.Hour.ToString() + ":" + this.Minute.ToString();
} else if (format == "h") {
return this.Hour.ToString();
} else if (format == "m") {
return this.Minute.ToString();
} else throw new Exception("Invalid format specified");
}
}
To use the new method, you could provide a format string to either string.Format or Console.WriteLine. Example:
TimeInterval timeInterval = new TimeInterval();
Console.WriteLine("{0:h} hours and {0:m} minutes", timeInterval);
Console.WriteLine("{0,2:h}:{0,2:m}", timeInterval); //the ",2" specifies the minimum number of characters in the resulting string - if positive, the output is left-padded, otherwise it is right-padded
To implement the IFormattable interface, you have to create a ToString method that accepts a format string and an IFormatProvider object. The IFormatProvider object isn't relevant but the format string can be used, such as to return only one field of an object. Here's a sample implementation:
class TimeInterval : IFormattable {
...
public override string ToString() {
return ToString(null, null);
}
public string ToString (string format, IFormatProvider formatProvider) {
if (format == null) {
return this.Hour.ToString() + ":" + this.Minute.ToString();
} else if (format == "h") {
return this.Hour.ToString();
} else if (format == "m") {
return this.Minute.ToString();
} else throw new Exception("Invalid format specified");
}
}
To use the new method, you could provide a format string to either string.Format or Console.WriteLine. Example:
TimeInterval timeInterval = new TimeInterval();
Console.WriteLine("{0:h} hours and {0:m} minutes", timeInterval);
Console.WriteLine("{0,2:h}:{0,2:m}", timeInterval); //the ",2" specifies the minimum number of characters in the resulting string - if positive, the output is left-padded, otherwise it is right-padded
Saturday, October 20, 2007
Common Interfaces
The following are some common interfaces implemented by user-defined types:
IComparable: This is required for sorting.
IDisposable: This is used for manually disposing an object
IConvertible: This provides conversion to a base type
ICloneable: This is to provide a copy for an object
IEquatable: This is used when comparison of objects using "==" is required
IFormattable: This is used to convert an object into a formatted string (rather than use the base ToString method)
IComparable: This is required for sorting.
IDisposable: This is used for manually disposing an object
IConvertible: This provides conversion to a base type
ICloneable: This is to provide a copy for an object
IEquatable: This is used when comparison of objects using "==" is required
IFormattable: This is used to convert an object into a formatted string (rather than use the base ToString method)
Friday, October 19, 2007
VB 7 vs VB 8: Operator Overloading
When designing .NET as a programming platform, the designers could either leave out operator overloading, as Java does, or they could continue the tradition started off by C and continued by C++ to provide greater control to the programmer. They chose to let the programmer decide which path to take and so added the feature into the framework, but for some reason didn't provide a way to use it through Visual Basic .NET 2003 (VB 7).
In .NET 1.1, VB 7 did not include the concept of operator overloading. You could neither define nor use overloaded operators and the workaround was to create methods, such as a .Add(object) method. C#, however, did include operator overloading even on .NET 1.1.
.NET 2.0 introduced the Operator keyword in Visual Basic, a feature in VB 8, which reduces the amount of code and provides added convenience for the developers.
In .NET 1.1, VB 7 did not include the concept of operator overloading. You could neither define nor use overloaded operators and the workaround was to create methods, such as a .Add(object) method. C#, however, did include operator overloading even on .NET 1.1.
.NET 2.0 introduced the Operator keyword in Visual Basic, a feature in VB 8, which reduces the amount of code and provides added convenience for the developers.
.NET Value Types
Value types in .NET
The following are the built-in value types in .NET. Value types are stored on the stack and can be accessed more efficiently than reference types.
Type: SByte
Alias(es): sbyte
Size (in bytes): 1
Range: -128 to 127
Type: Byte
Alias(es): byte
Size (in bytes): 1
Range: 0 to 255
Type: Int16
Alias(es): Short, short
Size (in bytes): 2
Range: -32768 to 32767
Type: Int32
Alias(es): Integer, int
Size (in bytes): 4
Range: -2147483648 to 2147483647
Type: UInt32
Alias(es): UInteger, uint
Size (in bytes): 4
Range: 0 to 4294967295
Type: Int64
Alias(es): Long, long
Size (in bytes): 8
Range: -9223372036854775808 to 9223372036854775807
Type: Single
Alias(es): float
Size (in bytes): 4
Range: -3.402823E+38 to 3.402823E+38
Type: Double
Alias(es): double
Size (in bytes): 8
Range: -1.79769313486232E+308 to 1.79769313486232E+308
Type: Decimal
Alias(es): decimal
Size (in bytes): 16
Range: -79228162514264337593543950335 to 79228162514264337593543950335
Type: Char
Alias(es): char
Size (in bytes): 2
Range:
Type: Boolean
Alias(es): bool
Size (in bytes): ?
Range: true, false
Type: IntPtr
Alias(es):
Size (in bytes): Platform-specific
Range:
Type: DateTime
Alias(es): Date, date
Size (in bytes): 8
Range: 1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM
Operations with Int32, UInt32 and Double types are optimized by the runtime or the hardware.
The following are the built-in value types in .NET. Value types are stored on the stack and can be accessed more efficiently than reference types.
Type: SByte
Alias(es): sbyte
Size (in bytes): 1
Range: -128 to 127
Type: Byte
Alias(es): byte
Size (in bytes): 1
Range: 0 to 255
Type: Int16
Alias(es): Short, short
Size (in bytes): 2
Range: -32768 to 32767
Type: Int32
Alias(es): Integer, int
Size (in bytes): 4
Range: -2147483648 to 2147483647
Type: UInt32
Alias(es): UInteger, uint
Size (in bytes): 4
Range: 0 to 4294967295
Type: Int64
Alias(es): Long, long
Size (in bytes): 8
Range: -9223372036854775808 to 9223372036854775807
Type: Single
Alias(es): float
Size (in bytes): 4
Range: -3.402823E+38 to 3.402823E+38
Type: Double
Alias(es): double
Size (in bytes): 8
Range: -1.79769313486232E+308 to 1.79769313486232E+308
Type: Decimal
Alias(es): decimal
Size (in bytes): 16
Range: -79228162514264337593543950335 to 79228162514264337593543950335
Type: Char
Alias(es): char
Size (in bytes): 2
Range:
Type: Boolean
Alias(es): bool
Size (in bytes): ?
Range: true, false
Type: IntPtr
Alias(es):
Size (in bytes): Platform-specific
Range:
Type: DateTime
Alias(es): Date, date
Size (in bytes): 8
Range: 1/1/0001 12:00:00 AM to 12/31/9999 11:59:59 PM
Operations with Int32, UInt32 and Double types are optimized by the runtime or the hardware.
Subscribe to:
Posts (Atom)