0% found this document useful (0 votes)
3 views

C-Sharp

The document explains type casting in C#, detailing implicit and explicit type casting along with the sizes of various data types. It covers key concepts such as classes, namespaces, properties, access modifiers, initializers, indexers, static methods, abstraction, delegates, and lambda expressions. Each concept is accompanied by syntax examples and descriptions of their functionalities within the C# programming language.

Uploaded by

dhakalshishir982
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C-Sharp

The document explains type casting in C#, detailing implicit and explicit type casting along with the sizes of various data types. It covers key concepts such as classes, namespaces, properties, access modifiers, initializers, indexers, static methods, abstraction, delegates, and lambda expressions. Each concept is accompanied by syntax examples and descriptions of their functionalities within the C# programming language.

Uploaded by

dhakalshishir982
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Type casting:

The conversion of one data type into another data type in c sharp is known as typecasting. There
are two types of typecasting in c sharp:
1) Implicit Type Casting
2) Explicit Type Casting
Size of data types in bits:
Byte(8) -> bool(8) -> char(16)-> int(32)-> string(32)-> float(32)-> long(64)-> double(64)-
>decimal(128)
IMPLICIT TYPE CASTING:
This is a automatic type casting in c sharp that converts low bit data types into high bit data
types.
EXPLICIT TYPE CASTING:
This is a manual conversion of data types that converts high bit data types into low bit data types.

Type System:
Class:
Class is unit that contains properties and behaviors describing the name class with different
activities like encapsulation, methods, abstraction, inheritance etc.

Syntax:
Modifier Class Name {
//properties
//behaviors
}

Class Student {
Public int a=
}

Namespace:
Syntax:
namespace AnyNameSpace{
class ItsClass{
}
}
Another syntax:
namespace AnyNameSpace;
class ItsClass{
}

Namespace groups same modules within a group and is used for bigger projects. It is just space
with a name.

Properties:
Properties are variables defined as global parameters. Normally, the defined properties are set
into the constructor as initializer.
Access Modifiers:
Public
Private
Protected
Static
Required
 Public modifier:
The value of public instances of a class can be called from any class not necessarily the
child class.
Public int p=30;
Public void Mymethod(){

}
 Protected Modifier:
The value of protected instances of a class can only be called within same class and its
child class.
Class A {
protected void Mycat(){

}
}

Class B : A {
void Animal(){
//we can call Mycat method here
}
}

 Private Modifier:
The value of private instances of a class can only be called within same class only.

Initializer:

 Class MyClass(){
//properties || modifier || initializers
Myclass(){

}
}
 Class P {
Private int num = 2;
//
}

 Class P {s
Public required String fullName(){get;set}
Public required String Address(){get;set}

}
The required modifier insist any object to define the function to access the object data.
While creating an object for class P we must pass fullname and address while defining it.
P obj = new P({fullName:”Ram”,Address:”Basundhara”})
Questions:
 What is properties in c-sharp and dotnet? Explain how you can create properties.
Answer:
In C#, properties are special members of a class that provide a flexible mechanism to read, write,
or compute the values of private fields. Properties look like fields from the outside, but they act
like methods internally, allowing validation or encapsulation of the underlying data.
using System;

namespace PropertyNS; A1Q

public class PropertyClass


{

private string name;

public string Name


{
get
{
return name;
}
set
{
name = value;
}
}
}

 What is a constructor in c-sharp? What are the methods that you can create constructors
in c-sharp?
Answer:
 What is an indexer? Demonstrate an example for it.
 What is a namespace in OOP? Demonstrate with an example.
 What is static method? Explain in terms of class and constructor.
 What is a finalizer in c sharp? Explain.
Indexer:
Indexers are a syntactic convenience that enables you to create the class, struct or interface that
client applications can access as an array.
Here, this [int index] defines a indexer.
namespace IndexerNS;
using System;
class MyIndexerClass{
public int[] arr = [2,6,8,9,3];
//creating indexer
public int this[int index]{
get => arr[index];
set=> arr[index] = value;
}
}

Static Method:
 Runs once only.
 Can’t be instantiated
 “new” keyword can’t be used to create instance.
 Only accepts static fields, properties, methods,
 Don’t allow any modifiers (public, )
 Runs automatically.
Static constructor:
 Runs at first while any instance is created
 Runs only once
 Doesn’t accept parameter.
 using System;
 namespace StaticMethodNS;

 static class MyStaticClass
 {
 public static void StaticMethod()
 {
 Console.WriteLine("called directly from class");
 }

 }
Abstraction:
The abstract modifier indicates that the thing being modified has a missing or incomplete
implementation. It can be used in class, methods, properties, indexers and events.
 Properties:
o It cannot be instantiated.
o It may contain method and accessor
o A non-abstract class derived from an abstract class must include actual
implementation of all inherited abstract methods and accessors.
o Because an abstract method declaration provides no actual implementation there
is no method body.
o The implementation is provided by a method override which is a method of non-
abstract class.
Delegates:
A delegate is an object which refers to a method that can hold a reference to the
method. It is similar to function pointer in c and c++ but it works as an object.
Features:
o It provides a good way to encapsulate the methods.
o These are type safety.
o These are mainly used in implementing callback methods and events.
Syntax:
Modifier delegate DelegateName (Parameters);
Public delegate int Calculation (int a, int b);

LambdaExpression:
Lambda expressions in C sharp are used like anonyms function (running function one
after another, after completing each function) with the difference that in lambda
expression we don’t need to specify the type of the value.
It is denoted by arrow (=>)
There are two types of lambda expression:
1) Expression Lambda:
Input => Expression;
2) Statement Lambda:
Input => {Statements};
Generally, we use one or two statements inside statement lambda expression.

You might also like