GST Billing Software

Structure: C# Class Properties: Indentation Members Accessibility

header image

Classes and Objects

Erachana Line

A class is a data structure that combines state (fields) and actions (methods and other function members) in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects. Classes support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes.

New classes are created using class declarations. A class declaration starts with a header that specifies the attributes and modifiers of the class, the name of the class, the base class (if given), and the interfaces implemented by the class. The header is followed by the class body, which consists of a list of member declarations written between the delimiters { }.

Using System //Header file//

Namespace HelloWorld //namespace is a collection of classes//

{

Class ClassName

{

Variable Declaration

Function();

PublicStaticVoidMain(String arg[]) //Public - access to all ,

Void - Returns value//

{

}

}

}

Example:

public class Point

{

public int x, y;

public Point(int x, int y)

{

this.x = x;

this.y = y;

}

}

Instances of classes are created using the new operator, which allocates memory for a new instance, invokes a constructor to initialize the instance, and returns a reference to the instance. The following statements create two Point objects and store references to those objects in two variables:

Point p1 = new Point(0, 0);

Point p2 = new Point(10, 20);

The memory occupied by an object is automatically reclaimed when the object is no longer reachable. It is neither necessary nor possible to explicitly deallocate objects in C#.

  • It should have main class

• Declaration, function terminates with semicolon ( ; )

  • Case Sensitive

• Class and File name must be same.

1. Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.

2. Comments should be on the same level as the code (use the same level of indentation).

3. Always place curly braces ({ and }) in a new line.

4. Use one blank line to separate logical groups of code.

5. There should be one and only one blank line between each method in the class.

6. The curly braces should be in a separate line and not in the same line as if, for etc.

7. Use a single space before and after each operator and brackets.

8. Keep private member variables, properties and methods at the top of the class file and public members after private member variable definition.

9. Place all namespaces ("using" statements) together at the top of the class file.

10. Use #region to group related pieces of code together.

The members of a class are either static members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes).

The following provides an overview of the kinds of members a class can contain:

• Constants: Constant values associated with the class

• Fields: Variables of the class

• Methods: Computations and actions that can be performed by the class

• Properties: Actions associated with reading and writing named properties of the class

• Indexers: Actions associated with indexing instances of the class like an array

• Events: Notifications that can be generated by the class

• Operators: Conversions and expression operators supported by the class

• Constructors: Actions required to initialize instances of the class or the class itself

• Finalizers: Actions to perform before instances of the class are permanently discarded

• Types: Nested types declared by the class.

Each member of a class has an associated accessibility, which controls the regions of program text that are able to access the member. There are six possible forms of accessibility. These are summarized below.

  • Public: Access not limited
  • Protected: Access limited to this class or classes derived from this class
  • Internal: Access limited to the current assembly (.exe, .dll, etc.)
  • Protected Internal: Access limited to the containing class, classes derived from the containing class, or classes within the same assembly
  • Private: Access limited to this class
  • Private Protected: Access limited to the containing class or classes derived from the containing type within the same assembly