Home > DeveloperSection > Articles > Keywords in C#

Keywords in C#


C# C# 
Ratings:
0 Comment(s)
 2561  View(s)
Rate this:

Keywords in C#

In this blog is m trying to explain the concept of keywords

Keywords are predefined, keyword have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @while is a valid identifier but if is not because while is a keyword.

The first table in this topic lists keywords that are reserved identifiers in any part of a C# program.

abstract

The abstract modifier indicates that the thing being modified has a missing or incomplete implementation. The abstract modifier can be used with classes, methods, properties, indexers, and events.

as

You can use the as operator to perform certain types of conversions between compatible reference types or null able types.

base

The base keyword is used to access members of the base class from within a derived class.

bool

The bool keyword is an alias of System.Boolean. It is used to declare variables to store the Boolean values, true and false.

break

The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.

byte

The byte keyword denotes an integral type that stores values as indicated in the following table.

case

The case switch statement is a control statement that selects a switch section to execute from a list of candidates.

catch

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions.

char

The char keyword is used to declare an instance of the System.Char structure that the .NET Framework uses to represent a Unicode character. The value of a Char object is a 16-bit numeric (ordinal) value.

checked

The checked keyword is used to explicitly enable overflow checking for integral-type arithmetic operations and conversions.

class

Classes are declared using the keyword class.

const

The const keyword is used to modify a declaration of a field or local variable. It specifies that the value of the field or the local variable is constant, which means it cannot be modified

continue

The continue statement passes control to the next iteration of the enclosing while, do, for, or foreach statement in which it appears.

decimal

The decimal keyword indicates a 128-bit data type. Compared to floating-point types, the decimal type has more precision and a smaller range, which makes it appropriate for financial and monetary calculations.

default

The default keyword can be used in the switch statement or in generic code:

<![if !supportLists]> ·         <![endif]>The switch statement: Specifies the default label.

<![if !supportLists]> ·         <![endif]>Generic code: Specifies the default value of the type parameter. This will be null for reference types and zero for value types.

delegate

The declaration of a delegate type is similar to a method signature. It has a return value and any number of parameters of any type

do

The do statement executes a statement or a block of statements repeatedly until a specified expression evaluates to false. The body of the loop must be enclosed in braces, {}, unless it consists of a single statement. In that case, the braces are optional.

double

The double keyword signifies a simple type that stores 64-bit floating-point values. The following table shows the precision and approximate range for the double type.

else

An if statement identifies which statement to run based on the value of a Boolean expression. In the following example, the Boolean variable result is set to true and then checked in the if statement. The output is The condition is true.

enum

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list.

event

Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class).

explicit

The explicit keyword declares a user-defined type conversion operator that must be invoked with a cast

extern

The extern modifier is used to declare a method that is implemented externally. A common use of the extern modifier is with the DllImport attribute when you are using Interop services to call into unmanaged code.

finally

By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

fixed

The fixed statement prevents the garbage collector from relocating a movable variable. The fixed statement is only permitted in an unsafe context. Fixed can also be used to create fixed size buffers.

float

The float keyword signifies a simple type that stores 32-bit floating-point values. The following table shows the precision and approximate range for the float type.

for

By using for loop, you can run a statement or a block of statements repeatedly until a specified expression evaluates to false. This kind of loop is useful for iterating over arrays and for other applications in which you know in advance how many times you want the loop to iterate.

foreach

The foreach statement is used to iterate through the collection to get the information that you want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects. If you need to add or remove items from the source collection, use a for loop.

goto

The goto statement transfers the program control directly to a labeled statement.

if

An if statement identifies which statement to run based on the value of a Boolean expression. In the following example, the Boolean variable result is set to true and then checked in the if statement. The output is The condition is true.

implicit

The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.

in (generic modifier)

For generic type parameters, the in keyword specifies that the type parameter is contra variant. You can use the in keyword in generic interfaces and delegates.

int

The int keyword denotes an integral type that stores values according to the size and range.

interface

An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. In the following example, class Implementation Class must implement a method named Sample Method that has no parameters and returns void.

internal

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.

is

Checks if an object is compatible with a given type.

lock

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock. The following example includes a lock statement.

long

The long keyword denotes an integral type that stores values according to the size and range.

namespace

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

new

In C#, the new keyword can be used as an operator, a modifier, or a constraint.

<![if !supportLists]> ·         <![endif]> new Operator

<![if !supportLists]> o   <![endif]>Used to create objects and invoke constructors.

<![if !supportLists]> ·         <![endif]> new Modifier

<![if !supportLists]> o   <![endif]>Used to hide an inherited member form a base class member.

<![if !supportLists]> ·         <![endif]> new Constraint

<![if !supportLists]> o   <![endif]>Used to restrict types that might be used as arguments for a type parameter in a generic declaration.

 

null

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. Ordinary value types cannot be null. However, C# 2.0 introduced nullable value types.

object

The object type is an alias for Object in the .NET Framework. In the unified type system of C#, all types, predefined and user-defined, reference types and value types, inherit directly or indirectly from Object. You can assign values of any type to variables of type object. When a variable of a value type is converted to object, it is said to be boxed. When a variable of type object is converted to a value type, it is said to be unboxed. For more information.

operator

Use the operator keyword to overload a built-in operator or to provide a user-defined conversion in a class or struct declaration.

out

The out contextual keyword is used in two contexts:

<![if !supportLists]> o   <![endif]>As a parameter modifier in parameter lists

<![if !supportLists]> o   <![endif]>In generic type parameter declarations in interfaces and delegates

out (generic modifier)

For generic type parameters, the out keyword specifies that the type parameter is covariant. You can use the out keyword in generic interfaces and delegates.

override

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

params

The params keyword lets you specify a method parameter that takes a variable number of arguments.

private

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.

protected

The protected keyword is a member access modifier. A protected member is accessible within its class and by derived class instances. For a comparison of protected with the other access modifiers.

public

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.

readonly

The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

ref

The ref keyword causes an argument to be passed by reference, not by value. The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

return

The return statement terminates execution of the method in which it appears and returns control to the calling method. It can also return an optional value. If the method is a void type, the return statement can be omitted.

sbyte

The sbyte keyword indicates an integral type that stores values according to the size and range.

sealed

When applied to a class, the sealed modifier prevents other classes from inheriting from it.

short

The short keyword denotes an integral data type that stores values according to the size and range.

sizeof

Used to obtain the size in bytes for an unmanaged type. Unmanaged types include the built-in types that are listed in the table that follows, and also the following:

<![if !supportLists]> o   <![endif]>Enum types

<![if !supportLists]> o   <![endif]>Pointer types

<![if !supportLists]> o   <![endif]>User-defined structs that do not contain any fields or properties that are reference types

stackalloc

The stackalloc keyword is used in an unsafe code context to allocate a block of memory on the stack.

static

Use the static modifier to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

string

The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.

struct

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory.

switch

The switch statement is a control statement that selects a switch section to execute from a list of candidates.

this

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

throw

The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution.

true

Used as an overloaded operator or as a literal:

<![if !supportLists]> o   <![endif]> true Operator

<![if !supportLists]> o   <![endif]> true Literal

try

The try-catch statement consists of a try block followed by one or more catch clauses, which specify handlers for different exceptions.

typeof

Used to obtain the System.Type object for a type. A typeof expression takes the following form:

System.Type type = typeof(int);

uint

The uint keyword signifies an integral type that stores values according to the size and range.

ulong

The ulong keyword denotes an integral type that stores values according to the size and range.

unchecked

The unchecked keyword is used to suppress overflow-checking for integral-type arithmetic operations and conversions.

unsafe

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

ushort

The ushort keyword indicates an integral data type that stores values according to the size and range.

using

The using keyword has two major uses:

<![if !supportLists]> o   <![endif]>As a directive, when it is used to create an alias for a namespace or to import types defined in other namespaces.

<![if !supportLists]> o   <![endif]>As a statement, when it defines a scope at the end of which an object will be disposed.

virtual

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class.

void

When used as the return type for a method, void specifies that the method doesn't return a value.

volatile

The volatile keyword indicates that a field might be modified by multiple threads that are executing at the same time. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.

while

The while statement executes a statement or a block of statements until a specified expression evaluates to false.

 


Don't want to miss updates? Please click the below button!

Follow MindStick