C# Tutorials

 

                                                  Short Notes

1:Access Modifier:-

Private:- Private can be access only within their containing type.

Public:- Public can be access within the current assembly and and the another assembly that is have the reference of current assembly.

Protected:- Protected can be access only wihtin the base class and within the derived class of the base class in the current assembly

Internal:- Internal can be access only within the current assembly.

Protected Internal:- They can be access only within the current assembly and the int another assembly from the derived class.


2:base:-

1.Base keyword call the hidden method of base class in the derived class.

2.Decide which constructor will call of the base class.


The base keyword is used to refer to the base class when chaining constructors or when you want to access a member (method, property, anything)

in the base class that has been overridden or hidden in the current class. 


3:new:-

1.Instance Creation

2.Method Hidding


4:this:-

1.Refer Current class Instance.

2.Differntiate between field and method parametet if they have same name.

3.Invoke the constructor in same class.

4.Invoke current class method 

 

5:using:-

1.As a statement

2.As a directive


6.Fields:-

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.


7.Constant vs ReadOnly:-

const:Define constant field and local variable.Only iniatialize during declartion.

readonly:In c#, the readonly fields can be initialized either at the declaration or in a constructor. 


8.Ref vs Out:-

ref tells the compiler that the object(arguments) is initialized before entering the function, while out tells the compiler that the object(parameters) will be initialized inside the function or called function or function definition.

The out modifier removes the requirement that a reference parameter be initiailzed.


Ref parameters are used to pass value types into a method by reference. This allows you to retrieve their modified value in the calling

method. Out parameters are used only to return information from a method.Within the called method the out parameters must be assigned a value before the method returns.


9.Params:-

By using the params keyword, you can specify a method parameter that takes a variable number of arguments. 

The parameter type must be a single-dimensional array. No additional parameters are permitted after the params keyword in a 

method declaration, and only one params keyword is permitted in a method declaration.


10.Convert.Int32() vs int.parse():-

Parse and Convert ToInt32 are two methods to convert a string to an integer. The main difference between int Parse and Convert ToInt32

in C# is that passing a null value to int Parse will throw an ArgumentNullException while passing a null value to Convert ToInt32 will give zero


11.ToString() and Convert.ToString():-

"Convert" function handles NULLS and return empty, while "i. ToString()" does not it will throw a NULL reference exception error.


12.int.Parse() and int.TryParse():-

In case of the string can’t be converted the int.Parse() throws an exceptions where as int.TryParse() return a bool value, false or true.


13.String vs StringBuilder:- 

string is mutable type means we can not modify string.It will create new object instance in memory everytime. stringBuilder is 

Immutable means we can modify it will not create new instance of object in memory.   


14:string vs System.String:-

string is keyword and String is a class with some added functionality.

https://www.c-sharpcorner.com/blogs/c-sharp-string-vs-string-the-difference


15:Important String function

 Split(),Substring(),Compare(),CompareTo(),Replace(),Contains(),Join(),Trim(),Concat(),Equals(),Length;


16:string interpolation:-

C# string interpolation is a method of concatenating, formatting and manipulating strings.This feature was introduced in C# 6. 


14.var vs dynamic:-

1.With var we don't need to define variable type but initialization is must but with with dynamic we don't need both things type declaration

and iniatialize of variable.

2.Type checking of var at complile time. Type cheking of dynamic type at run time.

3.var is used with local variable and dynamic can be used within a class.   


15.Nullable type:-

In C#, the compiler does not allow you to assign a null value to a variable. So, C# 2.0 provides a special feature to assign a null value to a 

variable that is known as the Nullable type.

One key difference between value types and reference types is that value types always contain a value,

whereas reference types can be null, reflecting the fact that they contain no value. It is, however, possible

to create a value type that behaves like a reference type in this respect (that is, it can be null)

by using nullable types, which are a form of generic.


15.Enum:-

In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values.

Enumerations provide a powerful alternative to constants. An enumeration is a distinct value type,

consisting of a set of named constants (called the enumerator list).



16.Boxing & Unboxing :-

Process of converting value type into reference type(object type) is called as boxing while converting reference type into the value type is 

known as called unboxing.Boxing is implicit; unboxing is explicit.


17.Type Casting:- 

Implicit and Explicit Type Conversion

Type conversion is converting one data type to another data type. It is also known as type casting.

These conversions are perform by C# in a type safe manner.It happens when two data types are compatible.when we convert smaller integral data type

to larger integral data type.

These conversions are done by user predefine function. Explicit conversion require cast operator.

They need when we want to assign value of larger data type to smaller data type.


18:-Array Types

Single

Two Dimentional

Multidimentional

Smart Array or Indexer

ArrayList

BitArray 

 

19:Exception Handling

Exception is a type of error that occur when we execute a program.

Types of Exception

Runtime exption

Compile Time Exception


System Exception :-system.exception is a base class for all CLR generated exception or runtime error.The SystemException class is the base class 

for all the built-in exception classes in .NET Framework.

Built-in Exception Classes:-

ArgumentException

ArgumentNullException

ArgumentOutofrangeException

DivideByZeroException

Null RefrenceException

Out Of MemoryException

IndexOutOfRangeException

FormateException

NullReferenceException


Try:-Enclose that statement where exception can occur.

Catch:-Exception raised by try block handle by catch block.This block can be execute if any error occur otherwise not.

Finally:-The finally block will always be executed whether an exception raised or not.It is used to realeased resources.


Rule for Exception Handling

1. A try block must be followed by catch or finally or both blocks. 

2. Multiple catch block are allowed but multiple catch blocks with the same exception type are not allowed. 

3. Multiple finally blocks are not allowed. Also, the finally block cannot have the return, continue, or break keywords.   


20.throw vs throw ex

throw contains full stack trace information .throw ex does not show full stack trace.

    

21.finally vs using

In the finally{} block we have to manually dispose the object but the Using statement automatically disposes the object when its being created

22.Staic Keywork can be applied to following

  • Classes
  • Method
  • Variable
  • Operators
  • Constructor
  • Properties
  • Event


Diff between sealed and static class

  • Both sealed and static class can not be inherited by another class but we can create the instance of sealed class.   
  • static method unlike a regular (instance) function, is not associated with an instance of the class.we can call a static method with class name.  
  • static methods can be overloaded but cannot be overridden.
  • static variable,When a variable is declared as static, then a single copy of the variable is created and shared among all objects at the class level. 





No comments:

Post a Comment