C#-Syntax

Syntax in C#

C#-Syntax : C# is an object-oriented programming language. In Object-Oriented Programming methodology, a program consists of various objects that interact with each other by means of actions. The actions that an object may take are called methods. Objects of the same kind are said to have the same type or, are said to be in the same class.


Let us look at implementation of a Rectangle class and discuss C# basic syntax :

C#-Syntax Example:

using System;
namespace RectangleApplication {
   class Rectangle {
      // member variables
      double length;
      double width;
      
      public void Acceptdetails() {
         length = 4.5;    
         width = 3.5;
      }
      public double GetArea() {
         return length * width; 
      }
      public void Display() {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
         Console.WriteLine("Area: {0}", GetArea());
      }
   }
   class ExecuteRectangle {
      static void Main(string[] args) {
         Rectangle r = new Rectangle();
         r.Acceptdetails();
         r.Display();
         Console.ReadLine(); 
      }
   }
}

The using Keyword:

The first statement in any C# program is:

using System;

The using keyword is used for including the namespaces in the program. A program can include multiple using statements.


The class Keyword:

The class keyword is used for declaring a class.


C# Keywords:

Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.

In C#, some identifiers have special meaning in context of code, such as get and set are called contextual keywords.


The following table lists the reserved keywords and contextual keywords in C# :

Reserved Keywords
abstractasbaseboolbreakbytecase
catchcharcheckedclassconstcontinuedecimal
defaultdelegatedodoubleelseenumevent
explicitexternfalsefinallyfixedfloatfor
foreachgotoifimplicitinin (generic modifier)int
interfaceinternalislocklongnamespacenew
nullobjectoperatoroutout (generic modifier)overrideparams
privateprotectedpublicreadonlyrefreturnsbyte
sealedshortsizeofstackallocstaticstringstruct
switchthisthrowtruetrytypeofuint
ulonguncheckedunsafeushortusingvirtualvoid
volatilewhile     
Contextual Keywords
addaliasascendingdescendingdynamicfromget
globalgroupintojoinletorderbypartial (type)
partial
(method)
removeselectset   

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *