Search This Blog

Friday, October 17, 2014

Writes lines to text file C#

C# > IO  > FileInfo > CreateText

Writes text a new text file.

Example


Writes lines to text file C#


string path = Path.GetTempFileName();
FileInfo fi = new FileInfo(path);

using (StreamWriter sw = fi.CreateText())
{
      sw.WriteLine("Line1");
      sw.WriteLine("Line2");
      sw.WriteLine("Line3");
      sw.WriteLine("Line4");


}      





FileInfo Class C#

C# > IO  > FileInfo Class

FileInfo

Use this class for working with files: create, open, delete, 

move, rename.

Methods

Properties






Thursday, October 16, 2014

String IsNullOrEmpty C# Example

C# > String > Methods > IsNullOrEmpty

With this method you can test simultaneously test whether a String is null or its value is Empty.

Example


string s = "test";
bool b = String.IsNullOrEmpty(s); // false
           
s = null;
b = String.IsNullOrEmpty(s); // true
           
s = "";
b = String.IsNullOrEmpty(s); // true

s = " ";

b = String.IsNullOrEmpty(s); // false








Wednesday, October 15, 2014

CONVERT SQL Server Example

SQL Server > Built-in Functions > CONVERT

CONVERT converts an expression of one data type to another.
Example

select CONVERT(nvarchar(30), GETDATE(), 100)

result
Oct 15 2014  9:54AM

Other examples:






Where keyword generic generic type constraint example C#

C# > Generics > where - generic type constraint

With where keyword you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates the generics class.


Example


    public class A
    {
    }
    public class B:A
    {
    }
    public class GenericList <T> where T : A
    {
    }

    GenericList<A> lst1 = new GenericList<A>(); // OK
    GenericList<B> lst2 = new GenericList<B>(); // OK B derives from A
    GenericList<int> lst3 = new GenericList<int>(); //Error  1      The type 'int' cannot be used as type parameter 'T' in the generic type or method 'WindowsFormsApplication1.GenericList'. There is no boxing conversion from 'int' to 'WindowsFormsApplication1.A'.
       






Tuesday, October 14, 2014

Ushort Type C# Example

C# > Types > ushort

The ushort keyword is an integral type.
Range: 0 to 63535


Example:

byte k = 63535; // ok
k = 63536;      // Error   




Byte Type C# Example

C# > Types > byte

The byte keyword is an integral type.
Range: 0 to 255


Example:

byte k = 255; // ok
k = 256;      // Error