Universe Guide

Home / Facts

Objects in .NET

What is a SEALED class?

A sealed class is a class that cannot be inherited from. The below code will cause an error if you attempt to compile.

 sealed class person { string FirstName { get; set; } 
string SurName { get; set; } }
class employee : person { }

What is an ABSTRACT class?


An abstract class is a class that cannot be instantiated so for our example below, you will get an error when you attempt to compile it.

class Program
{ static void Main(string[] args) { person Emp = new person(); } } abstract class person { string FirstName { get; set; } string SurName { get; set; } } class employee : person { string EmpNo; }

If you change person Emp = new person(); to person Emp = new employee(); , it will work but you won`t have access to EmpNo property.

What is a PARTIAL class?


A partial class is half or more of a class that can be split across many files. This makes it ideal for when multiple users want to work on the same file. You have two files each with a partial class in it and carry on working.

public partial class Person { public string FirstName { get; set; } public string FullName() { return FirstName + " " + SurName; } } public partial class Person { public string SurName { get; set; } } class Program { static void Main(string[] args) { Person A = new Person(); A.FirstName = "Joe"; A.SurName = "Bloggs"; Console.WriteLine("Employee :- " + A.FullName().ToString(>; Console.ReadLine(); } }

What is a VIRTUAL function?


A virtual function is a function that can be overridden in the inheriting class. If you don`t inherit, you will be able to access the base function. Any time you overwrite a function, the newer function must have the override keyword to indicate you`re overriding the base class.

class Program { static void Main(string[] args) { person Emp = new employee(); Emp.FirstName = "Joe"; Emp.SurName = "bloggs"; Console.WriteLine(Emp.FullName(>; Console.ReadKey(); } } class person { public string FirstName { get; set; } public string SurName { get; set; } public virtual string FullName() { return FirstName + " " + SurName; } } class employee : person { string empNo; public override string FullName() { return SurName + " " + FirstName; } }

In the above example, the name written in bloggs joe because it is using the overriding function.

What are Auto-Implemented Properties?


In .NET 3.0, these were created to make coding easier and quicker. When you define a property previously, you had to put in code to store the value in a private variable. With AIP, you don`t need to do that. In the below example, FirstName is using the traditional way of properties whereas Surname is an Auto-Implemented Property.

public class Person { private string m_strFirstName = ""; public string FirstName { get { return m_strFirstName; } set { m_strFirstName = value; } } public string Surname { get; set; } public string FullName() { return m_strFirstName + " " + Surname; } } class Program { static void Main(string[] args) { Person A = new Person(); A.FirstName = "Joe"; A.Surname = "Bloggs"; Console.WriteLine("Employee :- " + A.FullName().ToString(>; Console.ReadLine(); } }




Last Updated :

Related Pages of Interest :-



Add a Comment


Name:
Email: (Optional)
Comment: