Showing posts with label ClassesCsharp. Show all posts
Showing posts with label ClassesCsharp. Show all posts

Visibility Modifiers

Visibility Modifiers in C#
We have already encountered quite a number of so-called modifiers-keywords that can be applied to a type or to a member. Modifiers can indicate the visibility of a method, such as public or private, or the nature of an item, such as whether a method is virtual or abstract. C# has a number of modifiers, and at this point it’s worth taking a minute to provide the complete list.

Abstract Classes

C# allows both classes and functions to be declared as abstract. An abstract class cannot be instantiated, while an abstract function does not have an implementation, and must be overridden in any non abstract derived class.
Obviously, an abstract function is automatically virtual. If any class contains any abstract  functions, then that class is also abstract and must be declared as such. 
  • Abstract class declaration is:
abstract class MyBaseClass
{
       
}

Inheritance in C#

Inheritance allows you to create several classes that are distinct but share a common functionality. Specialized classes, called derived classes, inferit from a common class called the base class. A derived class is also said to extend a base class. The base class encapsulates the common functionality that will be present in each derived class, while the derived classes provide additional functionality specific to each class
  • If you want to declare that a class derives from another class, use the following syntax: 
   class MyDerivedClass : MyBaseClass
   {
     //Function and data members here

   }

Sealed Classes and Methods

Sealed Classes and Methods in C#C# allows classes and methods to be declared as sealed. In the case of a class, this means that you can’t inherit from that class. In the case of a method, this means that you can’t override that method.

sealed class mClass
{
  // etc.


Destructors in C#

We have seen that constructors allow you to specify actions that must take place whenever an instance of a class is created. Conversely, destructors are called before an object is destroyed by the garbage collector. Given this behavior, a destructor would initially seem like a great place to put code to free unmanaged resources and perform a general cleanup. Unfortunately, things are not so straightforward.

Constructors in C#

The syntax for declaring basic constructors in C# is the same as in Java and C++. We create a method that has the same name as the containing class, and which does not have any return type:
public class MyClass
{
   public MyClass()
   {
   }
}

Properties in C#

Properties in C# is a member that provides  a flexible work to write, read or compute a value of a private field. Properties is one or two code blocks and representing a get and set accessor. For get accessor the code block executed when the property is read and for the set executed when the property is assigned a new value. Set property is only for reading and get property considered write – only.

Defining Properties

Class Introduction

C# Language is all about Object Oriented programming and classes. A class is a group of related methods and variables. Classes describes these things, and in most cases, you create an instance of this class, now referred to as an object, on this object, you use the defined methods and variables. The Windows Form itself is a Class, and when you run your programme, you are creating Objects , example: a Form object, a Textbox object etc. When define a class, define a design for a data type.

Class definition: