C# Basics

C#, a programming language used to develop applications of various types and it is read as CSharp. Which is an Object-oriented developed by Microsoft. Anders Hejlsberg is the lead architect for C#.Net.

C# is designed for CLI, which has executable code and run-time environment that permit use of various high-level languages on various platforms and architectures.
  • C# is general purpose language which can produce efficient programs. 
  • C# program can be compiled on different platforms. 
  • C is a part of dot Net Framework. 

It can be used to create windows applications, web applications, and Web services. Visual Studio is the best IDE for writing C# programs.

Every C# program consists of a Namespace, Class, Main method, Methods, Statements, Comments etc. 

Take a look at the basic program: 

using System;

namespace first_namespace
{
    class first_program
    {
        static void Main()
        {
            Console.WriteLine("Csharp World");
        }
    }
}
Output: Csharp World

Extension of a C# program is ".cs".

Keyword "using" is to include namespaces. Any C# program can have one or more using statements in it. 

Syntax for calling a namespace:

using <namespace>;

A namespace is a group of classes and methods. 

In the above example we have created a namespace with name first_namespace. 

Syntax: 

namespace namespace_name
{
   class class_name
   {
         //members
   }
}

If we write System.Console.WriteLine("Csharp World"); then for this line of code we don't require to call System namespace with using keyword.

namespace first_namespace
{
    class first_program
    {
        static void Main()
        {
            System.Console.WriteLine("Csharp World 2");
        }
    }
}
Output: Csharp World 2

In the above example we did not write using keyword because we prefixed System namespace with Console class directly.







A class in C# is used to create a custom type or template, it allows us to create objects. Class can have members like objects, variables, and methods in it. A class is a reference type.



   [<Modifiers>] class class_name
   {
         //members;
   }

Every statement and an expression should end with a semicolon(;).

CSharp is case-sensitive, all the predefined libraries are implemented in "Proper Case". Proper Case means the first character of every word is Capital/Upper Case. 

In C#, all the keywords are in lower case.

Syntax:

static <void/int> Main([<String[] args>])
{
 // statements or code. 
}
  • Return type of Main method can be void or int only and Main method should be declared as static. 
  • Main method is the entry point of all C# programs. 

Comments:

We use //(double forward slash) to indicate Single line comments. 

Example 1:
static <void/int> Main([<String[] args>])
{
 // statements or code. 
}

We use / * */ for Multi-line comments:

Example 2:
/* statements..
    statements..
    statements..
*/
The text inside the comments will not be excluded, they're only to give information. 

C# Primitive Data Types:

bool
Signed integral:
  •   sbyte
  •   short 
  •   int 
  •   long
Unsinged intergral:
  •   byte
  •   ushort
  •   uint
  •   ulong
floating point:
  •   float
  •   double
char
decimal

List of Keywords in C#:

as                abstract           base           bool

break           byte                catch          case
char             checked          clonst         class
continue      decimal           default        delegate
do               double             else            enum
event           explicit           extern         false
finally          fixed               for              float
foreach        goto                if                implicit
in                 int                  interface     internal
is                 long                lock            namespace
new             null                 object         operator
out              override          params        private
public          protected        ref              readonly
return          sbyte              sealed         short
sizeof           stackalloc       static          string
struct           switch            this             throw
try               true                typeof         uint
ulong           unchecked       ushort         unsafe      
using           using static      virtual         volatile
void             while


Operators in C#: 

Arithmetic                              :  +, -, *, /, %

Assignment                             : =, +=, -=, *=, /=, %=

Comparison                            : ==, !=, <, <=, >, >=, is, as, Like


Concatenation                        : +


Increment and Decrements     : ++, --


Logical                                   : &&, ||, ^


No comments:

Post a Comment