SPSite site = new SPSite("http://example.com"); SPWeb web = site.RootWeb;
SPSite site = new SPSite("http://example.com"); using (SPWeb web = site.OpenWeb()) { // code to work with the web }This example creates a new SPSite object and opens the RootWeb property using the OpenWeb() method. It is surrounded by a using statement to ensure that the SPWeb object is disposed of properly when it is no longer needed. Package Library: Microsoft.SharePoint (for on-premises SharePoint) or Microsoft.SharePoint.Client (for SharePoint online and SharePoint add-ins).