OOPS
1.Class
- Collection of Objects.
Ex: public class student{ }
2.Objects
- Objects are instance of
class.
- It
is a real time Entity.
-
Object are Blue print of class
- combination
of variables, functions, and data structures.
Ex: student objstudent=new student ();
3.Encapsulations
- Encapsulation is a process of binding the
data members and member functions into a single unit.
- Encapsulation is a process of hiding complex
process out side the world make your object simple.
4.Abstraction
- Abstraction is a process of
hiding the implementation details and displaying the essential features.
5.Constructors
-
Constructors do not have return types
-
Constructors Name Same as Class Name
-
Constructors cannot be declared with the keyword virtual.
-
mainly for memory allocation.
6.Destructors
-
Destructors do not have return types
-
Destructors Name Same as Class Name
- using (~) symbol
7.Inheritance
-
Inheritance is a process of deriving the new class from already existing class
- When Class have Property of another class
called Inheritance
- Process of Object Re-usability
Types of Inheritance
1.Single inheritance
2.Multi-level inheritance
3.Multiple inheritance
4.Multipath inheritance
5.Hierarchical inheritance
6.Hybrid
inheritance
8.Abstract Class
- Abstract
class is a mandatory class.
- Abstract class can have both abstract and non abstract methods.
- we can't create the object of abstract class.
- abstract class have no implementation.
- the abstract methods must be implemented by derived class.
- abstract class can not be sealed or static.
- abstract class can inherit from a class and one or more interface.
- abstract class can not support multiple inheritance.
- using override keyword.
- We can
use an Abstract class as a Base Class.
- Abstract
method must be implemented in the non-Abstract class using the override keyword
Features:
- An abstract calss can inherit
from a class and one or more interfaces.
- An abstract class can implement
code with non-Abstract methods.
- An Abstract class can have
modifiers for methods, properties etc.
- An Abstract class can have
constants and fields.
- An abstract class can implement
a property.
- An abstract class can have
constructors or destructors.
- An abstract class cannot be
inherited from by structures.
- An abstract class cannot
support multiple inheritance.
9.Interface
- C# does not support Multiple Inherientane.
so we are using Interafe.
- Contains Methods, Property, Events, Indexes
- there is no implementation. Only
declaration.
-it contains declarations of events,
indexers, methods and/or properties
10.Polymorphism
- one
from act as many level
- one name many forms, Polymorphism means many
forms.
- one
function behaviors different forms
- one interface,
multiple functions.
two types of Polymorphism
1.
Compile
time polymorphism/Overloading
2.
Runtime
polymorphism/Overriding
1.Compile
Time Polymorphism
-when method created with
same name, but with different signature it's called overloading.
-It is also called early
binding.
-Compile time polymorphism is method and operators overloading.
-In method overloading method
performs the different task at the different input parameters.
Type of overloading
1.constructor
overloading.
2.function
overloading
3.Operator
overloading.
EX:
using System;
namespace method_overloading_polymorphism
{
Class Program
{
Public class Shape
{
Public void Area
(float r)
{
float a
= (float)3.14 *
r; //
here we have used function overload with 1 parameter.
Console.WriteLine
("Area of a circle: {0}",a);
}
Public void Area(float l, float b)
{
float x
= (float)l*
b; //
here we have used function overload with 2 parameters.
Console.WriteLine
("Area of a rectangle: {0}",x);
}
public void Area(float a, float b, float c)
{
float s
= (float)(a*b*c)/2; //
here we have used function overload with 3 parameters.
Console.WriteLine ("Area of a circle: {0}", s);
}
}
Static void Main
(string[] args)
{
Shape ob
= new Shape ();
ob.Area(2.0f);
ob.Area(20.0f,30.0f);
ob.Area(2.0f,3.0f,4.0f);
Console.ReadLine
();
}
}
}
2.Runtime Time Polymorphism
-Runtime
time polymorphism is done using inheritance and virtual functions.
-Method
overriding is called runtime polymorphism.
-It
is also called late binding.
-When overriding a method, you change
the behavior of the method for the derived class. Overloading a method simply involves having another method
with the same prototype.
EX:// Base class
public class BaseClass{public virtual void Method1()
{
Console.Write("Base
Class Method");
}
}
//
Derived class
public class DerivedClass : BaseClass
{
public override void Method1()
{
Console.Write("Derived
Class Method");
}
}//
Using base and derived class
public class Sample
{
public void TestMethod()
{// calling the overriden method
DerivedClass objDC
= new DerivedClass();
objDC.Method1(); // calling the baesd class method
BaseClass objBC
= (BaseClass)objDC;
objDC.Method1();
}
}
Output
Derived
Class Method
Derived
Class Method
11.Access Specifiers
Public -- Accessible outside the class
through object reference.
Private -- Accessible inside the class
only through member functions.
Protected -- Just like private but
Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly.
Accessible through objects.
Protected Internal -- Visible inside the assembly
through objects and in derived classes outside the assembly through member
functions.
12.String
-String is immutable
-Immutable means if you create string object
then you cannot modify it and It always create new object of string type in
memory.
Example
string strMyValue = "Hello Visitor";
// create a new string instance instead of changing the
old one
strMyValue += "How Are";
strMyValue += "You ??";
13.Stringbuilder
-StringBuilder is mutable
-means if create string builder object then you
can perform any operation like insert, replace or append without creating new
instance for every time.it will update string at one place in memory doesnt
create new space in memory.
Example:
StringBuilder sbMyValue = new
StringBuilder("");
sbMyValue.Append("Hello Visitor");
sbMyValue.Append("How Are You ??");
string strMyValue = sbMyValue.ToString();
14. Const
- Const is nothing but "constant"
- a variable of which the value is constant
EX:
Const string a=”testing”;
15. Readonly
- Readonly is the keyword
- value we can change during runtime or we can
assign it at run time but only through the non-static constructor
EX:
Radonly string a=”testing”;
16.Var
- dynamic Doe’s not allows the type of value to
change after it is assigned to initially
EX:
Var a=99; ---intteger
A=”tesing”; --- string now it will show error
17.Dynamic
- dynamic allows the type of value to change
after it is assigned to initially
EX:
- Objects are instance of class.
4.Abstraction
- Abstraction is a process of hiding the implementation details and displaying the essential features.
6.Destructors
7.Inheritance
- Inheritance is a process of deriving the new class from already existing class
- Contains Methods, Property, Events, Indexes
-Compile time polymorphism is method and operators overloading.
{
2.Runtime Time Polymorphism
EX:// Base class
{
}
{
}
{// calling the overriden method
objDC.Method1(); // calling the baesd class method
objDC.Method1();
Output
Public -- Accessible outside the class through object reference.
Private -- Accessible inside the class only through member functions.
Protected -- Just like private but Accessible in derived classes also through member functions.
Internal -- Visible inside the assembly. Accessible through objects.
Protected Internal -- Visible inside the assembly through objects and in derived classes outside the assembly through member functions.
-String is immutable
Example
string strMyValue = "Hello Visitor";
// create a new string instance instead of changing the old one
strMyValue += "How Are";
strMyValue += "You ??";
13.Stringbuilder
-StringBuilder is mutable
sbMyValue.Append("Hello Visitor");
sbMyValue.Append("How Are You ??");
string strMyValue = sbMyValue.ToString();
Var a=99; ---intteger
A=”tesing”; --- string , it will allo
18.Static
- no object
of static class can be created
- static class must contain only static members
19.Delegates
- C# delegates are similar to pointers to functions
- A delegate is a
reference type variable that holds the reference to a method. The reference can
be changed at runtime.
- used to implementing event and call back methods
EX:
public
delegate int MyDelegate (string s);
20.events:
-It is a way to provide
notifications to client when something happens to an object.
21.Garbage Collection
- Garbage
collection (GC) is a form of automatic memory management.
22.OUT
-The out keyword passes arguments by reference.
This is very similar to the ref keyword.
23.REF
-The ref keyword passes arguments by reference.
It means any changes made to this argument in the method will be reflected in
that variable when control returns to the calling method.
24. Sealed
classes
- used to restrict the inheritance feature of
object oriented programming.
- Once a class is defined as
a sealed class, the class cannot be inherited. In C#,
25. Generics
Type safety
Using angle brackets
26. optional parameters
Ex value=null
27. Extention methods
Extention methods enable to add methods to exerting type with out creating a new derived type.
Using this keyword
Convert List to DataTable
DataTable dt = (DataTable) _user;
ActiveDirectory or LDAP Login
using (var context = new PrincipalContext(ContextType.Domain, "KONENET"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
_user.Add(new User()
{
UserFirstName = de.Properties["givenName"].Value == null ? string.Empty : de.Properties["givenName"].Value.ToString(),
UserName = de.Properties["sn"].Value == null ? string.Empty : de.Properties["sn"].Value.ToString(),
Email = de.Properties["samAccountName"].Value == null ? string.Empty : de.Properties["samAccountName"].Value.ToString(),
Phone = de.Properties["userPrincipalName"].Value == null ? string.Empty : de.Properties["userPrincipalName"].Value.ToString()
});
}
DataTable dt = (DataTable)_user;
}
}
Create and Write the File in Network Path
//Method 1 - Local
string filePath = @"D:\Project\Sample\test.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message : Good Morning <br/>" + Environment.NewLine + "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "------------------------------------------------" + Environment.NewLine);
}
//Method 2 – Network Path
System.Net.WebResponse response;
String strMSG = string.Empty;
WebRequest myReq = WebRequest.Create(@"D:\Project\Sample\test1.txt");
myReq.PreAuthenticate = true;
NetworkCredential networkCredential = new NetworkCredential("baskaran_r@hcl.com", "Password@1");
myReq.Credentials = networkCredential;
myReq.Method = "POST";
try
{
using (var sw = new StreamWriter(myReq.GetRequestStream(), System.Text.Encoding.ASCII))
{
sw.Write("tect to be written");
}
response = myReq.GetResponse();
strMSG = string.Format("{0} {1}", response.ContentLength, response.ContentType);
}
catch (Exception ex)
{
strMSG = ex.Message;
}
Read the Data From XML to List
Method 1
System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Load(@"D:\Users\baskaran_r\Samples\xml\WebApplication1\WebApplication1\Content\sample.xml");
var nodes = (from n in xml.Descendants("note")
select new
{
to = (string)n.Element("to").Value,
heading = (string)n.Element("from").Value
}).ToList();
Method 2
List<data> obj123 = new List<data>();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Content/sample.xml"));
string text = string.Empty;
XmlNodeList xnl = doc.SelectNodes("/Group");
foreach (XmlNode node in xnl)
{
XmlNodeList xnl2 = doc.SelectNodes("/Group/note");
foreach (XmlNode node2 in xnl2)
{
obj123.Add(new data()
{
from = node2["from"].InnerText,
to = node2["to"].InnerText
});
}
}
public class data
{
public string from { get; set; }
public string to { get; set; }
}
Read the Data From Json File
using (StreamReader r = new StreamReader(@"c:\users\baskaran_r\documents\visual studio 2015\Projects\WebApplication1\WebApplication1\Scripts\MBOM_1.1.json"))
{
string json = r.ReadToEnd();
JObject o = JObject.Parse(json);
var value2 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["PLM_NAME"];
var value3 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["Organization"];
var value4 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["Description"];
var value5 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["DELMIA_NAME"];
var value6 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["id"];
var value11 = (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.PLM_NAME");
var value12= (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.Organization");
var value13 = (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.id");
return View();
}
public
delegate int MyDelegate (string s);
Convert List to DataTable
DataTable dt = (DataTable) _user;
ActiveDirectory or LDAP Login
using (var context = new PrincipalContext(ContextType.Domain, "KONENET"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
_user.Add(new User()
{
UserFirstName = de.Properties["givenName"].Value == null ? string.Empty : de.Properties["givenName"].Value.ToString(),
UserName = de.Properties["sn"].Value == null ? string.Empty : de.Properties["sn"].Value.ToString(),
Email = de.Properties["samAccountName"].Value == null ? string.Empty : de.Properties["samAccountName"].Value.ToString(),
Phone = de.Properties["userPrincipalName"].Value == null ? string.Empty : de.Properties["userPrincipalName"].Value.ToString()
});
}
DataTable dt = (DataTable)_user;
}
}
Create and Write the File in Network Path
//Method 1 - Local
string filePath = @"D:\Project\Sample\test.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Message : Good Morning <br/>" + Environment.NewLine + "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
writer.WriteLine(Environment.NewLine + "------------------------------------------------" + Environment.NewLine);
}
//Method 2 – Network Path
System.Net.WebResponse response;
String strMSG = string.Empty;
WebRequest myReq = WebRequest.Create(@"D:\Project\Sample\test1.txt");
myReq.PreAuthenticate = true;
NetworkCredential networkCredential = new NetworkCredential("baskaran_r@hcl.com", "Password@1");
myReq.Credentials = networkCredential;
myReq.Method = "POST";
try
{
using (var sw = new StreamWriter(myReq.GetRequestStream(), System.Text.Encoding.ASCII))
{
sw.Write("tect to be written");
}
response = myReq.GetResponse();
strMSG = string.Format("{0} {1}", response.ContentLength, response.ContentType);
}
catch (Exception ex)
{
strMSG = ex.Message;
}
Read the Data From XML to List
Method 1
System.Xml.Linq.XDocument xml = System.Xml.Linq.XDocument.Load(@"D:\Users\baskaran_r\Samples\xml\WebApplication1\WebApplication1\Content\sample.xml");
var nodes = (from n in xml.Descendants("note")
select new
{
to = (string)n.Element("to").Value,
heading = (string)n.Element("from").Value
}).ToList();
Method 2
List<data> obj123 = new List<data>();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("~/Content/sample.xml"));
string text = string.Empty;
XmlNodeList xnl = doc.SelectNodes("/Group");
foreach (XmlNode node in xnl)
{
XmlNodeList xnl2 = doc.SelectNodes("/Group/note");
foreach (XmlNode node2 in xnl2)
{
obj123.Add(new data()
{
from = node2["from"].InnerText,
to = node2["to"].InnerText
});
}
}
public class data
{
public string from { get; set; }
public string to { get; set; }
}
using (StreamReader r = new StreamReader(@"c:\users\baskaran_r\documents\visual studio 2015\Projects\WebApplication1\WebApplication1\Scripts\MBOM_1.1.json"))
{
string json = r.ReadToEnd();
JObject o = JObject.Parse(json);
var value2 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["PLM_NAME"];
var value3 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["Organization"];
var value4 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["Description"];
var value5 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["DELMIA_NAME"];
var value6 = (string)o["IR62AZ_PANEL_1NEW"]["Attributes"]["id"];
var value11 = (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.PLM_NAME");
var value12= (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.Organization");
var value13 = (string)o.SelectToken("IR62AZ_PANEL_1NEW.Attributes.id");
return View();
}
No comments:
Post a Comment