Csharp notes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 9

1)if we are developing any application 1st we need put proper file structure, 2nd

coding style easier to read and understand.provide meaning ful variable and
function names.

what is c#?
1)csharp is one of the microsoft programming language to work with .net framework
and .net core using this we can build different types of applications.
2)csharp is completely object-oriented programming language.it supports 4
principles Inheritance,Abstraction,polymorphism,and Encapsulation.

visual studio is a IDE tool.using this tool we can develop,build,compile,publish


and run application with the .net framework.

Basic structur of c# programming:


1)Import base class libraries as well as user defined namespace/Importing Namespace
2)declare namespace 3)class name 4)proerties/DataMembers 5)Functions/methods/
6)Statements

After creating the project floder structure is


1st Solution name 2)project name 3)class containing the main method.

in c# we have different datatypes based on value scenario we mention the datatype.


Value DataType:
The datatype which stores the value directly in the memory is called the value data
type.
reference datatype:
the datatype which is used to store the reference of variable is called reference
data type.

What is typecasting?
1)typecasting is the process of changing one datatype value in to another datatype
value.
Types of Casting :they are two types.1)implicit 2)Explicit both should be
compatiable to each other.
1)the implicit type casting is automatically done by the compiler from a smaller
datatype to larger datatype.There will be no data loss.

Explicit Conversion: here we use cast expression


1)the explicit casting Is done by the developer.if we want to convert larger
datatype to smaller datatype.there will be a dataloss.

ReadOnly Struct:
1)When we declare a field using the readonly modifier.readonly modifier indicates
to the fields only part of the declaration or in a constructor of the same
class.readonly only fields can only be assigned or reassigned multiples times only
at the declaration or in a constructor.
What is concurrentBag<T> class in c#?
1)

what is BlockingCollection in c#?


1)The blockingcollection is a concurrent class which provides thread
safety.multiple threads can add as well as remove objects form the
blockingcollection concurrently.

the blockingcollection implements the producer-consumer pattern.both threads will


share a common collection class to exchange the data between them.the producer
thread is going to generate the data while the consumer thread is going to consume
the data

Object-Oriented programming

How do we Develop Applications?


1)Object-Oriented Programming is a strategy that provides some principles for
developing applications is more related to the designing of software.

1)Reusability:Class and Object


Rather than copy and pasting the same code repeatedly in different places.we can
create a class and make an instance of that class and reuse it whenever we want.
2)Extensibility :Inheritance 3)Simplicity:
Abstraction,Encapsulation and polymorphism

Object-Oriented provide 4 oops principles:


1)Encapsulation 2)Abstraction 3)Polymorphism and 4)Inheritance
we use classes and objects to implement oops principles

Class and Objects in C#


-------------------------
1)class and object address the reusability functionality.Rather than copy and
pasting the same code repeatedly in different places.we can create a class and make
an instance of that class and reuse it whenever we want.

class:
A class is user-defined data type that represent both state and behaviour. the
state represents the properties and behaviour is the action that object can
perform.

object:
Object is an instance of a class.All the memebers of a class can be accessed
through the object.to access the class members we need to use the (.) dot
operator.if we want to create an object of a class we need to use the new keyword.

**Types of Classes:
1)Abstract Class 2)sealed class 3)partial class 4)static class

Encapsulation:
1)Encapsulation hides the internal state and functionality of an object and only
allows access through a public set of functions.

2)The process of binding the data members and member function together into a sinle
unit. [Class , interface ,Enum ,Struct etc ]

3)Using Encapsulation we can achieve Data Hiding. is a process which we hide the
internal data from the outside world.
4)We declare variable as private to stop accessing them directly from the outside
world. the public getter and setter methods are used to access the private
variables from outside the class.

What is Abstraction in C#?


1)The process of representing the essential features without including the
background details.It means we need to expose what is necessary and compulsory. and
we need to hide unnecessary things.
Constructors:

What is a constructor?
1)Constructor is a special method present inside a class that is responsible for
initializing the variables of that class.

2)the name of the constructor is exactly the same name as the class.
3)Every field or variable we declared inside a class has a default value.

4)we can also define a constructor under the class and if we define it .we can call
it an explicit constructor and an explicit constructor can be parameter less and
parameterized also.

Types of Constructors:
1)Default or parameter less Constructor 2)Parameterized constructor
3)private constructor 4)static constructor 5)copy constructor

when should we define a parameterized constructor in a class?


if we want to initialize the object dynamically with the user given values.

What are Access Specifiers in c#?


1)we have 6 types of access specifiers public,private,protected,internal,protected
internal,private protected.

using access modifier we can define the scope of a


type(class,interface,structs,Enum,Delegate etc) and its member (Variables ,
properties ,constructor and methods).

using this access modifier we are able do who can access them and who cannot access
them.

OOPS

Inheritance:
1)A class is collection of members. the members defined in one class can be
consumed by another class by establishing relationship between two classes.
2)Using inhertiance we avoid rewriting the code.
3)Derived class can consume all the bass class members(public and protected) except
private(we can use protected method to access private member).

there are some types of Inheritance:


1)single:One base class and one derived class
2)multilevel:a class derived from another derived class
3)multiple classes can derived from single base class
C# does not support multiple base class but allows multiple interface
implementation.

[Base Keyord is used to access the base class methods ,constructors ,properties
from the derived class.

Abstract class and abstract methods:


-----------------------------------
1)if we want to use method as abstract we need use Abstract as keyword. Abstract
methods can be declared in abstract class or an interface.
2)An Abstract method is a method that is declared without a implementation and must
be implemented by derived class.

Polymorphism:
1)The word polymorphism is derived from two greek words poly and morphs.the word
poly is many and morphs means forms.
using polymorphism we can perform a single task in different ways.behaving in
different ways depending on the input received.
we can perform various operations by using methods with the same name.

Types of Polymorphism: There are two types of polymorphism


---------------------
1)compile-time(Static) 1)method overloading 2)Operator overloading 3)Method hiding
2)Run-Time(Dynamic)1)method overriding

**[while working with polymorphism we need understand two things 1)what happens at
the time of compilation and Execution for a given method call.

what is compile-time polymorphism?


1)n Compile-Time Polymorphism, the method to be executed is determined during the
program's compilation. This happens through method overloading.

what is Run-time polymorphism?


1)In Runtime Polymorphism, the method that gets executed is determined at runtime,
not at compile time. This happens through method overriding.

method overloading:
------------------
method overloading is one of the common way to implement compile-time polymorphism.
1)When multiple methods have the same name but different parameters.it allows the
compiler to decide which method to call based on the arguments provided.

How we can re-implement the parent class method under child class?
in to two different approaches. 1)Method Overriding and 2)Method Hiding

What is Method Overriding?


1)method overriding is an approach of re-implementing the parent class methods
under the child class exactly with the same signature.
2)The parent class methods Which are declared as virtual.if we want use those
parent class method under the child class overriding the methods using the override
modifier.
[Method overriding is an approach of implementing polymorphism]
What is method Hiding?
---------------------
the child classes can re-implement any method of its parent class methods even if
they are not declared as virtual.
2)when we use new keyword we can hide the parent class methods under child class.

Partial class:
-------------
1)To Make any class partial we need to use partial keyword. partial class allows us
to define a class on multiple files.we can physically spilt the content of the
class but logically it is one single unit only.
2)the class file names are different but the class name is going to be the same.
all parts of the partial class must have the same access modifier.

Sealed class:
-------------
1)to make any class sealed we need to use keyword sealed .Sealed class cannot be
inherited any other class.
*2)Sealed class cannot contain any abstract methods.

sealed method:
---------------
1)The method that is defined in a parnet class. If that method cannot overridden
under a child class. if the method is declared as sealed that cannot be inherited.
2)Evey method is a sealed method because overriding is not possible unless the
method is not declared as virtual in the parnet class.
3) If a method is declared as virtual in a class, any child class of it can have
the right to override that method.

Extension Method:
----------------
1)Extension method allows us to add new methods into a class without editing the
source of the class.if a class consists a set of members in it and in the future if
you want add new methods into the class .we can add those methods without making
any changes to the source code of the class.
2)Extension methods must be defined only under the static class.we can able to
create instance of extension an class .because another class is a non-static class.
the method changes into non-static.
3)the frst parameter is a binding parameter which should be the name of the class.

4)Using extension method we can inherit sealed class also.

Static class:
------------
1)The Class Which is created by using the static Modifier is called static class. A
static class can contain only static members.It is not possible to create a
instance of a static class.using the static class name we can directly access
static members.
2)A static class cannot inheratie another class.

You might also like