ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to Fix Error Failed to create directory In Android SDK Not Downloading Packages?
  • What’s new in visual studio 2019?
  • How to access Visual studio wep app using Conveyor Cloud services?
  • Introduction to SQL Server Management Studio(SSMS)
  • Git Commands you must know
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

How to code quicker with code snippets in Visual Studio 2019

Tools Visual Studio about 7 years ago || 1/16/2019 || 13.3 K View

  • Hide

 

Code Snippets are small blocks of code that are reusable which can be inserted into a code file using a simple short cut key. Various code snippets are provided by Visual Studio for many languages including C#, C, Visual Basic to name a few.

Use of Snippets make coding more faster and easier. Like in C# just typing if-else, for each and many other snippets and a double tab key provides whole block of code.

To view the available code snippets you can go to Code Snippets Manager from the tools menu.

An example: just typing prop and pressing double tab

Expands to   public string MyProperty{get; set;}

Furthermore you can even create your own code snippets with just few steps.

The basic steps are provided below:

1. Create a new XML file in Visual Studio and add the XML code as shown below:

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>Add Class</Title>
			<Author>Sateesh</Author>
			<Description>Adds boilerplate code for creating a class</Description>
			<Shortcut>addclass</Shortcut>
		</Header>
		<Snippet>
			<References>
				<Reference>
					<Assembly>System.ComponentModel.DataAnnotations.dll</Assembly>
				</Reference>
			</References>
			<Imports>
				<Import>
					<Namespace>System.ComponentModel.DataAnnotations</Namespace>
				</Import>
			</Imports>
			<Declarations>
				<Literal>
					<ID>classname</ID>
					<ToolTip>Replace with specified name</ToolTip>
					<Default>MyClass</Default>
				</Literal>
				<Object>
					<ID>Object</ID>
					<Type>System.Object</Type>
					<ToolTip>Replace with a object in your application.</ToolTip>
					<Default>myObject</Default>
				</Object>
			</Declarations>
			<Code Language="CSharp">
				<![CDATA[ 
public class $classname${ 
        private static $classname$ myObj= new $classname$(); 
Object con = $Object$; 
        public int Id 
        { 
            get; set; 
        } 
        [StringLength(20)] 
        public string Name 
        { 
            get; set; 
        } 
        [EmailAddress] 
        public string Email 
        { 
            get; set; 
        } 
} 
]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>


The title defines the snippet that is Add Class in our case. The code is for the snippet isprovided in code section inside CDATA. The header section defines the details of the snippets.

2. Save the file with .snippet extension to some locationNow go to tools, click on Code Snippets Manager and import the previously saved snippet file

 

 

3. Now add the file to one of the locations in Code Snippets Manager, most prefer My Code Snippets

 

4. Now the snippet is ready to be used. Create a new console application and type add class and double tab and the snippets expands providing the defination for the class as below:

 

 public class MyClass
        {
            private static MyClass myObj = new MyClass();
            Object con = myObject;
            public int Id
            {
                get; set;
            }
            [StringLength(20)]
            public string Name
            {
                get; set;
            }
            [EmailAddress]
            public string Email
            {
                get; set;
            }
        }

 

In this way we can create our own code snippet and make the coding process easier and faster.

Also Read: Programming Language to Learn in 2019

  • 1
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey