We have a lot of keywords in C#, but we’ll learn the 10 most used keywords in this article. Let's have a look at the list:
- using
- class
- struct
- enum
- interface
- abstract
- null
- this
- throw
- finally
Using
Generally, we use the using keyword to add namespaces in code-behind and class files. Then it makes all the classes, interfaces and abstract classes and their methods and properties available in the current page. Adding a namespace can be done in the following two ways,
A. To allow the normal use of types in a namespace:
- using System.IO;
- using System.Text;
- using MyProject = TruckingApp.Services;
- MyProject.Truck newObj = new MyProject.Truck();
Let's say the Truck class is present in TruckingApp1, TruckingApp2 and TruckingApp3. Then it is difficult to call the Truck class of namespace2.
Here the alias directive gives an elegant syntax to use the Truck class.
Code
- using namespace1 = TruckingApp1.Services;
- using namespace2 = TruckingApp2.Services;
- using namespace3 = TruckingApp3.Services;
- namespace2.Truck newObj = new namespace2.Truck();
Class
Class is a keyword in C#.NET which is used to create a class. Classes are special kinds of templates from which you can create objects. Each object contains data and methods to manipulate and access that data. The class defines the data and the functionality that each object of that class can contain.
A class declaration consists of a class header and body. The class header includes attributes, modifiers, and the class keyword. The class body encapsulates the members of the class that are the data members and member functions. The syntax of a class declaration is as follows:
Attributes accessibility modifiers class identifier: baselist { body }
Attributes provide additional context to a class, like adjectives, for example, the Serializable attribute. Accessibility is the visibility of the class. The default accessibility of a class is internal. Private is the default accessibility of class members. The following table lists the accessibility keywords,
| Keyword | Description |
| public | Public class is visible in the current and referencing assembly. |
| private | Visible inside current class. |
| protected | Visible inside current and derived class. |
| Internal | Visible inside containing assembly. |
| Internal protected | Visible inside containing assembly and descendent of the current class. |
Modifiers refine the declaration of a class. The list of all modifiers are defined in the table as follows,
| Modifier | Description |
| sealed | Class can't be inherited by a derived class. |
| static | Class contains only static members. |
| unsafe | The class that has some unsafe construct likes pointers. |
| Abstract | The instance of the class is not created if the Class is abstract. |
For more details follow the link,
Struct
Struct is an encapsulated entity. Struct doesn't uses complete oops concept but is used for user defined data type. All the members of the struct have to be initialized, as it is value type.
A struct is a simple user-defined type, a lightweight alternative to a class. A structure in C# is simply a composite data type consisting of a number of elements of other types.
Similar to classes, structures have behaviors and attributes. As a value type, structures directly contain their value so their object or instance is stored on the stack.
Struct supports access modifiers, constructors, indexers, methods, fields, nested types, operators, and properties.
How to define struct
- public struct Student
- {
- int id;
- int zipcode;
- double salary;
- }
- Struct is used to improve the performance and clarity of code.
- Struct uses fewer resources in memory than class.
- When we have small and frequent use of some work use struct over classes.
- Performance can suffer when using structures in situations where reference types are expected due to boxing and unboxing.
- You should pass structs to method as ref parameters in order to avoid the performance loss associated with copying data.
- Structs reside on the stack, so we should keep them small.
- Structs can't be inherited and we can say they are sealed.
- Structure implicitly inherits from System.ValueType.
- The default constructor of a structure initializes each field to a default value. You cannot replace the default constructor of a structure.
- You can't define destructor for struct.
- Struct can be inherited from an interface.
For more details follow the link,
Enum
Enum is use to create enums in C#.NET. An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.
Enum type can be integer (float, int, byte, double etc.). But if you used beside int it has to be cast.
enum is used to create numeric constants in .NET framework. All member of enum are of enum type. There must be a numeric value for each enum type.
The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
- enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
- enums are enumerated data type in C#.
- enums are not for end-users, they are meant for developers.
- enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
- Enumerations (enums) make your code much more readable and understandable.
- enum values are fixed AND enum can be displayed as a string and processed as an integer.
- The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
- Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.
- enums are value types and are created on the stack and not on the heap.
For more details follow the link:
Interface
Interface is used to create an interface in C#. An interface looks like a class that can have a set of properties, methods, events and indexers, but has no implementation. The interface does not have an implementation of properties, methods, events and indexers because they are inherited by classes and structs, which must provide an implementation for each interface member.
An interface can be used to hide implementation details of classes from each other and it allows various objects to interact easily. It provides a separation between interface and implementation.
An interface is not only a contractual obligation to implement certain methods, properties, events and indexes but also provides a very low coupling between two classes. Interfaces in C# are provided as a replacement of multiple inheritances of classes because C# does not support multiple inheritance of classes.
Interfaces and abstract classes have similar purposes. Generally, an interface should be used when the implementation of the members it defines is not fixed, or when it is known that a single class requires the contents of several interfaces. Where the functionality of some members is fixed and the limited multiple inheritance facility is not required, abstract classes may be more appropriate.
How to create an Interface
Syntax
Interface interface_name
{
}
For more details follow the link:
Abstract
Abstract keyword is used to create class or as a method in C#, so let’s see how to use abstract keyword for creating class and also for method,
abstract class
Let's take an example: First of all, select a console application and use the following code,
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication13
- {
- abstract class mcn
- {
- public int add(int a, int b)
- {
- return (a + b);
- }
- }
- class mcn1: mcn
- {
- public int mul(int a, int b)
- {
- return a * b;
- }
- }
- class test
- {
- static void Main(string[] args)
- {
- mcn1 ob = new mcn1();
- int result = ob.add(5, 10);
- Console.WriteLine("the result is {0}", result);
- }
- }
- }
Output
The result is 15
Abstract method

Ashish SrivastavaPosted Apr 26, 2016, 12:22 AM
Very well explained...
Sr KarthigaPosted Feb 19, 2016, 6:00 AM
Nice Explanation
Sujeet SumanPosted Nov 20, 2015, 5:39 AM
Nice article..............
Former memberPosted Nov 18, 2015, 1:33 PM
very good
Banketeshvar NarayanPosted Nov 15, 2015, 5:46 AM
Great.. Nice article
Vijay KumarPosted Nov 6, 2015, 10:22 AM
nice & i think it's very good for Biginner
Former memberPosted Nov 1, 2015, 12:29 AM
nice
Santhakumar MunuswamyPosted Oct 25, 2015, 11:26 AM
Good Article
Muhammad Aqib ShehzadPosted Oct 24, 2015, 5:26 AM
nice and informative
Sibeesh VenuPosted Oct 24, 2015, 3:31 AM
Nice Share
NitinPosted Oct 24, 2015, 1:07 AM
Good one
Harshad PansuriyaPosted Oct 23, 2015, 11:59 PM
Nice one
Mukesh KumarPosted Oct 23, 2015, 1:15 PM
Great one sir!
Del WoodcockPosted Oct 23, 2015, 10:55 AM
Great information!
Gowtham KPosted Oct 23, 2015, 10:46 AM
Good one
Nilesh JadavPosted Oct 23, 2015, 8:12 AM
Nice Article sir !