CSharp-Tutorial (1)

Introduction

About C#

  • C# (C-Sharp) is a programming language developed by Microsoft that runs on the .NET Framework.
  • C# is used to develop web apps, desktop apps, mobile apps, games and much more.
  • It is an object-oriented programming language created by Microsoft that runs on the .NET Framework.
  • C# has roots from the C family, and the language is close to other popular languages like C++ and Java.
  • The first version was released in year 2002. The latest version, C# 12, was released in November 2023.

C# is used for:

  • Mobile applications
  • Desktop applications
  • Web applications
  • Web services
  • Web sites
  • Games
  • VR
  • Database applications
  • And much, much more!

keywords :- 

Access Modifiers

  • public
  • private
  • protected
  • internal

Data Types 

  • bool
  • byte
  • char
  • decimal
  • double
  • float
  • int
  • long
  • sbyte
  • short
  • string
  • uint
  • ulong
  • ushort

Control Flow

  • break
  • case
  • catch
  • continue
  • default
  • do
  • else
  • finally
  • for
  • foreach
  • goto
  • if
  • lock
  • return
  • switch
  • throw
  • try
  • while

Modifiers

  • abstract
  • async
  • const
  • event
  • extern
  • override
  • partial
  • readonly
  • sealed
  • static
  • unsafe
  • virtual
  • volatile

Others

  • as
  • base
  • checked
  • default
  • explicit
  • false
  • fixed
  • implicit
  • is
  • new
  • null
  • operator
  • sizeof
  • stackalloc
  • this
  • true
  • typeof
  • unchecked
  • void

Class Members

  • class
  • delegate
  • enum
  • interface
  • namespace
  • struct

Method Parameters

  • in
  • out
  • ref
  • params

C# Syntax :- 

using System;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use Console class from the System namespace
            Console.WriteLine("Hello World!");
        }
    }
}


Variable: 

variable is a storage location identified by a name that holds data which can be modified during program execution.

  • int age;
  • string name;

Identifier: 

an identifier is a name used to identify a variable, method, class, or any other user-defined item.

Note: 

Every C# statement ends with a semicolon ;.

Note:

C# is case-sensitive; “MyClass” and “myclass” have different meaning.

C# Output

To output values or print text in C#, you can use the WriteLine() method:

The curly braces {} marks the beginning and the end of a block of code.

C# Comments

Single-line comments start with two forward slashes (//).

Multi-line comments start with /* and ends with */.

C# Data Types

1. Value Types

2. Reference Types

3. Nullable Types

4. Enumerations (enum)

5. Structs

6. Tuples

1. Value Types

  • int: 32-bit integer
  • double: 64-bit floating-point
  • char: 16-bit Unicode character
  • bool: Boolean (true/false)
  • byte: 8-bit unsigned integer
  • short: 16-bit integer
  • long: 64-bit integer
  • float: 32-bit floating-point
  • decimal: 128-bit precise decimal

Syntax Data type:-

  • int number = 10;
  • float price = 9.99f;
  • double total = 100.50;
  • char letter = ‘A’;
  • string message = “Hello”;
  • bool isTrue = true;+

2. Reference Types

Class Types

  • Defined using class
  • Example: class Person { public string Name; public int Age; }
  • Array Types
  • Fixed-size or dynamic collection of elements
  • Example: int[] numbers = new int[5];

String Type

  • Sequence of characters
  • Example: string greeting = “Hello, World!”;
  • Delegate Types
  • Reference to methods
  • Example: delegate void Del(string message);

3. Nullable Types

Allow value types to be null

Example: int? nullableInt = null;

4. Enumerations (enum)

Enums are a distinct type that consists of a set of named constants called the enumerator list.

Example:-  enum Days { Sunday, Monday, Tuesday, 

Wednesday, Thursday, Friday, Saturday }

Days today = Days.Monday;

5. Structs

Structs are value types that are typically used to encapsulate small groups of related variables.

Example: struct Point { public int X; public int Y; }

6. Tuples

Tuples are data structures that can store multiple items of different types.

Example:

var person = (Name: “John”, Age: 30);

Console.WriteLine(person.Name);  // Outputs “John”

7. Dynamic Type

The dynamic type bypasses compile-time type checking. This allows for operations that will be resolved at runtime.

dynamic myVar = 1;

myVar = “Hello”; // No compile-time error

Type Casting

Type casting in C# is the process of converting a variable from one type to another. There are two main types of casting in C#: implicit casting and explicit casting.

Implicit Casting :-

Implicit casting is automatically performed by the C# compiler when there is no risk of data loss. This usually happens when converting from a smaller type to a larger type

.

char -> int -> long -> float -> double

Explicit Casting :-

Explicit casting is required when there is a possibility of data loss or when converting from a larger type to a smaller type. This type of casting requires the use of a cast operator.

  • double -> float -> long -> int -> char

Arrays

  • Arrays are fixed-size collections of elements of the same type.
  • Accessed using zero-based index.

Lists

  • Arrays are fixed-size collections of elements of the same type.
  • Accessed using zero-based index.

When to Use Each:

  • Use Arrays when the size is fixed or known.
  • Use Lists when flexibility and dynamic resizing are needed.

Interview Questions 

What is C#?

  • C# is a modern, object-oriented programming language developed by Microsoft. It is designed for building a variety of applications that run on the .NET Framework.

What is the .NET Framework?

  • The .NET Framework is a software framework developed by Microsoft that provides a controlled programming environment where software can be developed, installed, and executed on Windows-based operating systems.

Explain the public, private, protected, and internal access modifiers in C#.

  • public: Accessible from any other class.
  • private: Accessible only within the class in which it is declared.
  • protected: Accessible within its class and by derived class instances.
  • internal: Accessible only within files in the same assembly.

What is the difference between a class and an object?

  • A class is a blueprint for creating objects, defining the data and behavior that the objects will have. An object is an instance of a class.

What are the pillars of Object-Oriented Programming (OOP)?

  • The four pillars of OOP are Encapsulation, Inheritance, Polymorphism, and Abstraction.

Write A Comment