Wednesday, 26 November 2014

C# Tutorial and Notes

Static Classes

A class can be declared static, which indicates that it contains only static members. It is not possible to use the new keyword to create instances of a static class. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace that contains the class is loaded.
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.
Following are the main features of a static class:
·         They only contain static members.
·         They cannot be instantiated.
·         They are sealed.
·         They cannot contain Instance Constructors (C# Programming Guide).
Creating a static class is therefore basically the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object. Static classes cannot contain an instance constructor; however, they can have a static constructor. For more information

Static Members

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for example, a math library might contain static methods for calculating sine and cosine.
Static methods can be overloaded but not overridden.
Static class members are declared by using the static keyword before the return type of the member, for example:


Accessibility Levels


Use the access modifiers, public, protected, internal, or private, to specify one of the following declared accessibility for members.

 Public keyword

The public keyword is an access modifier for types and type members. Public access is the most permissive access level. There are no restrictions on accessing public members, as in this example:
class SampleClass
{
    public int x; // No access restrictions.
}


Protected keyword


The protected keyword is a member access modifier. A protected member is accessible within its class and by derived classes. For a comparison of protected with the other access modifiers, see Accessibility Levels.
A protected member of a base class is accessible in a derived class only if the access occurs through the derived class type. For example, consider the following code segment:

// protected_keyword.cs
using System;
class A
{
    protected int x = 123;
}

class B : A
{
    static void Main()
    {
        A a = new A();
        B b = new B();

        // Error CS1540, because x can only be accessed by
        // classes derived from A.
        // a.x = 10;
       
        // OK, because this class derives from A.
        b.x = 10;  
    }
}
The statement a.x =10 generates an error because A is not derived from B.
Struct members cannot be protected because the struct cannot be inherited.

Internal keyword
The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly, as in this example:
public class BaseClass
{
    // Only accessible within the same assembly
    internal static int x = 0;
}


Private keyword


The private keyword is a member access modifier. Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared, as in this example:
class Employee
{
    private int i;
    double d;   // private access by default
}

In this example, the Employee class contains two private data members, name and salary. As private members, they cannot be accessed except by member methods. Public methods named GetName and Salary are added to allow controlled access to the private members. The name member is accessed by way of a public method, and the salary member is accessed by way of a public read-only property
// private_keyword.cs
using System;
class Employee
{
    private string name = "FirstName, LastName";
    private double salary = 100.0;

    public string GetName()
    {
        return name;
    }

    public double Salary
    {
        get { return salary; }
    }
}

class MainClass
{
    static void Main()
    {
        Employee e = new Employee();

        // The data members are inaccessible (private), so
        // then can't be accessed like this:
        //    string n = e.name;
        //    double s = e.salary;

        // 'name' is indirectly accessed via method:
        string n = e.GetName();

        // 'salary' is indirectly accessed via property
        double s = e.Salary;
    }
}

Declared accessibility
Meaning
public
Access is not restricted.
protected
Access is limited to the containing class or types derived from the containing class.
internal
Access is limited to the current assembly.
protectedinternal
Access is limited to the current assembly or types derived from the containing class.
private
Access is limited to the containing type.


How to upload a file to Server

File is a collection of related inforation and in web development area there are many ocassion when we make to upload a file to web server here is code will help you upload a picture file to your server.

Important point

  • Make  a folder on your server named image
  • Use system.IO  name space
File is a collection of related inf
Control : (FileUpload,Label,Button)
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    public static bool getValidPicture(string str)
    {
        str = Path.GetExtension(str);
        switch (str.ToLower())
        {
            case ".jpg":
                return true;
            case ".jpeg":
                return true;
            case ".bmp":
                return true;
            case ".gif":
                return true;
            case ".png":
                return true;
            default:
                return false;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (getValidPicture(FileUpload1.FileName) == true)
        {
            string picpath = "~/image/" + FileUpload1.FileName;
            FileUpload1.SaveAs(MapPath(picpath));
            Label1.Text = "Saved";
        }
        else
        {
            Label1.Text = "Invalid Picrure format";
        }
    }

}